pub struct ResGuard<R, F>
where F: FnOnce(R),
{ /* private fields */ }
Expand description

Holds a resource and a free-closure that is called when the guard is dropped.

Allows to couple resource acquisition and freeing, while treating the guard as the contained resource and ensuring freeing will happen. When writing the code, it’s also nice to transfer the documentation into everything that has to happen in one go without having to split it into upper and lower or here- and there-code. In a function, Rust’s drop order should ensure that later aquired resources are freed first.

For functions ending in Windows API function names (differently cased), you have to activate crate features. First, see the repository’s read-me. Then, derive the needed features from the Windows API function and the handle type associated with it.

Implementations§

source§

impl<R, F> ResGuard<R, F>
where F: FnOnce(R),

source

pub fn new(resource: R, free: F) -> Self

Should normally not be needed.

source

pub fn with_acquisition<A, E>(acquire: A, free: F) -> Result<Self, E>
where A: FnOnce() -> Result<R, E>,

For functions that return the resource.

source

pub fn with_mut_acquisition<A, T, E>(acquire: A, free: F) -> Result<Self, E>
where A: FnOnce(&mut R) -> Result<T, E>, R: Default,

For functions that provide the resource by means of an out-parameter.

source

pub fn two_with_mut_acquisition<A, T, E>( acquire_both: A, free_first: F, free_second: F ) -> Result<(Self, Self), E>
where A: FnOnce(&mut R, &mut R) -> Result<T, E>, R: Default,

source§

impl<R> ResGuard<R, fn(_: R)>
where R: CanInto<HANDLE> + TypeKind<TypeKind = CopyType> + Clone,

source

pub fn with_acq_and_close_handle<A, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce() -> Result<R, E>,

source

pub fn with_mut_acq_and_close_handle<A, T, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce(&mut R) -> Result<T, E>, R: Default,

source§

impl<R> ResGuard<R, fn(_: R)>
where R: CanInto<HDC> + TypeKind<TypeKind = CopyType> + Clone,

source

pub fn with_acq_and_delete_dc<A, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce() -> Result<R, E>,

source

pub fn with_mut_acq_and_delete_dc<A, T, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce(&mut R) -> Result<T, E>, R: Default,

source§

impl<R> ResGuard<R, fn(_: R)>
where R: CanInto<HGDIOBJ> + TypeKind<TypeKind = CopyType> + Clone,

source

pub fn with_acq_and_delete_object<A, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce() -> Result<R, E>,

source

pub fn with_mut_acq_and_delete_object<A, T, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce(&mut R) -> Result<T, E>, R: Default,

source§

impl<R> ResGuard<R, fn(_: R)>
where R: CanInto<HGLOBAL> + TypeKind<TypeKind = CopyType> + Clone,

source

pub fn with_acq_and_global_free<A, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce() -> Result<R, E>,

source

pub fn with_mut_acq_and_global_free<A, T, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce(&mut R) -> Result<T, E>, R: Default,

source§

impl<R> ResGuard<R, fn(_: R)>
where R: CanInto<HICON> + TypeKind<TypeKind = CopyType> + Clone,

source

pub fn with_acq_and_destroy_icon<A, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce() -> Result<R, E>,

source

pub fn with_mut_acq_and_destroy_icon<A, T, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce(&mut R) -> Result<T, E>, R: Default,

source§

impl<R> ResGuard<R, fn(_: R)>
where R: CanInto<HLOCAL> + TypeKind<TypeKind = CopyType> + Clone,

source

pub fn with_acq_and_local_free<A, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce() -> Result<R, E>,

source

pub fn with_mut_acq_and_local_free<A, T, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce(&mut R) -> Result<T, E>, R: Default,

source§

impl<R> ResGuard<R, fn(_: R)>
where R: CanInto<HMENU> + TypeKind<TypeKind = CopyType> + Clone,

source

pub fn with_acq_and_destroy_menu<A, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce() -> Result<R, E>,

source

pub fn with_mut_acq_and_destroy_menu<A, T, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce(&mut R) -> Result<T, E>, R: Default,

source§

impl<R> ResGuard<R, fn(_: R)>
where R: CanInto<HMODULE> + TypeKind<TypeKind = CopyType> + Clone,

source

pub fn with_acq_and_free_library<A, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce() -> Result<R, E>,

source

pub fn with_mut_acq_and_free_library<A, T, E>( acquire: A ) -> Result<ResGuard<R, fn(_: R)>, E>
where A: FnOnce(&mut R) -> Result<T, E>, R: Default,

source§

impl ResGuard<PWSTR, fn(_: PWSTR)>

source

pub fn with_mut_pwstr_acq_and_local_free<A, T, E>(acquire: A) -> Result<Self, E>
where A: FnOnce(&mut PWSTR) -> Result<T, E>,

Useful for functions like ConvertSidToStringSidW() and FormatMessageW(), which allocate for you and are documented to require a call to LocalFree().

source§

impl<R> ResGuard<R, fn(_: R)>
where R: CanInto<HANDLE> + TypeKind<TypeKind = CopyType> + Clone,

source

pub fn two_with_mut_acq_and_close_handle<A, T, E>( acquire_both: A ) -> Result<(ResGuard<R, fn(_: R)>, ResGuard<R, fn(_: R)>), E>
where A: FnOnce(&mut R, &mut R) -> Result<T, E>, R: Default,

For a function like CreatePipe() that returns two resources at once.

Trait Implementations§

source§

impl<R, F> Deref for ResGuard<R, F>
where F: FnOnce(R),

§

type Target = R

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<R, F> Drop for ResGuard<R, F>
where F: FnOnce(R),

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<R, F> RefUnwindSafe for ResGuard<R, F>

§

impl<R, F> Send for ResGuard<R, F>
where F: Send, R: Send,

§

impl<R, F> Sync for ResGuard<R, F>
where F: Sync, R: Sync,

§

impl<R, F> Unpin for ResGuard<R, F>
where F: Unpin, R: Unpin,

§

impl<R, F> UnwindSafe for ResGuard<R, F>
where F: UnwindSafe, R: UnwindSafe,

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>,

§

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>,

§

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.