Struct windows_helpers::ResGuard
source · 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),
impl<R, F> ResGuard<R, F>where
F: FnOnce(R),
sourcepub fn with_acquisition<A, E>(acquire: A, free: F) -> Result<Self, E>
pub fn with_acquisition<A, E>(acquire: A, free: F) -> Result<Self, E>
For functions that return the resource.
sourcepub fn with_mut_acquisition<A, T, E>(acquire: A, free: F) -> Result<Self, E>
pub fn with_mut_acquisition<A, T, E>(acquire: A, free: F) -> Result<Self, E>
For functions that provide the resource by means of an out-parameter.
sourcepub fn two_with_mut_acquisition<A, T, E>(
acquire_both: A,
free_first: F,
free_second: F
) -> Result<(Self, Self), E>
pub fn two_with_mut_acquisition<A, T, E>( acquire_both: A, free_first: F, free_second: F ) -> Result<(Self, Self), E>
For purpose, see Self::two_with_mut_acq_and_close_handle().
source§impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HANDLE> + TypeKind<TypeKind = CopyType> + Clone,
impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HANDLE> + TypeKind<TypeKind = CopyType> + Clone,
source§impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HGDIOBJ> + TypeKind<TypeKind = CopyType> + Clone,
impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HGDIOBJ> + TypeKind<TypeKind = CopyType> + Clone,
source§impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HGLOBAL> + TypeKind<TypeKind = CopyType> + Clone,
impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HGLOBAL> + TypeKind<TypeKind = CopyType> + Clone,
source§impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HICON> + TypeKind<TypeKind = CopyType> + Clone,
impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HICON> + TypeKind<TypeKind = CopyType> + Clone,
source§impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HLOCAL> + TypeKind<TypeKind = CopyType> + Clone,
impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HLOCAL> + TypeKind<TypeKind = CopyType> + Clone,
source§impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HMENU> + TypeKind<TypeKind = CopyType> + Clone,
impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HMENU> + TypeKind<TypeKind = CopyType> + Clone,
source§impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HMODULE> + TypeKind<TypeKind = CopyType> + Clone,
impl<R> ResGuard<R, fn(_: R)>where
R: CanInto<HMODULE> + TypeKind<TypeKind = CopyType> + Clone,
source§impl ResGuard<PWSTR, fn(_: PWSTR)>
impl ResGuard<PWSTR, fn(_: PWSTR)>
sourcepub fn with_mut_pwstr_acq_and_local_free<A, T, E>(acquire: A) -> Result<Self, E>
pub fn with_mut_pwstr_acq_and_local_free<A, T, E>(acquire: A) -> Result<Self, E>
Useful for functions like ConvertSidToStringSidW() and FormatMessageW(), which allocate for you and are documented to require a call to LocalFree().