pub struct Scope { /* private fields */ }Expand description
Named scope created using bypass::scope.
§Examples
// Declare a thread-local scope called `NAME`.
bypass::scope!(static NAME);
// Creates the first, top-level subscope.
NAME.scope(|| {
// Perform operations on the scope.
NAME.insert("x", 123);
});Implementations§
Source§impl Scope
impl Scope
Sourcepub fn scope<F, R>(&self, work: F) -> Rwhere
F: FnOnce() -> R,
pub fn scope<F, R>(&self, work: F) -> Rwhere
F: FnOnce() -> R,
A scope constrains the extent of inserts. It can insert into its parent using lift.
§Examples
bypass::scope!(static NAME);
NAME.scope(|| {
NAME.insert("x", 1);
NAME.scope(|| {
NAME.insert("x", 2);
let value: i32 = NAME.get("x");
assert_eq!(value, 2);
});
let value: i32 = NAME.get("x");
assert_eq!(value, 1);
});Sourcepub fn lift<A: Into<Cow<'static, str>>>(&self, from: A) -> Lift
pub fn lift<A: Into<Cow<'static, str>>>(&self, from: A) -> Lift
Lifts a given key to the parent scope.
Core operations whose keys match a
lift will perform their operations in the parent
scope. If no parent scope exists then the current scope is used.
§Panics
Panics if a lift of the same key already exists in the current scope.
§Examples
bypass::scope!(static X);
X.scope(|| {
X.scope(|| {
X.lift("x");
X.insert("x", 123);
});
let value: i32 = X.get("x");
println!("{}", value);
});Sourcepub fn insert_clone<K: Into<Cow<'static, str>>, V: Any + Clone>(
&self,
key: K,
value: V,
) -> V
pub fn insert_clone<K: Into<Cow<'static, str>>, V: Any + Clone>( &self, key: K, value: V, ) -> V
Sourcepub fn modify<V: Any, K: Into<Cow<'static, str>>, F: FnOnce(&mut V) -> R, R>(
&self,
key: K,
modifier: F,
) -> R
pub fn modify<V: Any, K: Into<Cow<'static, str>>, F: FnOnce(&mut V) -> R, R>( &self, key: K, modifier: F, ) -> R
Modify an entry in the scope.
§Panics
Panics if the key does not exist, or if the value associated with
this key is not does not match V.
§Examples
bypass::scope!(static X);
X.scope(|| {
X.insert("abc", 123);
X.modify("abc", |item: &mut i32| {
*item += 1;
});
let value: i32 = X.get("abc");
println!("{}", value);
assert_eq!(value, 124);
});Auto Trait Implementations§
impl Freeze for Scope
impl RefUnwindSafe for Scope
impl Send for Scope
impl Sync for Scope
impl Unpin for Scope
impl UnwindSafe for Scope
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