pub struct Error { /* private fields */ }
Expand description

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.

Implementations§

source§

impl Error

source

pub fn new<E>(kind: ErrorKind, error: E) -> Errorwhere E: Into<Box<dyn Error + Sync + Send + 'static, 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.

Note that this function allocates memory on the heap. If no extra payload is required, use the From conversion from ErrorKind.

Examples
use std::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 (and without memory allocation)
let eof_error = Error::from(ErrorKind::UnexpectedEof);
source

pub fn other<E>(error: E) -> Errorwhere E: Into<Box<dyn Error + Sync + Send + 'static, Global>>,

🔬This is a nightly-only experimental API. (io_error_other)

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.

Examples
#![feature(io_error_other)]

use std::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);
source

pub fn last_os_error() -> Error

Returns an error representing the last OS error which occurred.

This function reads the value of errno for the target platform (e.g. GetLastError on Windows) and will return a corresponding instance of Error for the error code.

This should be called immediately after a call to a platform function, otherwise the state of the error value is indeterminate. In particular, other standard library functions may call platform functions that may (or may not) reset the error value even if they succeed.

Examples
use std::io::Error;

let os_error = Error::last_os_error();
println!("last OS error: {os_error:?}");
source

pub fn from_raw_os_error(code: i32) -> Error

Creates a new instance of an Error from a particular OS error code.

Examples

On Linux:

use std::io;

let error = io::Error::from_raw_os_error(22);
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);

On Windows:

use std::io;

let error = io::Error::from_raw_os_error(10022);
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
source

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.

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!"));
}
1.3.0 · source

pub fn get_ref(&self) -> Option<&(dyn Error + Sync + Send + '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!"));
}
1.3.0 · source

pub fn get_mut(&mut self) -> Option<&mut (dyn Error + Sync + Send + '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())));
}
1.3.0 · source

pub fn into_inner( self ) -> Option<Box<dyn Error + Sync + Send + 'static, 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 std::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".
    print_error(Error::last_os_error());
    // Will print "Inner error: ...".
    print_error(Error::new(ErrorKind::Other, "oh no!"));
}
source

pub fn downcast<E>(self) -> Result<Box<E, Global>, Error>where E: Error + Send + Sync + 'static,

🔬This is a nightly-only experimental API. (io_error_downcast)

Attempt to downgrade the inner error to E if any.

If this Error was constructed via new then this function will attempt to perform downgrade on it, otherwise it will return Err.

If downgrade succeeds, it will return Ok, otherwise it will also return Err.

Examples
#![feature(io_error_downcast)]

use std::fmt;
use std::io;
use std::error::Error;

#[derive(Debug)]
enum E {
    Io(io::Error),
    SomeOtherVariant,
}

impl fmt::Display for E {
   // ...
}
impl Error for E {}

impl From<io::Error> for E {
    fn from(err: io::Error) -> E {
        err.downcast::<E>()
            .map(|b| *b)
            .unwrap_or_else(E::Io)
    }
}
source

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

Trait Implementations§

source§

impl AsRef<Error> for OneErr

source§

fn as_ref(&self) -> &Error

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Debug for Error

source§

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

Formats the value using the given formatter. Read more
source§

impl Display for Error

source§

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

Formats the value using the given formatter. Read more
source§

impl Error for Error

source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
source§

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

The lower-level source of this error, if any. Read more
source§

fn provide<'a>(&'a self, demand: &mut Demand<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type based access to context intended for error reports. Read more
§

impl ErrorExt for Error

§

fn id(&self) -> Cow<'_, str>

Get the identifier of this error type, or the string representation.
§

fn err_clone(&self) -> Error

Clone the error maintaining any meta info is available if we are a tx5 error type.
§

impl From<&Error> for Error

§

fn from(e: &Error) -> Error

Converts to this type from the input type.
source§

impl From<&OneErr> for Error

source§

fn from(e: &OneErr) -> Error

Converts to this type from the input type.
§

impl From<ASN1Error> for Error

§

