pub struct StateGuard<S> { /* private fields */ }
Expand description
State 守卫,使用 RAII 模式自动管理 State 的设置和清理
当 StateGuard 被创建时,会自动设置当前线程的 State 上下文 当 StateGuard 被丢弃时(包括异常情况),会自动清理 State 上下文
§示例
ⓘ
use std::sync::Arc;
use mf_state::State;
use mf_rules_expression::functions::StateGuard;
// 创建 State
let state = Arc::new(State::default());
{
// 设置 State 上下文
let _guard = StateGuard::new(state);
// 在这个作用域内,自定义函数可以访问 State
// 即使发生 panic,State 也会被正确清理
} // 这里 StateGuard 被自动丢弃,State 上下文被清理
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for StateGuard<S>
impl<S> RefUnwindSafe for StateGuard<S>where
S: RefUnwindSafe,
impl<S> Send for StateGuard<S>where
S: Send,
impl<S> Sync for StateGuard<S>where
S: Sync,
impl<S> Unpin for StateGuard<S>where
S: Unpin,
impl<S> UnwindSafe for StateGuard<S>where
S: 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