pub struct Error { /* private fields */ }Expand description
Implementations§
Source§impl Error
impl Error
Sourcepub fn new<E>(kind: ErrorKind, error: E) -> Error
pub fn new<E>(kind: ErrorKind, error: E) -> Error
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.
§Example code
use portable_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 other<E>(error: E) -> Error
pub fn other<E>(error: E) -> Error
Creates a new I/O error from an arbitrary error payload.
This function is used to generically create I/O errors which do not
originate from the OS itself. It is a shortcut for Error::new
with ErrorKind::Other.
§Example code
use portable_io::Error;
// errors can be created from strings
let custom_error = Error::other("oh no!");
// errors can also be created from other errors
let custom_error2 = Error::other(custom_error);Sourcepub fn last_os_error() -> Error
pub fn last_os_error() -> Error
Sourcepub fn from_raw_os_error(code: i32) -> Error
pub fn from_raw_os_error(code: i32) -> Error
Creates a new instance of an Error from a particular OS error code.
os-errorSourcepub fn raw_os_error(&self) -> Option<i32>
pub fn raw_os_error(&self) -> Option<i32>
Returns the OS error that this error represents (if any).
If this Error was constructed via last_os_error or
from_raw_os_error, then this function will return Some, otherwise
it will return None.
§Example code
use portable_io::{Error, ErrorKind};
fn print_os_error(err: &Error) {
if let Some(raw_os_err) = err.raw_os_error() {
println!("raw OS error: {:?}", raw_os_err);
} else {
println!("Not an OS error");
}
}
fn main() {
// Will print "raw OS error: ...".
// (only compiles with `os-error` feature enabled)
// print_os_error(&Error::last_os_error());
// Will print "Not an OS error".
print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
}Sourcepub fn get_ref(&self) -> Option<&(dyn Error + Send + Sync + 'static)>
pub fn get_ref(&self) -> Option<&(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.
§Example code
use portable_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".
// (only compiles with `os-error` feature enabled)
// print_error(&Error::last_os_error());
// Will print "Inner error: ...".
print_error(&Error::new(ErrorKind::Other, "oh no!"));
}Sourcepub fn get_mut(&mut self) -> Option<&mut (dyn Error + Send + Sync + 'static)>
pub fn get_mut(&mut self) -> Option<&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.
§Example code
use portable_io::{Error, ErrorKind};
use core::{error, fmt};
use core::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".
// (only compiles with `os-error` feature enabled)
// print_error(&change_error(Error::last_os_error()));
// Will print "Inner error: ...".
print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
}Sourcepub fn into_inner(self) -> Option<Box<dyn Error + Send + Sync>>
pub fn into_inner(self) -> Option<Box<dyn Error + Send + Sync>>
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.
§Example code
use portable_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".
// (only compiles with `os-error` feature enabled)
// print_error(Error::last_os_error());
// Will print "Inner error: ...".
print_error(Error::new(ErrorKind::Other, "oh no!"));
}Sourcepub fn kind(&self) -> ErrorKind
pub fn kind(&self) -> ErrorKind
Returns the corresponding ErrorKind for this error.
§Example code
use portable_io::{Error, ErrorKind};
fn print_error(err: Error) {
println!("{:?}", err.kind());
}
fn main() {
// Will panic (MISSING FUNCTIONALITY) - SHOULD print "Uncategorized".
// (only compiles with `os-error` feature enabled)
// print_error(Error::last_os_error());
// Will print "AddrInUse".
print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
}