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.

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.