Skip to content

Commit 77abe7a

Browse files
authored
Rollup merge of #84744 - kornelski:enomem, r=joshtriplett
Add ErrorKind::OutOfMemory Ability to express `ENOMEM` as an `io::Error`. I've used `OutOfMemory` as opposed to `NotEnoughMem` or `AllocationFailed`, because "OOM" is used in Rust already. See also #84612
2 parents 966e9e2 + 19568f9 commit 77abe7a

File tree

8 files changed

+15
-2
lines changed

8 files changed

+15
-2
lines changed

library/std/src/io/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ pub enum ErrorKind {
186186
/// This means that the operation can never succeed.
187187
#[stable(feature = "unsupported_error", since = "1.53.0")]
188188
Unsupported,
189+
190+
/// An operation could not be completed, because it failed
191+
/// to allocate enough memory.
192+
#[stable(feature = "out_of_memory_error", since = "1.53.0")]
193+
OutOfMemory,
189194
}
190195

191196
impl ErrorKind {
@@ -210,6 +215,7 @@ impl ErrorKind {
210215
ErrorKind::Other => "other os error",
211216
ErrorKind::UnexpectedEof => "unexpected end of file",
212217
ErrorKind::Unsupported => "unsupported",
218+
ErrorKind::OutOfMemory => "out of memory",
213219
}
214220
}
215221
}

library/std/src/sys/unix/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
149149
libc::ETIMEDOUT => ErrorKind::TimedOut,
150150
libc::EEXIST => ErrorKind::AlreadyExists,
151151
libc::ENOSYS => ErrorKind::Unsupported,
152+
libc::ENOMEM => ErrorKind::OutOfMemory,
152153

153154
// These two constants can have the same value on some systems,
154155
// but different values on others, so we can't use a match

library/std/src/sys/wasi/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub fn decode_error_kind(errno: i32) -> std_io::ErrorKind {
7777
wasi::ERRNO_EXIST => AlreadyExists,
7878
wasi::ERRNO_AGAIN => WouldBlock,
7979
wasi::ERRNO_NOSYS => Unsupported,
80+
wasi::ERRNO_NOMEM => OutOfMemory,
8081
_ => Other,
8182
}
8283
}

library/std/src/sys/windows/c.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ pub const ERROR_FILE_NOT_FOUND: DWORD = 2;
168168
pub const ERROR_PATH_NOT_FOUND: DWORD = 3;
169169
pub const ERROR_ACCESS_DENIED: DWORD = 5;
170170
pub const ERROR_INVALID_HANDLE: DWORD = 6;
171+
pub const ERROR_NOT_ENOUGH_MEMORY: DWORD = 8;
172+
pub const ERROR_OUTOFMEMORY: DWORD = 14;
171173
pub const ERROR_NO_MORE_FILES: DWORD = 18;
172174
pub const ERROR_HANDLE_EOF: DWORD = 38;
173175
pub const ERROR_FILE_EXISTS: DWORD = 80;

library/std/src/sys/windows/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
7171
c::ERROR_PATH_NOT_FOUND => return ErrorKind::NotFound,
7272
c::ERROR_NO_DATA => return ErrorKind::BrokenPipe,
7373
c::ERROR_INVALID_PARAMETER => return ErrorKind::InvalidInput,
74+
c::ERROR_NOT_ENOUGH_MEMORY | c::ERROR_OUTOFMEMORY => return ErrorKind::OutOfMemory,
7475
c::ERROR_SEM_TIMEOUT
7576
| c::WAIT_TIMEOUT
7677
| c::ERROR_DRIVER_CANCEL_TIMEOUT

src/tools/clippy/tests/ui/wildcard_enum_match_arm.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn main() {
7777
let error_kind = ErrorKind::NotFound;
7878
match error_kind {
7979
ErrorKind::NotFound => {},
80-
ErrorKind::PermissionDenied | ErrorKind::ConnectionRefused | ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted | ErrorKind::NotConnected | ErrorKind::AddrInUse | ErrorKind::AddrNotAvailable | ErrorKind::BrokenPipe | ErrorKind::AlreadyExists | ErrorKind::WouldBlock | ErrorKind::InvalidInput | ErrorKind::InvalidData | ErrorKind::TimedOut | ErrorKind::WriteZero | ErrorKind::Interrupted | ErrorKind::Other | ErrorKind::UnexpectedEof | ErrorKind::Unsupported | _ => {},
80+
ErrorKind::PermissionDenied | ErrorKind::ConnectionRefused | ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted | ErrorKind::NotConnected | ErrorKind::AddrInUse | ErrorKind::AddrNotAvailable | ErrorKind::BrokenPipe | ErrorKind::AlreadyExists | ErrorKind::WouldBlock | ErrorKind::InvalidInput | ErrorKind::InvalidData | ErrorKind::TimedOut | ErrorKind::WriteZero | ErrorKind::Interrupted | ErrorKind::Other | ErrorKind::UnexpectedEof | ErrorKind::Unsupported | ErrorKind::OutOfMemory | _ => {},
8181
}
8282
match error_kind {
8383
ErrorKind::NotFound => {},
@@ -99,6 +99,7 @@ fn main() {
9999
ErrorKind::Other => {},
100100
ErrorKind::UnexpectedEof => {},
101101
ErrorKind::Unsupported => {},
102+
ErrorKind::OutOfMemory => {},
102103
_ => {},
103104
}
104105
}

src/tools/clippy/tests/ui/wildcard_enum_match_arm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ fn main() {
9999
ErrorKind::Other => {},
100100
ErrorKind::UnexpectedEof => {},
101101
ErrorKind::Unsupported => {},
102+
ErrorKind::OutOfMemory => {},
102103
_ => {},
103104
}
104105
}

src/tools/clippy/tests/ui/wildcard_enum_match_arm.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ error: wildcard matches known variants and will also match future added variants
3232
--> $DIR/wildcard_enum_match_arm.rs:80:9
3333
|
3434
LL | _ => {},
35-
| ^ help: try this: `ErrorKind::PermissionDenied | ErrorKind::ConnectionRefused | ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted | ErrorKind::NotConnected | ErrorKind::AddrInUse | ErrorKind::AddrNotAvailable | ErrorKind::BrokenPipe | ErrorKind::AlreadyExists | ErrorKind::WouldBlock | ErrorKind::InvalidInput | ErrorKind::InvalidData | ErrorKind::TimedOut | ErrorKind::WriteZero | ErrorKind::Interrupted | ErrorKind::Other | ErrorKind::UnexpectedEof | ErrorKind::Unsupported | _`
35+
| ^ help: try this: `ErrorKind::PermissionDenied | ErrorKind::ConnectionRefused | ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted | ErrorKind::NotConnected | ErrorKind::AddrInUse | ErrorKind::AddrNotAvailable | ErrorKind::BrokenPipe | ErrorKind::AlreadyExists | ErrorKind::WouldBlock | ErrorKind::InvalidInput | ErrorKind::InvalidData | ErrorKind::TimedOut | ErrorKind::WriteZero | ErrorKind::Interrupted | ErrorKind::Other | ErrorKind::UnexpectedEof | ErrorKind::Unsupported | ErrorKind::OutOfMemory | _`
3636

3737
error: aborting due to 5 previous errors
3838

0 commit comments

Comments
 (0)