-
-
Save mykhailokrainik/89a63f521129eceffed07bad48a66691 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(try_trait)] | |
enum InnerError { | |
NoneError, | |
} | |
trait CustomError: std::error::Error { | |
fn description(&self) -> &str; | |
fn cause(&self) -> Option<&std::error::Error> { | |
None | |
} | |
} | |
impl std::fmt::Display for InnerError { | |
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | |
let msg = match &self { | |
InnerError::NoneError => "NoneError", | |
}; | |
write!(f, "{}", msg) | |
} | |
} | |
impl std::fmt::Debug for InnerError { | |
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | |
write!(f, "{}", format!("{}", self)) | |
} | |
} | |
impl std::error::Error for InnerError { | |
fn description(&self) -> &str { | |
match &self { | |
InnerError::NoneError => "std::error::Error NoneError", | |
} | |
} | |
} | |
impl CustomError for InnerError { | |
fn description(&self) -> &str { | |
match &self { | |
InnerError::NoneError => "CustomError NoneError", | |
} | |
} | |
} | |
fn res() -> Result<i32, Box<dyn CustomError>> { | |
let a = None; | |
let b = a?; | |
Ok(b) | |
} | |
impl From<std::option::NoneError> for Box<dyn CustomError> { | |
fn from(_: std::option::NoneError) -> Self { | |
Box::new(InnerError::NoneError) | |
} | |
} | |
fn main() { | |
match res() { | |
Ok(v) => println!("Recieve {}", v), | |
Err(e) => eprintln!("Error {:?}", e), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment