From a191d1c271b1c95811e97dbbc0d074c41a0d75a4 Mon Sep 17 00:00:00 2001 From: Nikolai Vazquez Date: Thu, 28 Apr 2022 02:57:19 -0400 Subject: [PATCH 1/3] Implement `From` for `io::Error` The `try_reserve` methods may be called when allocating for I/O. --- library/std/src/io/error.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 4a50e647c640e..27a8ca7a2fd7f 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,21 @@ 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 { + match error.kind() { + TryReserveErrorKind::CapacityOverflow => { + ErrorKind::InvalidInput.into() + } + TryReserveErrorKind::AllocError { .. } => { + ErrorKind::OutOfMemory.into() + } + } + } +} + impl Error { /// Creates a new I/O error from a known kind of error as well as an /// arbitrary error payload. From ecb94ba6ddce5ef3e23cfa450e120ecaca6d17ca Mon Sep 17 00:00:00 2001 From: Nikolai Vazquez Date: Thu, 28 Apr 2022 03:06:21 -0400 Subject: [PATCH 2/3] Add error messages --- library/std/src/io/error.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 27a8ca7a2fd7f..714e14415a2cf 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -472,10 +472,10 @@ impl From for Error { fn from(error: TryReserveError) -> Error { match error.kind() { TryReserveErrorKind::CapacityOverflow => { - ErrorKind::InvalidInput.into() + const_io_error!(ErrorKind::InvalidInput, "collection capacity overflowed") } TryReserveErrorKind::AllocError { .. } => { - ErrorKind::OutOfMemory.into() + const_io_error!(ErrorKind::OutOfMemory, "collection cannot allocate memory") } } } From c35eb0a372d816a4bb3b49a4eb795fd6fbfbdb9e Mon Sep 17 00:00:00 2001 From: Nikolai Vazquez Date: Thu, 28 Apr 2022 03:13:12 -0400 Subject: [PATCH 3/3] Switch to `io::Error::new` --- library/std/src/io/error.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 714e14415a2cf..049fe4e433833 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -470,14 +470,12 @@ impl From for Error { impl From for Error { #[inline] fn from(error: TryReserveError) -> Error { - match error.kind() { - TryReserveErrorKind::CapacityOverflow => { - const_io_error!(ErrorKind::InvalidInput, "collection capacity overflowed") - } - TryReserveErrorKind::AllocError { .. } => { - const_io_error!(ErrorKind::OutOfMemory, "collection cannot allocate memory") - } - } + let kind = match error.kind() { + TryReserveErrorKind::CapacityOverflow => ErrorKind::InvalidInput, + TryReserveErrorKind::AllocError { .. } => ErrorKind::OutOfMemory, + }; + + Error::new(kind, error) } }