diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 4a50e647c640..049fe4e43383 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -11,6 +11,7 @@ mod repr_unpacked; #[cfg(not(target_pointer_width = "64"))] use repr_unpacked::Repr; +use crate::collections::{TryReserveError, TryReserveErrorKind}; use crate::convert::From; use crate::error; use crate::fmt; @@ -465,6 +466,19 @@ impl From for Error { } } +#[stable(feature = "io_error_from_try_reserve", since = "1.62.0")] +impl From for Error { + #[inline] + fn from(error: TryReserveError) -> Error { + let kind = match error.kind() { + TryReserveErrorKind::CapacityOverflow => ErrorKind::InvalidInput, + TryReserveErrorKind::AllocError { .. } => ErrorKind::OutOfMemory, + }; + + Error::new(kind, error) + } +} + impl Error { /// Creates a new I/O error from a known kind of error as well as an /// arbitrary error payload.