File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -83,6 +83,18 @@ impl From<alloc::ffi::NulError> for Error {
83
83
}
84
84
}
85
85
86
+ #[stable(feature = "io_error_from_try_reserve", since = "CURRENT_RUSTC_VERSION")]
87
+ impl From<alloc::collections::TryReserveError> for Error {
88
+ /// Converts `TryReserveError` to an error with [`ErrorKind::OutOfMemory`].
89
+ ///
90
+ /// `TryReserveError` won't be available as the error `source()`,
91
+ /// but this may change in the future.
92
+ fn from(_: alloc::collections::TryReserveError) -> Error {
93
+ // ErrorData::Custom allocates, which isn't great for handling OOM errors.
94
+ ErrorKind::OutOfMemory.into()
95
+ }
96
+ }
97
+
86
98
// Only derive debug in tests, to make sure it
87
99
// doesn't accidentally get printed.
88
100
#[cfg_attr(test, derive(Debug))]
Original file line number Diff line number Diff line change @@ -692,3 +692,13 @@ fn read_buf_full_read() {
692
692
693
693
assert_eq!(BufReader::new(FullRead).fill_buf().unwrap().len(), DEFAULT_BUF_SIZE);
694
694
}
695
+
696
+ #[test]
697
+ // 64-bit only to be sure the allocator will fail fast on an impossible to satsify size
698
+ #[cfg(target_pointer_width = "64")]
699
+ fn try_oom_error() {
700
+ let mut v = Vec::<u8>::new();
701
+ let reserve_err = v.try_reserve(isize::MAX as usize - 1).unwrap_err();
702
+ let io_err = io::Error::from(reserve_err);
703
+ assert_eq!(io::ErrorKind::OutOfMemory, io_err.kind());
704
+ }
You can’t perform that action at this time.
0 commit comments