diff options
Diffstat (limited to 'library/std/src/io')
| -rw-r--r-- | library/std/src/io/error.rs | 12 | ||||
| -rw-r--r-- | library/std/src/io/tests.rs | 10 |
2 files changed, 22 insertions, 0 deletions
diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 13cc0511e10..7ae15e0fd01 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -83,6 +83,18 @@ impl From<alloc::ffi::NulError> for Error { } } +#[stable(feature = "io_error_from_try_reserve", since = "CURRENT_RUSTC_VERSION")] +impl From<alloc::collections::TryReserveError> for Error { + /// Converts `TryReserveError` to an error with [`ErrorKind::OutOfMemory`]. + /// + /// `TryReserveError` won't be available as the error `source()`, + /// but this may change in the future. + fn from(_: alloc::collections::TryReserveError) -> Error { + // ErrorData::Custom allocates, which isn't great for handling OOM errors. + ErrorKind::OutOfMemory.into() + } +} + // Only derive debug in tests, to make sure it // doesn't accidentally get printed. #[cfg_attr(test, derive(Debug))] diff --git a/library/std/src/io/tests.rs b/library/std/src/io/tests.rs index 5396f7f6e21..c306de3039f 100644 --- a/library/std/src/io/tests.rs +++ b/library/std/src/io/tests.rs @@ -692,3 +692,13 @@ fn read_buf_full_read() { assert_eq!(BufReader::new(FullRead).fill_buf().unwrap().len(), DEFAULT_BUF_SIZE); } + +#[test] +// 64-bit only to be sure the allocator will fail fast on an impossible to satsify size +#[cfg(target_pointer_width = "64")] +fn try_oom_error() { + let mut v = Vec::<u8>::new(); + let reserve_err = v.try_reserve(isize::MAX as usize - 1).unwrap_err(); + let io_err = io::Error::from(reserve_err); + assert_eq!(io::ErrorKind::OutOfMemory, io_err.kind()); +} |
