pub trait ErrorContext {
// Required methods
fn into_inner_wc(self) -> Result<Box<dyn Error + Send + Sync>>;
fn get_mut_wc(&mut self) -> Result<&mut (dyn Error + Send + Sync + 'static)>;
fn get_ref_wc(&self) -> Result<&(dyn Error + Send + Sync + 'static)>;
}Required Methods§
Sourcefn into_inner_wc(self) -> Result<Box<dyn Error + Send + Sync>>
fn into_inner_wc(self) -> Result<Box<dyn Error + Send + Sync>>
Consumes the Error, returning its inner error (if any).
If this [Error] was constructed via new or other,
then this function will return Some,
otherwise it will return None.
§Examples
use std::io::{Error, ErrorKind};
fn print_error(err: Error) {
if let Some(inner_err) = err.into_inner() {
println!("Inner error: {inner_err}");
} else {
println!("No inner error");
}
}
fn main() {
// Will print "No inner error".
print_error(Error::last_os_error());
// Will print "Inner error: ...".
print_error(Error::new(ErrorKind::Other, "oh no!"));
}Sourcefn get_mut_wc(&mut self) -> Result<&mut (dyn Error + Send + Sync + 'static)>
fn get_mut_wc(&mut self) -> Result<&mut (dyn Error + Send + Sync + 'static)>
Returns a mutable reference to the inner error wrapped by this error (if any).
If this [Error] was constructed via new then this function will
return Some, otherwise it will return None.
§Examples
use std::io::{Error, ErrorKind};
use std::{error, fmt};
use std::fmt::Display;
#[derive(Debug)]
struct MyError {
v: String,
}
impl MyError {
fn new() -> MyError {
MyError {
v: "oh no!".to_string()
}
}
fn change_message(&mut self, new_message: &str) {
self.v = new_message.to_string();
}
}
impl error::Error for MyError {}
impl Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MyError: {}", self.v)
}
}
fn change_error(mut err: Error) -> Error {
if let Some(inner_err) = err.get_mut() {
inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
}
err
}
fn print_error(err: &Error) {
if let Some(inner_err) = err.get_ref() {
println!("Inner error: {inner_err}");
} else {
println!("No inner error");
}
}
fn main() {
// Will print "No inner error".
print_error(&change_error(Error::last_os_error()));
// Will print "Inner error: ...".
print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
}Sourcefn get_ref_wc(&self) -> Result<&(dyn Error + Send + Sync + 'static)>
fn get_ref_wc(&self) -> Result<&(dyn Error + Send + Sync + 'static)>
Returns a reference to the inner error wrapped by this error (if any).
If this [Error] was constructed via new then this function will
return Some, otherwise it will return None.
§Examples
use std::io::{Error, ErrorKind};
fn print_error(err: &Error) {
if let Some(inner_err) = err.get_ref() {
println!("Inner error: {inner_err:?}");
} else {
println!("No inner error");
}
}
fn main() {
// Will print "No inner error".
print_error(&Error::last_os_error());
// Will print "Inner error: ...".
print_error(&Error::new(ErrorKind::Other, "oh no!"));
}Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".