Struct core2::io::Error [−][src]
pub struct Error { /* fields omitted */ }
Expand description
Implementations
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());
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();
}
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();
}
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
Intended for use for errors not exposed to the user, where allocating onto the heap (for normal construction via Error::new) is too costly.