pub struct Error { /* private fields */ }
Expand description
Implementations§
source§impl Error
impl Error
sourcepub fn new(kind: ErrorKind, error: &'static str) -> Error
pub fn new(kind: ErrorKind, error: &'static str) -> 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
.
§Examples
use core2::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.into_inner().unwrap());
sourcepub fn get_ref(&self) -> Option<&&'static str>
pub fn get_ref(&self) -> Option<&&'static str>
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 core2::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");
}
}
#[cfg(feature = "std")]
fn emit_error() {
// Will print "No inner error".
print_error(&Error::from(std::io::Error::last_os_error()));
// Will print "Inner error: ...".
print_error(&Error::new(ErrorKind::Other, "oh no!"));
}
#[cfg(not(feature = "std"))]
fn emit_error() {
// Will print "No inner error".
print_error(&ErrorKind::Other.into());
// Will print "Inner error: ...".
print_error(&Error::new(ErrorKind::Other, "oh no!"));
}
fn main() {
emit_error();
}
sourcepub fn into_inner(self) -> Option<&'static str>
pub fn into_inner(self) -> Option<&'static str>
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 core2::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");
}
}
#[cfg(feature = "std")]
fn emit_error() {
// Will print "No inner error".
print_error(std::io::Error::last_os_error().into());
// Will print "Inner error: ...".
print_error(Error::new(ErrorKind::Other, "oh no!"));
}
#[cfg(not(feature = "std"))]
fn emit_error() {
// Will print "No inner error".
print_error(ErrorKind::Other.into());
// Will print "Inner error: ...".
print_error(Error::new(ErrorKind::Other, "oh no!"));
}
fn main() {
emit_error();
}
sourcepub fn kind(&self) -> ErrorKind
pub fn kind(&self) -> ErrorKind
Returns the corresponding ErrorKind
for this error.
§Examples
use core2::io::{Error, ErrorKind};
fn print_error(err: Error) {
println!("{:?}", err.kind());
}
#[cfg(feature = "std")]
fn emit_error() {
// Will print "Other".
print_error(std::io::Error::last_os_error().into());
// Will print "AddrInUse".
print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
}
#[cfg(not(feature = "std"))]
fn emit_error() {
// Will print "Other".
print_error(ErrorKind::Other.into());
// Will print "AddrInUse".
print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
}
fn main() {
emit_error();
}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Error
impl RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnwindSafe for Error
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more