fn from(e: ASN1Error) -> Error

Converts to this type from the input type.
source§

impl From<CompressError> for Error

source§

fn from(data: CompressError) -> Error

Converts to this type from the input type.
§

impl From<ConnectionError> for Error

§

fn from(x: ConnectionError) -> Error

Converts to this type from the input type.
source§

impl From<DecompressError> for Error

source§

fn from(data: DecompressError) -> Error

Converts to this type from the input type.
source§

impl From<Elapsed> for Error

source§

fn from(_err: Elapsed) -> Error

Converts to this type from the input type.
source§

impl From<Elapsed> for Error

source§

fn from(_err: Elapsed) -> Error

Converts to this type from the input type.
§

impl From<Errno> for Error

§

fn from(err: Errno) -> Error

Converts to this type from the input type.
§

impl From<Errno> for Error

§

fn from(err: Errno) -> Error

Converts to this type from the input type.
§

impl From<Error> for AnyDelimiterCodecError

§

fn from(e: Error) -> AnyDelimiterCodecError

Converts to this type from the input type.
source§

impl From<Error> for ConductorApiError

source§

fn from(source: Error) -> Self

Converts to this type from the input type.
source§

impl From<Error> for ConductorConfigError

source§

fn from(source: Error) -> ConductorConfigError

Converts to this type from the input type.
source§

impl From<Error> for ConductorError

source§

fn from(source: Error) -> Self

Converts to this type from the input type.
source§

impl From<Error> for DatabaseError

source§

fn from(source: Error) -> DatabaseError

Converts to this type from the input type.
§

impl From<Error> for DeserializeError

§

fn from(source: Error) -> DeserializeError

Converts to this type from the input type.
source§

impl From<Error> for DnaError

source§

fn from(error: Error) -> DnaError

Converts to this type from the input type.
source§

impl From<Error> for Error

source§

fn from(j: Error) -> Error

Convert a serde_json::Error into an io::Error.

JSON syntax and data errors are turned into InvalidData IO errors. EOF errors are turned into UnexpectedEof IO errors.

use std::io;

enum MyError {
    Io(io::Error),
    Json(serde_json::Error),
}

impl From<serde_json::Error> for MyError {
    fn from(err: serde_json::Error) -> MyError {
        use serde_json::error::Category;
        match err.classify() {
            Category::Io => {
                MyError::Io(err.into())
            }
            Category::Syntax | Category::Data | Category::Eof => {
                MyError::Json(err)
            }
        }
    }
}
§

impl<F> From<Error> for Error<F>where F: ErrorFormatter,

§

fn from(e: Error) -> Error<F>

Converts to this type from the input type.
source§

impl From<Error> for Error

source§

