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.

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§

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

source

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

Activate feature HANDLE_CloseHandle.

source

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

Activate same feature.

source§

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

source

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

Activate feature HBITMAP_DeleteObject.

source

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

Activate same feature.

source§

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

source

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

Activate feature HBRUSH_DeleteObject.

source

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

Activate same feature.

source§

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

source

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

Activate feature HDC_DeleteDC.

source

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

Activate same feature.

source§

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

source

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

Activate feature HFONT_DeleteObject.

source

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

Activate same feature.

source§

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

source

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

Activate feature HGDIOBJ_DeleteObject.

source

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

Activate same feature.

source§

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

source

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

Activate feature HGLOBAL_GlobalFree.

source

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

Activate same feature.

source§

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

source

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

Activate feature HICON_DestroyIcon.

source

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

Activate same feature.

source§

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

source

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

Activate feature HLOCAL_LocalFree.

source

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

Activate same feature.

source§

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

source

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

Activate feature HMENU_DestroyMenu.

source

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

Activate same feature.

source§

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

source

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

Activate feature HMODULE_FreeLibrary.

source

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

Activate same feature.

source§

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

source

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

Activate feature HPALETTE_DeleteObject.

source

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

Activate same feature.

source§

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

source

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

Activate feature HPEN_DeleteObject.

source

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

Activate same feature.

source§

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

source

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

Activate feature HRGN_DeleteObject.

source

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

Activate same feature.

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.