Struct Scope

Source
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

Source

pub fn scope<F, R>(&self, work: F) -> R
where 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);
});
Source

pub fn debug<F>(&self, log: F)
where F: Fn(Log<'_>) + 'static,

Sets the debugger for this scope.

log will be called for each get and remove.

§Examples
bypass::scope!(static X);

X.debug(|log| { println!("{}", log); });
Source

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);
});
Source

pub fn insert<K: Into<Cow<'static, str>>, V: Any>(&self, key: K, value: V)

Inserts an entry into the scope.

§Panics

Panics if the key already exists.

§Example
bypass::scope!(static X);

X.scope(|| {
    X.insert("abc", 123);
});
Source

pub fn insert_clone<K: Into<Cow<'static, str>>, V: Any + Clone>( &self, key: K, value: V, ) -> V

Inserts an entry into the scope and returns a clone.

Convenience function that prevents the need for manually cloning.

§Panics

Panics if the key already exists.

§Example
bypass::scope!(static X);

X.scope(|| {
    let number = X.insert_clone("abc", 123);
    assert_eq!(number, 123);
});
Source

pub fn get<V: Any + Clone, K: Into<Cow<'static, str>>>(&self, key: K) -> V

Gets a clone of an entry from 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);
    let value: i32 = X.get("abc");
    println!("{}", value);
});
Source

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);
});
Source

pub fn remove<V: Any, K: Into<Cow<'static, str>>>(&self, key: K) -> V

Removes an entry from 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);
    let value: i32 = X.remove("abc");
    println!("{}", value);
});

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> 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, 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.