fn from(err: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(from: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(e: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(e: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(source: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(source: Error) -> Error

Converts to this type from the input type.
source§

impl From<Error> for Error

source§

fn from(e: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(err: Error) -> Error

Converts to this type from the input type.
source§

impl From<Error> for Error

source§

fn from(error: Error) -> Error

Converts to this type from the input type.
source§

impl From<Error> for Error

source§

fn from(err: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(e: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(_: Error) -> Error

Converts to this type from the input type.
source§

impl From<Error> for Error

source§

fn from(error: Error) -> Error

Converts to this type from the input type.
source§

impl From<Error> for Error

source§

fn from(_err: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(e: Error) -> Error

Converts to this type from the input type.
§

impl From<Error> for Error

§

fn from(error: Error) -> Error

Creates a new Error::Io from the given error

§

impl From<Error> for Format

§

fn from(err: Error) -> Format

Converts to this type from the input type.
§

impl From<Error> for GetTimezoneError

§

fn from(orig: Error) -> GetTimezoneError

Converts to this type from the input type.
source§

impl From<Error> for KitsuneP2pError

source§

fn from(source: Error) -> KitsuneP2pError

Converts to this type from the input type.
§

impl From<Error> for LinesCodecError

§

fn from(e: Error) -> LinesCodecError

Converts to this type from the input type.
source§

impl From<Error> for ManagedTaskError

source§

fn from(source: Error) -> Self

Converts to this type from the input type.
source§

impl From<Error> for MrBundleError

source§

fn from(source: Error) -> MrBundleError

Converts to this type from the input type.
source§

impl From<Error> for OneErr

source§

fn from(e: Error) -> OneErr

Converts to this type from the input type.
§

impl From<Error> for ProtobufError

§

fn from(err: Error) -> ProtobufError

Converts to this type from the input type.
§

impl From<Error> for SerializeError

§

fn from(source: Error) -> SerializeError

Converts to this type from the input type.
source§

impl From<Error> for UnpackingError

source§

fn from(source: Error) -> UnpackingError

Converts to this type from the input type.
source§

impl From<Error> for WebsocketError

source§

fn from(source: Error) -> WebsocketError

Converts to this type from the input type.
1.14.0 · source§

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.

source§

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}"));
source§

impl From<ErrorStack> for Error

source§

fn from(e: ErrorStack) -> Error

Converts to this type from the input type.
source§

impl<W> From<IntoInnerError<W>> for Error

source§

fn from(iie: IntoInnerError<W>) -> Error

Converts to this type from the input type.
source§

impl From<JoinError> for Error

source§

fn from(src: JoinError) -> Error

Converts to this type from the input type.
source§

impl From<NulError> for Error

source§

impl From<OneErr> for Error

source§

fn from(e: OneErr) -> Error

Converts to this type from the input type.
source§

impl From<PathPersistError> for Error

source§

fn from(error: PathPersistError) -> Error

Converts to this type from the input type.
source§

impl<F> From<PersistError<F>> for Error

source§

fn from(error: PersistError<F>) -> Error

Converts to this type from the input type.
§

impl From<ProtobufError> for Error

§

fn from(err: ProtobufError) -> Error

Converts to this type from the input type.
§

impl From<ReadError> for Error

§

fn from(x: ReadError) -> Error

Converts to this type from the input type.
source§

impl From<SpawnError> for Error

source§

fn from(e: SpawnError) -> Error

Converts to this type from the input type.
§

impl From<ValueWriteError<Error>> for Error

§

fn from(err: ValueWriteError<Error>) -> Error

Converts to this type from the input type.
§

impl From<WriteError> for Error

§

fn from(x: WriteError) -> Error

Converts to this type from the input type.
§

impl Into<Error> for Error

§

fn into(self) -> Error

Converts this type into the (usually inferred) input type.
§

impl NonBlockingError for Error

§

fn into_non_blocking(self) -> Option<Error>

Convert WouldBlock to None and don’t touch other errors.
§

impl NonBlockingError for Error

§

fn into_non_blocking(self) -> Option<Error>

Convert WouldBlock to None and don’t touch other errors.
§

impl TryFrom<Error> for Errno

§

type Error = Error

The type returned in the event of a conversion error.
§

fn try_from(ioerror: Error) -> Result<Errno, Error>

Performs the conversion.
§

impl TryFrom<Format> for Error

§

type Error = DifferentVariant

The type returned in the event of a conversion error.
§

fn try_from(err: Format) -> Result<Error, <Error as TryFrom<Format>>::Error>

Performs the conversion.
§

impl RmpReadErr for Error

§

impl RmpWriteErr for Error

Auto Trait Implementations§

§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Any for Twhere T: Any,

§

fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>

§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>

§

fn type_name(&self) -> &'static str

§

impl<T> AnySync for Twhere T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send + 'static>

§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
§

impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

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> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<E> Provider for Ewhere E: Error + ?Sized,

source§

fn provide<'a>(&'a self, demand: &mut Demand<'a>)

🔬This is a nightly-only experimental API. (provide_any)
Data providers should implement this method to provide all values they are able to provide by using demand. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
§

impl<T> Upcastable for Twhere T: Any + Send + Sync + 'static,

§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

upcast ref
§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

upcast mut ref
§

fn upcast_any_box(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>

upcast boxed dyn
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more