diff options
| author | bors <bors@rust-lang.org> | 2022-04-27 03:42:59 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-04-27 03:42:59 +0000 |
| commit | bb85bcaca93fd7cf1955d5eb0cb1e16ef462bc7e (patch) | |
| tree | 93cd7e42d20b7ad6d4aaca6bab09b8b61abbd434 /library/std | |
| parent | 99b70ee230a363220d97148d567f07366d7ea4e0 (diff) | |
| parent | 531c9373be094ad4e3ee28464dfacbb2fdea50a5 (diff) | |
| download | rust-bb85bcaca93fd7cf1955d5eb0cb1e16ef462bc7e.tar.gz rust-bb85bcaca93fd7cf1955d5eb0cb1e16ef462bc7e.zip | |
Auto merge of #96195 - sunfishcode:sunfishcode/handle-or-error-type, r=joshtriplett
Define a dedicated error type for `HandleOrNull` and `HandleOrInvalid`. Define `NullHandleError` and `InvalidHandleError` types, that implement std::error::Error, and use them as the error types in `HandleOrNull` and `HandleOrInvalid`, This addresses [this concern](https://github.com/rust-lang/rust/issues/87074#issuecomment-1080031167). This is the same as #95387. r? `@joshtriplett`
Diffstat (limited to 'library/std')
| -rw-r--r-- | library/std/src/os/windows/io/handle.rs | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/library/std/src/os/windows/io/handle.rs b/library/std/src/os/windows/io/handle.rs index ee30cc8be6b..e4de52612ef 100644 --- a/library/std/src/os/windows/io/handle.rs +++ b/library/std/src/os/windows/io/handle.rs @@ -76,7 +76,7 @@ pub struct OwnedHandle { /// `NULL`. This ensures that such FFI calls cannot start using the handle without /// checking for `NULL` first. /// -/// This type concerns any value other than `NULL` to be valid, including `INVALID_HANDLE_VALUE`. +/// This type considers any value other than `NULL` to be valid, including `INVALID_HANDLE_VALUE`. /// This is because APIs that use `NULL` as their sentry value don't treat `INVALID_HANDLE_VALUE` /// as special. /// @@ -96,7 +96,7 @@ pub struct HandleOrNull(OwnedHandle); /// `INVALID_HANDLE_VALUE`. This ensures that such FFI calls cannot start using the handle without /// checking for `INVALID_HANDLE_VALUE` first. /// -/// This type concerns any value other than `INVALID_HANDLE_VALUE` to be valid, including `NULL`. +/// This type considers any value other than `INVALID_HANDLE_VALUE` to be valid, including `NULL`. /// This is because APIs that use `INVALID_HANDLE_VALUE` as their sentry value may return `NULL` /// under `windows_subsystem = "windows"` or other situations where I/O devices are detached. /// @@ -143,17 +143,17 @@ impl BorrowedHandle<'_> { } impl TryFrom<HandleOrNull> for OwnedHandle { - type Error = (); + type Error = NullHandleError; #[inline] - fn try_from(handle_or_null: HandleOrNull) -> Result<Self, ()> { + fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NullHandleError> { let owned_handle = handle_or_null.0; if owned_handle.handle.is_null() { // Don't call `CloseHandle`; it'd be harmless, except that it could // overwrite the `GetLastError` error. forget(owned_handle); - Err(()) + Err(NullHandleError(())) } else { Ok(owned_handle) } @@ -201,23 +201,59 @@ impl OwnedHandle { } impl TryFrom<HandleOrInvalid> for OwnedHandle { - type Error = (); + type Error = InvalidHandleError; #[inline] - fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, ()> { + fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, InvalidHandleError> { let owned_handle = handle_or_invalid.0; if owned_handle.handle == c::INVALID_HANDLE_VALUE { // Don't call `CloseHandle`; it'd be harmless, except that it could // overwrite the `GetLastError` error. forget(owned_handle); - Err(()) + Err(InvalidHandleError(())) } else { Ok(owned_handle) } } } +/// This is the error type used by [`HandleOrNull`] when attempting to convert +/// into a handle, to indicate that the value is null. +// The empty field prevents constructing this, and allows extending it in the future. +#[unstable(feature = "io_safety", issue = "87074")] +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct NullHandleError(()); + +#[unstable(feature = "io_safety", issue = "87074")] +impl fmt::Display for NullHandleError { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + "A HandleOrNull could not be converted to a handle because it was null".fmt(fmt) + } +} + +#[unstable(feature = "io_safety", issue = "87074")] +impl crate::error::Error for NullHandleError {} + +/// This is the error type used by [`HandleOrInvalid`] when attempting to +/// convert into a handle, to indicate that the value is +/// `INVALID_HANDLE_VALUE`. +// The empty field prevents constructing this, and allows extending it in the future. +#[unstable(feature = "io_safety", issue = "87074")] +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct InvalidHandleError(()); + +#[unstable(feature = "io_safety", issue = "87074")] +impl fmt::Display for InvalidHandleError { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + "A HandleOrInvalid could not be converted to a handle because it was INVALID_HANDLE_VALUE" + .fmt(fmt) + } +} + +#[unstable(feature = "io_safety", issue = "87074")] +impl crate::error::Error for InvalidHandleError {} + impl AsRawHandle for BorrowedHandle<'_> { #[inline] fn as_raw_handle(&self) -> RawHandle { |
