Skip to content

Commit 9a06637

Browse files
committed
Define dedicated error types for HandleOrNull and HandleOrInvalid
Introduce the `InvalidHandleError` and `NullHandleError` error types, corresponding to rust-lang/rust#95387.
1 parent f5b52f0 commit 9a06637

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ pub use traits::{FromHandle, FromSocket, IntoHandle, IntoSocket};
5656
pub use types::{BorrowedFd, OwnedFd};
5757
#[cfg(not(io_lifetimes_use_std))]
5858
#[cfg(windows)]
59-
pub use types::{BorrowedHandle, BorrowedSocket, HandleOrInvalid, OwnedHandle, OwnedSocket};
59+
pub use types::{
60+
BorrowedHandle, BorrowedSocket, HandleOrInvalid, InvalidHandleError, NullHandleError,
61+
OwnedHandle, OwnedSocket,
62+
};
6063

6164
#[cfg(io_lifetimes_use_std)]
6265
#[cfg(unix)]

src/types.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,17 +491,17 @@ impl BorrowedSocket<'_> {
491491

492492
#[cfg(windows)]
493493
impl TryFrom<HandleOrInvalid> for OwnedHandle {
494-
type Error = ();
494+
type Error = InvalidHandleError;
495495

496496
#[inline]
497-
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, ()> {
497+
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, InvalidHandleError> {
498498
let raw = handle_or_invalid.0;
499499
if raw == INVALID_HANDLE_VALUE {
500500
// Don't call `CloseHandle`; it'd be harmless, except that it could
501501
// overwrite the `GetLastError` error.
502502
forget(handle_or_invalid);
503503

504-
Err(())
504+
Err(InvalidHandleError(()))
505505
} else {
506506
Ok(OwnedHandle { handle: raw })
507507
}
@@ -510,23 +510,53 @@ impl TryFrom<HandleOrInvalid> for OwnedHandle {
510510

511511
#[cfg(windows)]
512512
impl TryFrom<HandleOrNull> for OwnedHandle {
513-
type Error = ();
513+
type Error = NullHandleError;
514514

515515
#[inline]
516-
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, ()> {
516+
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NullHandleError> {
517517
let raw = handle_or_null.0;
518518
if raw.is_null() {
519519
// Don't call `CloseHandle`; it'd be harmless, except that it could
520520
// overwrite the `GetLastError` error.
521521
forget(handle_or_null);
522522

523-
Err(())
523+
Err(NullHandleError())
524524
} else {
525525
Ok(OwnedHandle { handle: raw })
526526
}
527527
}
528528
}
529529

530+
/// This is the error type used by [`HandleOrNull`] when attempting to convert
531+
/// into a handle, to indicate that the value is null.
532+
#[derive(Debug, Clone, PartialEq, Eq)]
533+
pub struct NullHandleError(());
534+
535+
impl fmt::Display for NullHandleError {
536+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
537+
"A HandleOrNull could not be converted to a handle because it was null".fmt(fmt)
538+
}
539+
}
540+
541+
impl crate::error::Error for NullHandleError {}
542+
543+
/// This is the error type used by [`HandleOrInvalid`] when attempting to
544+
/// convert into a handle, to indicate that the value is
545+
/// `INVALID_HANDLE_VALUE`.
546+
#[unstable(feature = "io_safety", issue = "87074")]
547+
#[derive(Debug, Clone, PartialEq, Eq)]
548+
pub struct InvalidHandleError(());
549+
550+
#[unstable(feature = "io_safety", issue = "87074")]
551+
impl fmt::Display for InvalidHandleError {
552+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
553+
"A HandleOrInvalid could not be converted to a handle because it was INVALID_HANDLE_VALUE"
554+
.fmt(fmt)
555+
}
556+
}
557+
558+
impl crate::error::Error for InvalidHandleError {}
559+
530560
#[cfg(any(unix, target_os = "wasi"))]
531561
impl AsRawFd for BorrowedFd<'_> {
532562
#[inline]

0 commit comments

Comments
 (0)