Struct Error

pub struct Error { pub(in ::io::error) repr: Repr }

The error type for I/O operations of the Read, Write, Seek, and associated traits.

Errors mostly originate from the underlying OS, but custom instances of Error can be created with crafted error messages and a particular value of ErrorKind.

Fields

repr: Repr

Implementations

impl Error

const INVALID_UTF8: Self = _;
const READ_EXACT_EOF: Self = _;
const UNKNOWN_THREAD_COUNT: Self = _;
const UNSUPPORTED_PLATFORM: Self = _;
const WRITE_ALL_EOF: Self = _;
const ZERO_TIMEOUT: Self = _;
const NO_ADDRESSES: Self = _;

impl Error

unsafe fn from_custom_owner(custom: CustomOwner) -> Error

Safety

The provided CustomOwner must have been constructed from a Box from the alloc crate.

fn into_custom_owner(self) -> Result<CustomOwner, Self>
const fn from_static_message(msg: &'static SimpleMessage) -> Error

Creates a new I/O error from a known kind of error as well as a constant message.

This function does not allocate.

You should not use this directly, and instead use the const_error! macro: io::const_error!(ErrorKind::Something, "some_message").

This function should maybe change to from_static_message<const MSG: &'static str>(kind: ErrorKind) in the future, when const generics allow that.

unsafe fn from_raw_os_error_with_functions(code: RawOsError, functions: &'static OsFunctions) -> Error

Safety

functions must point to data that is entirely constant; it must not be created during runtime.

fn raw_os_error(&self) -> Option<RawOsError>

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.

Examples

use std::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: ...".
    print_os_error(&Error::last_os_error());
    // Will print "Not an OS error".
    print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
}
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.

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!"));
}
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.

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())));
}
fn kind(&self) -> ErrorKind

Returns the corresponding ErrorKind for this error.

This may be a value set by Rust code constructing custom io::Errors, or if this io::Error was sourced from the operating system, it will be a value inferred from the system's error encoding. See last_os_error for more details.

Examples

use std::io::{Error, ErrorKind};

fn print_error(err: Error) {
    println!("{:?}", err.kind());
}

fn main() {
    // As no error has (visibly) occurred, this may print anything!
    // It likely prints a placeholder for unidentified (non-)errors.
    print_error(Error::last_os_error());
    // Will print "AddrInUse".
    print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
}
fn is_interrupted(&self) -> bool

Trait Implementations

impl Debug for Error

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Display for Error

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

impl Error for Error

fn cause(&self) -> Option<&dyn Error>
fn source(&self) -> Option<&dyn Error + 'static>

impl From<ErrorKind> for Error

fn from(kind: ErrorKind) -> Error

Converts an ErrorKind into an Error.

This conversion creates a new error with a simple representation of error kind.

Examples

use std::io::{Error, ErrorKind};

let not_found = ErrorKind::NotFound;
let error = Error::from(not_found);
assert_eq!("entity not found", format!("{error}"));

Auto Trait Implementations

impl !RefUnwindSafe for Error

impl !UnwindSafe for Error

impl Freeze for Error

impl Send for Error

impl Sync for Error

impl Unpin for Error

impl UnsafeUnpin for Error

Blanket Implementations

impl<T> Any for Error where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Error where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Error where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for Error

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for Error where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for Error

impl<T, U> Into<U> for Error where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Error where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for Error where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>