with_global_context

Function with_global_context 

Source
pub fn with_global_context<C: 'static, R>(
    f: impl FnOnce(&mut C) -> Result<R>,
) -> Result<R>
Expand description

Runs f with a mutable reference to thread-local global context C.

Returns Error::MissingGlobalContext if no context is registered.

ยงExamples

use ad_tensors_rs::{set_global_context, with_global_context};

let _guard = set_global_context::<usize>(11);
let value = with_global_context::<usize, _>(|ctx| {
    *ctx += 1;
    Ok(*ctx)
})
.unwrap();
assert_eq!(value, 12);