Simulate OOM for the try_oom_error test · rust-lang/rust@53f488a (original) (raw)

`@@ -811,13 +811,17 @@ fn read_to_end_error() {

`

811

811

`}

`

812

812

``

813

813

`#[test]

`

814

``

`-

// Miri does not support signalling OOM

`

815

``

`-

#[cfg_attr(miri, ignore)]

`

816

``

`-

// 64-bit only to be sure the allocator will fail fast on an impossible to satisfy size

`

817

``

`-

#[cfg(target_pointer_width = "64")]

`

818

814

`fn try_oom_error() {

`

819

``

`-

let mut v = Vec::::new();

`

820

``

`-

let reserve_err = v.try_reserve(isize::MAX as usize - 1).unwrap_err();

`

``

815

`+

use alloc::alloc::Layout;

`

``

816

`+

use alloc::collections::{TryReserveError, TryReserveErrorKind};

`

``

817

+

``

818

`` +

// We simulate a Vec::try_reserve error rather than attempting a huge size for real. This way

``

``

819

`+

// we're not subject to the whims of optimization that might skip the actual allocation, and it

`

``

820

`+

// also works for 32-bit targets and miri that might not OOM at all.

`

``

821

`+

let layout = Layout:🆕:();

`

``

822

`+

let kind = TryReserveErrorKind::AllocError { layout, non_exhaustive: () };

`

``

823

`+

let reserve_err = TryReserveError::from(kind);

`

``

824

+

821

825

`let io_err = io::Error::from(reserve_err);

`

822

826

`assert_eq!(io::ErrorKind::OutOfMemory, io_err.kind());

`

823

827

`}

`