Skip to main content

ContextGuard

Struct ContextGuard 

Source
pub struct ContextGuard<T, S, C>
where S: FnOnce(&T), C: FnOnce(),
{ /* private fields */ }
Expand description

Context guard that manages thread-local or scoped context

This is a specialized guard for managing context that needs to be set and cleared in a scoped manner.

Implementations§

Source§

impl<T, S, C> ContextGuard<T, S, C>
where S: FnOnce(&T), C: FnOnce(),

Source

pub fn new(context: T, setter: S, clearer: C) -> Self

Create a new context guard

§Arguments
  • context: The context value to manage
  • setter: Function to set the context
  • clearer: Function to clear the context
§Example
use foundation_utils::raii::ContextGuard;

let _guard = ContextGuard::new(
    "my_context",
    |ctx| println!("set: {}", ctx),
    || println!("cleared"),
);
// Context is automatically cleared when guard drops
Source

pub fn context(&self) -> &T

Get a reference to the context

Trait Implementations§

Source§

impl<T, S, C> Drop for ContextGuard<T, S, C>
where S: FnOnce(&T), C: FnOnce(),

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, S, C> Freeze for ContextGuard<T, S, C>
where T: Freeze, S: Freeze, C: Freeze,

§

impl<T, S, C> RefUnwindSafe for ContextGuard<T, S, C>

§

impl<T, S, C> Send for ContextGuard<T, S, C>
where T: Send, S: Send, C: Send,

§

impl<T, S, C> Sync for ContextGuard<T, S, C>
where T: Sync, S: Sync, C: Sync,

§

impl<T, S, C> Unpin for ContextGuard<T, S, C>
where T: Unpin, S: Unpin, C: Unpin,

§

impl<T, S, C> UnsafeUnpin for ContextGuard<T, S, C>

§

impl<T, S, C> UnwindSafe for ContextGuard<T, S, C>
where T: UnwindSafe, S: UnwindSafe, C: 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.