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 managedF: The type of cleanup function (must beFnOnce(T))
§Example
use foundation_utils::raii::Guard;
let _guard = Guard::new(42, |value| {
println!("Cleaning up value: {}", value);
});
// Cleanup happens automatically when guard dropsImplementations§
Source§impl<T, F> Guard<T, F>where
F: FnOnce(T),
impl<T, F> Guard<T, F>where
F: FnOnce(T),
Sourcepub fn resource(&self) -> &T
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)
Sourcepub fn resource_mut(&mut self) -> &mut T
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)
Sourcepub fn into_inner(self) -> T
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
Sourcepub fn cleanup_now(self)
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§
Auto Trait Implementations§
impl<T, F> Freeze for Guard<T, F>
impl<T, F> RefUnwindSafe for Guard<T, F>where
T: RefUnwindSafe,
F: RefUnwindSafe,
impl<T, F> Send for Guard<T, F>
impl<T, F> Sync for Guard<T, F>
impl<T, F> Unpin for Guard<T, F>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more