Struct HandleOrInvalid

1.63.0 ยท Source
pub struct HandleOrInvalid(/* private fields */);
Available on Windows only.
Expand description

FFI type for handles in return values or out parameters, where INVALID_HANDLE_VALUE is used as a sentry value to indicate errors, such as in the return value of CreateFileW. This uses repr(transparent) and has the representation of a host handle, so that it can be used in such FFI declarations.

The only thing you can usefully do with a HandleOrInvalid is to convert it into an OwnedHandle using its TryFrom implementation; this conversion takes care of the check for INVALID_HANDLE_VALUE. This ensures that such FFI calls cannot start using the handle without checking for INVALID_HANDLE_VALUE first.

This type may hold any handle value that OwnedHandle may hold, except that when it holds -1, that value is interpreted to mean INVALID_HANDLE_VALUE.

If holds a handle other than INVALID_HANDLE_VALUE, it will close the handle on drop.

Implementationsยง

Sourceยง

impl HandleOrInvalid

1.63.0 ยท Source

pub unsafe fn from_raw_handle(handle: RawHandle) -> Self

Constructs a new instance of Self from the given RawHandle returned from a Windows API that uses INVALID_HANDLE_VALUE to indicate failure, such as CreateFileW.

Use HandleOrNull instead of HandleOrInvalid for APIs that use null to indicate failure.

ยงSafety

The passed handle value must either satisfy the safety requirements of FromRawHandle::from_raw_handle, or be INVALID_HANDLE_VALUE (-1). Note that not all Windows APIs use INVALID_HANDLE_VALUE for errors; see here for the full story.

Trait Implementationsยง

1.63.0 ยท Sourceยง

impl Debug for HandleOrInvalid

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
1.63.0 ยท Sourceยง

impl Drop for HandleOrInvalid

Sourceยง

fn drop(&mut self)

Executes the destructor for this type. Read more
1.63.0 ยท Sourceยง

impl TryFrom<HandleOrInvalid> for OwnedHandle

Sourceยง

type Error = InvalidHandleError

The type returned in the event of a conversion error.
Sourceยง

fn try_from( handle_or_invalid: HandleOrInvalid, ) -> Result<Self, InvalidHandleError>

Performs the conversion.
1.63.0 ยท Sourceยง

impl Send for HandleOrInvalid

1.63.0 ยท Sourceยง

impl Sync for HandleOrInvalid

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.