Skip to content

Commit

Permalink
#53 Add with_state
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Dec 11, 2023
1 parent 18985bc commit 87c0b7e
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,17 @@ pub struct Context {

thread_local! {
pub(crate) static CONTEXT: RefCell<Context> = RefCell::new(Context::new());
pub(crate) static STATE_MAP: RefCell<StateMap> = RefCell::new(StateMap::new());
}

pub(crate) fn with_context<T>(id: ContextId, f: impl FnOnce(&mut Context) -> T) -> T {
CONTEXT.with(|cx| f(&mut cx.borrow_mut()))
}

pub(crate) fn with_state<T>(f: impl FnOnce(&mut StateMap) -> T) -> T {
STATE_MAP.with(|map| f(&mut map.borrow_mut()))
}

impl Default for Context {
fn default() -> Self {
Self::new()
Expand Down

0 comments on commit 87c0b7e

Please sign in to comment.