Define a dedicated error type for HandleOrNull and HandleOrInvalid.

Define a `NotHandle` type, that implements `std::error::Error`, and use
it as the error type in `HandleOrNull` and `HandleOrInvalid`.
This commit is contained in:
Dan Gohman 2022-03-27 15:41:13 -07:00
parent 311e2683e1
commit 703a33673d

View File

@ -143,17 +143,17 @@ impl BorrowedHandle<'_> {
}
impl TryFrom<HandleOrNull> for OwnedHandle {
type Error = ();
type Error = NotHandle;
#[inline]
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, ()> {
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NotHandle> {
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(NotHandle(()))
} else {
Ok(owned_handle)
}
@ -201,23 +201,37 @@ impl OwnedHandle {
}
impl TryFrom<HandleOrInvalid> for OwnedHandle {
type Error = ();
type Error = NotHandle;
#[inline]
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, ()> {
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, NotHandle> {
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(NotHandle(()))
} else {
Ok(owned_handle)
}
}
}
/// This is the error type used by [`HandleOrInvalid`] and
/// [`HandleOrNull`] when attempting to convert into a handle,
/// to indicate that the value is not a handle.
#[unstable(feature = "io_safety", issue = "87074")]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct NotHandle(());
#[unstable(feature = "io_safety", issue = "87074")]
impl fmt::Display for NotHandle {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"the return value of a Windows API call indicated an error".fmt(fmt)
}
}
impl AsRawHandle for BorrowedHandle<'_> {
#[inline]
fn as_raw_handle(&self) -> RawHandle {