pub struct Error { /* private fields */ }
Expand description
Implementations§
source§impl Error
impl Error
sourcepub fn new<E>(kind: ErrorKind, error: E) -> Errorwhere
E: Into<Box<dyn ErrorTrait + Send + Sync, Global>>,
pub fn new<E>(kind: ErrorKind, error: E) -> Errorwhere E: Into<Box<dyn ErrorTrait + Send + Sync, Global>>,
Creates a new I/O error from a known kind of error as well as an arbitrary error payload.
This function is used to generically create I/O errors which do not
originate from the OS itself. The error
argument is an arbitrary
payload which will be contained in this Error
.
If no extra payload is required, use the From
conversion from
ErrorKind
.
Examples
use acid_io::{Error, ErrorKind};
// errors can be created from strings
let custom_error = Error::new(ErrorKind::Other, "oh no!");
// errors can also be created from other errors
let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
// creating an error without payload
let eof_error = Error::from(ErrorKind::UnexpectedEof);
sourcepub fn get_ref(&self) -> Option<&(dyn ErrorTrait + Send + Sync + 'static)>
pub fn get_ref(&self) -> Option<&(dyn ErrorTrait + 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 acid_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 "Inner error: ...".
print_error(&Error::new(ErrorKind::Other, "oh no!"));
}
sourcepub fn get_mut(
&mut self
) -> Option<&mut (dyn ErrorTrait + Send + Sync + 'static)>
pub fn get_mut( &mut self ) -> Option<&mut (dyn ErrorTrait + 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 core::fmt::{self, Display};
use acid_io::{Error, ErrorKind};
#[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 acid_io::ErrorTrait 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 "Inner error: ...".
print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
}
sourcepub fn into_inner(self) -> Option<Box<dyn ErrorTrait + Send + Sync, Global>>
pub fn into_inner(self) -> Option<Box<dyn ErrorTrait + Send + Sync, Global>>
Consumes the Error
, returning its inner error (if any).
If this Error
was constructed via new
then this function will
return [Some
], otherwise it will return [None
].
Examples
use acid_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 "Inner error: ...".
print_error(Error::new(ErrorKind::Other, "oh no!"));
}
Trait Implementations§
source§impl ErrorTrait for Error
impl ErrorTrait for Error
source§fn source(&self) -> Option<&(dyn ErrorTrait + 'static)>
fn source(&self) -> Option<&(dyn ErrorTrait + 'static)>
source§impl From<ErrorKind> for Error
impl From<ErrorKind> for Error
Intended for use for errors not exposed to the user, where allocating onto the heap (for normal construction via Error::new) is too costly.