pub struct ResGuard<R: Copy> { /* private fields */ }
Expand description

Holds a resource and a free-function (like a non-capturing 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, like ..._destroy_icon()), 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 the instance manages.

Implementations§

source§

impl<R: Copy> ResGuard<R>

source

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

Should normally not be needed.

source

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

For use with functions that return the resource.

source

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

For use with 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: fn(_: R), free_second: fn(_: R) ) -> Result<(Self, Self), E>
where R: Null, A: FnOnce(&mut R, &mut R) -> Result<T, E>,

source§

impl ResGuard<HANDLE>

source

pub fn with_res_and_close_handle(resource: HANDLE) -> Self

source

pub fn with_acq_and_close_handle<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HANDLE, E>,

source

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

source§

impl ResGuard<HBITMAP>

source

pub fn with_res_and_delete_object(resource: HBITMAP) -> Self

source

pub fn with_acq_and_delete_object<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HBITMAP, E>,

source

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

source§

impl ResGuard<HBRUSH>

source

pub fn with_res_and_delete_object(resource: HBRUSH) -> Self

source

pub fn with_acq_and_delete_object<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HBRUSH, E>,

source

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

source§

impl ResGuard<HDC>

source

pub fn with_res_and_delete_dc(resource: HDC) -> Self

source

pub fn with_acq_and_delete_dc<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HDC, E>,

source

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

source§

impl ResGuard<HFONT>

source

pub fn with_res_and_delete_object(resource: HFONT) -> Self

source

pub fn with_acq_and_delete_object<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HFONT, E>,

source

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

source§

impl ResGuard<HGDIOBJ>

source

pub fn with_res_and_delete_object(resource: HGDIOBJ) -> Self

source

pub fn with_acq_and_delete_object<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HGDIOBJ, E>,

source

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

source§

impl ResGuard<HGLOBAL>

source

pub fn with_res_and_global_free(resource: HGLOBAL) -> Self

source

pub fn with_acq_and_global_free<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HGLOBAL, E>,

source

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

source§

impl ResGuard<HICON>

source

pub fn with_res_and_destroy_icon(resource: HICON) -> Self

source

pub fn with_acq_and_destroy_icon<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HICON, E>,

source

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

source§

impl ResGuard<HLOCAL>

source

pub fn with_res_and_local_free(resource: HLOCAL) -> Self

source

pub fn with_acq_and_local_free<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HLOCAL, E>,

source

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

source§

impl ResGuard<HMENU>

source

pub fn with_res_and_destroy_menu(resource: HMENU) -> Self

source

pub fn with_acq_and_destroy_menu<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HMENU, E>,

source

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

source§

impl ResGuard<HMODULE>

source

pub fn with_res_and_free_library(resource: HMODULE) -> Self

source

pub fn with_acq_and_free_library<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HMODULE, E>,

source

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

source§

impl ResGuard<HPALETTE>

source

pub fn with_res_and_delete_object(resource: HPALETTE) -> Self

source

pub fn with_acq_and_delete_object<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HPALETTE, E>,

source

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

source§

impl ResGuard<HPEN>

source

pub fn with_res_and_delete_object(resource: HPEN) -> Self

source

pub fn with_acq_and_delete_object<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HPEN, E>,

source

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

source§

impl ResGuard<HPOWERNOTIFY>

source

pub fn with_res_and_unregister_power_setting_notification( resource: HPOWERNOTIFY ) -> Self

source

pub fn with_acq_and_unregister_power_setting_notification<A, E>( acquire: A ) -> Result<Self, E>
where A: FnOnce() -> Result<HPOWERNOTIFY, E>,

source

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

source§

impl ResGuard<HRGN>

source

pub fn with_res_and_delete_object(resource: HRGN) -> Self

source

pub fn with_acq_and_delete_object<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<HRGN, E>,

source

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

source§

impl ResGuard<PWSTR>

source

pub fn with_res_and_local_free(resource: PWSTR) -> Self

source

pub fn with_acq_and_local_free<A, E>(acquire: A) -> Result<Self, E>
where A: FnOnce() -> Result<PWSTR, E>,

source

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

source§

impl ResGuard<HANDLE>

source

pub fn two_with_mut_acq_and_close_handle<A, T, E>( acquire_both: A ) -> Result<(Self, Self), E>
where A: FnOnce(&mut HANDLE, &mut HANDLE) -> Result<T, E>,

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

Trait Implementations§

source§

impl<R: Copy> Deref for ResGuard<R>

§

type Target = R

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

impl<R: Copy> Drop for ResGuard<R>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<R> RefUnwindSafe for ResGuard<R>
where R: RefUnwindSafe,

§

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

§

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

§

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

§

impl<R> UnwindSafe for ResGuard<R>
where 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> MapSelf for T

source§

fn map_self<O, T>(self, op: O) -> T
where O: FnOnce(Self) -> T, Self: Sized,

Example: Read more
source§

fn map_self_or_keep<O>(self, op: O) -> Self
where O: FnOnce(&Self) -> Option<Self>, Self: Sized,

Example: Read more
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.