Struct Scope

Source
pub struct Scope<RT: Runtime> { /* private fields */ }
Expand description

Signals are created in scopes and can only be deleted by discarding the scope.

Scopes are created in a tree structure, where the root scope is created by one of the runtimes, and child scopes can be added to any Scope by calling the new_child() function on a scope.

When calling a Scope’s discard() function, the Scope and it’s child scopes are discarded together with their signals.

Internally, a Scope is really just a u16 index into an arena based tree which contains the full ScopeInner data (not exposed in the api doc). The Scope implements Copy which makes it much easier to use in closures.

There can be a maximum of 65k Scopes.

§Typed attached data

This has not been implemented! A proof of concept has been done but the exact details of how the api will work are not cemented yet.

It is possible to attach data to a Scope and then, in a type-safe and performant manner, access it. When attached to a Scope the data gets transformed into a Signal which can be retrieved with a function named as the data struct but snake-cased.

You can add several nested data values to a scope. The cost of adding one is 2 bytes added to the scope id which is the vector index of the signal plus the cost of the Signal added to the ScopeInner.

// derive using Scoped with the input scope type as argument
// the generated scope wrapper is named MyCounterScope
#[derive(Scoped(Scope), Clone, Copy)]
struct MyCounter(u8);

// a derive with the name argument as well
#[derive(Scoped(MyCounterScope, name = "BaseScope"), Clone)]
struct MyGreeting(String);

fn some_func<RT: Runtime>(sc: Scope<RT>) {
    // create your data
    let count = MyCounter(0);

    // attach it to the scope (type annotations not necessary)
    let sc: MyCounterScope<RT: Runtime> = count.attach_to(sc);
     
    // The MyCount instance can be accessed as a signal (type annotations not necessary)
    let count_signal: Signal<EqData<i32>, RT> = sc.my_counter();
     
    // Create a MyGreeting and attach it to the MyCounterScope
    let sc: BaseScope<RT: Runtime> = MyGreeting("hi ".to_string()).attach_to(sc);

    next_func(sc);
}

// The scope is passed as a typed parameter
fn next_func<RT: Runtime>(sc: BaseScope<RT>) {
    // the scoped data can be modified
    sc.my_greeting().update(|s| *s = *s.trim());

    signal!(sc, move || {
        sc.my_greeting().with(|greet| println!("{greet} {} times", sc.my_counter().get()))
    });
}

Implementations§

Source§

impl<RT: Runtime> Scope<RT>

Source

pub fn new_child(&self) -> Self

Source

pub fn discard(self)

Trait Implementations§

Source§

impl<RT: Clone + Runtime> Clone for Scope<RT>

Source§

fn clone(&self) -> Scope<RT>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<RT: Copy + Runtime> Copy for Scope<RT>

Auto Trait Implementations§

§

impl<RT> Freeze for Scope<RT>
where RT: Freeze,

§

impl<RT> RefUnwindSafe for Scope<RT>
where RT: RefUnwindSafe,

§

impl<RT> Send for Scope<RT>
where RT: Send,

§

impl<RT> Sync for Scope<RT>
where RT: Sync,

§

impl<RT> Unpin for Scope<RT>
where RT: Unpin,

§

impl<RT> UnwindSafe for Scope<RT>
where RT: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.