Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

std::option::NoneError, what to do? #61

Closed
stephanbuys opened this issue Nov 18, 2017 · 3 comments
Closed

std::option::NoneError, what to do? #61

stephanbuys opened this issue Nov 18, 2017 · 3 comments

Comments

@stephanbuys
Copy link

Hi, I scoured the docs and had a bit of a hard time discerning if Failure should handle the NoneError type that can be returned when a you call to_str() on a PathBuf, I'm migrating my code from error_chain and I used the .chain_error() method to convert these errors into compatible types. Am I missing something or should I implement From fro NoneError?

@Celti
Copy link

Celti commented Nov 18, 2017

As I just discovered a couple hours ago (see #59), NoneError can not implement Fail at this time, because it lacks an implementation of Display (and we can't simply derive both because it's an external type, AIUI). This could be fixed if NoneError gains an impl of Display or of std::error::Error.

If you're using the Error and ErrorKind pattern for failure, you could easily implement From<NoneError> for your own Error type. The crates I'm working on haven't grown to needing their own Error type yet, and I'm too lazy to do that just for NoneError, so I'm sticking with Option::ok_or(err_msg("Option did not contain a value.")) for now.

@stephanbuys
Copy link
Author

Thank you - I'm also in the early stages and don't need Error types yet, so I ok_or it is.

@U007D
Copy link
Contributor

U007D commented Mar 16, 2018

If you're using the Error and ErrorKind pattern for failure, you could easily implement From for your own Error type.

@Celti can you elaborate on this? I am implementing my custom error type:

#[derive(Fail, Debug)]
enum MyError {
    Variant1,
}

// impl Display for MyError

impl From<NoneError> for MyError {
    fn from(err: NoneError) -> Self {
        MyError::Variant1
    }
}

gives the trait bound std::option::NoneError: std::error::Error is not satisfied when I try to use the error check operator: foo()? (where fn foo() -> Option<T>).

How to work around this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants