Skip to main content

Guard

Struct Guard 

Source
pub struct Guard<T, F>
where F: FnOnce(T),
{ /* private fields */ }
Expand description

Generic RAII guard that owns a resource and cleans it up on drop

This is the fundamental building block for all RAII patterns in the project. It ensures that resources are properly cleaned up even if panics occur.

§Type Parameters

  • T: The type of resource being managed
  • F: The type of cleanup function (must be FnOnce(T))

§Example

use foundation_utils::raii::Guard;

let _guard = Guard::new(42, |value| {
    println!("Cleaning up value: {}", value);
});
// Cleanup happens automatically when guard drops

Implementations§

Source§

impl<T, F> Guard<T, F>
where F: FnOnce(T),

Source

pub fn new(resource: T, cleanup: F) -> Self

Create a new RAII guard

§Arguments
  • resource: The resource to manage
  • cleanup: Function to call when the guard drops
§Example
use foundation_utils::raii::Guard;

let guard = Guard::new("resource", |r| println!("Cleanup: {}", r));
Source

pub fn resource(&self) -> &T

Get a reference to the managed resource

§Panics

Panics if the guard has already been consumed (should not happen in normal use)

Source

pub fn resource_mut(&mut self) -> &mut T

Get a mutable reference to the managed resource

§Panics

Panics if the guard has already been consumed (should not happen in normal use)

Source

pub fn into_inner(self) -> T

Consume the guard and return the resource without cleanup

This is useful when you want to transfer ownership without cleanup

Source

pub fn cleanup_now(self)

Manually trigger cleanup now (guard becomes invalid after this)

This allows explicit cleanup before the guard would naturally drop

Trait Implementations§

Source§

impl<T, F> Debug for Guard<T, F>
where T: Debug, F: FnOnce(T),

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, F> Drop for Guard<T, F>
where F: FnOnce(T),

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, F> Freeze for Guard<T, F>
where T: Freeze, F: Freeze,

§

impl<T, F> RefUnwindSafe for Guard<T, F>

§

impl<T, F> Send for Guard<T, F>
where T: Send, F: Send,

§

impl<T, F> Sync for Guard<T, F>
where T: Sync, F: Sync,

§

impl<T, F> Unpin for Guard<T, F>
where T: Unpin, F: Unpin,

§

impl<T, F> UnsafeUnpin for Guard<T, F>
where T: UnsafeUnpin, F: UnsafeUnpin,

§

impl<T, F> UnwindSafe for Guard<T, F>
where T: UnwindSafe, F: 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> IntoScoped for T

Source§

type Resource = T

Source§

fn into_scoped(self) -> <T as IntoScoped>::Resource

Convert into a scoped resource
Source§

fn scoped<F, R>(self, f: F) -> R
where Self: Sized, F: FnOnce(&Self::Resource) -> R,

Execute a scoped operation with this resource
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.