diff options
| author | Kornel <kornel@geekhood.net> | 2024-02-21 15:56:50 +0000 |
|---|---|---|
| committer | Kornel <kornel@geekhood.net> | 2024-02-21 16:31:53 +0000 |
| commit | e49cd1c578227e450f0a703b08fa429e3931c228 (patch) | |
| tree | 44fdca08111610c5995d258be3d7b9546fd50b17 /library/std/src | |
| parent | 1d447a9946effc38c4b964a888ab408a3df3c246 (diff) | |
| download | rust-e49cd1c578227e450f0a703b08fa429e3931c228.tar.gz rust-e49cd1c578227e450f0a703b08fa429e3931c228.zip | |
TryReserveError to ErrorKind::OutOfMemory
Diffstat (limited to 'library/std/src')
| -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()); +} |
