Skip to main content

Var

Struct Var 

Source
pub struct Var<T> { /* private fields */ }
Expand description

A writable source of signal state.

Implementations§

Source§

impl<T> Var<T>
where T: 'static + PartialEq,

Source

pub fn new(value: T) -> Self

Create a new Var holding value.

Examples found in repository?
examples/validation.rs (line 13)
11fn main() {
12    // Counter state.
13    let count = Var::new(0);
14
15    // Derived state: a doubled value and a validation message.
16    let doubled = count.signal().map(|c| c * 2);
17    let validation = count.signal().map(|c| {
18        if *c < 0 {
19            "negative"
20        } else if *c > 10 {
21            "too large"
22        } else {
23            "ok"
24        }
25    });
26
27    // Event sampling: what was the count when a "click" happened?
28    let clicks = Sink::new();
29    let clicked_value = clicks.stream().sample(&count.signal());
30
31    let log: Rc<RefCell<Vec<(i32, &str)>>> = Rc::new(RefCell::new(Vec::new()));
32    let log_rc = log.clone();
33    let count_rc = count.clone();
34    let validation_rc = validation.clone();
35    let _count_sub = count_rc.signal().observe(move |value| {
36        log_rc.borrow_mut().push((*value, *validation_rc.value()));
37    });
38    let log_rc = log.clone();
39    let _clicked_sub = clicked_value.observe(move |value| {
40        log_rc.borrow_mut().push((*value, "sampled"));
41    });
42
43    count.set(5);
44    clicks.send(());
45    count.set(12);
46
47    assert_eq!(*doubled.value(), 24);
48    assert_eq!(*validation.value(), "too large");
49    assert_eq!(
50        *log.borrow(),
51        vec![(0, "ok"), (5, "ok"), (5, "sampled"), (12, "too large")]
52    );
53    println!(
54        "counter={} doubled={} validation={}",
55        count.value(),
56        doubled.value(),
57        validation.value()
58    );
59}
Source

pub fn set(&self, value: T)

Write value, propagating through the graph inside a transaction.

Examples found in repository?
examples/validation.rs (line 43)
11fn main() {
12    // Counter state.
13    let count = Var::new(0);
14
15    // Derived state: a doubled value and a validation message.
16    let doubled = count.signal().map(|c| c * 2);
17    let validation = count.signal().map(|c| {
18        if *c < 0 {
19            "negative"
20        } else if *c > 10 {
21            "too large"
22        } else {
23            "ok"
24        }
25    });
26
27    // Event sampling: what was the count when a "click" happened?
28    let clicks = Sink::new();
29    let clicked_value = clicks.stream().sample(&count.signal());
30
31    let log: Rc<RefCell<Vec<(i32, &str)>>> = Rc::new(RefCell::new(Vec::new()));
32    let log_rc = log.clone();
33    let count_rc = count.clone();
34    let validation_rc = validation.clone();
35    let _count_sub = count_rc.signal().observe(move |value| {
36        log_rc.borrow_mut().push((*value, *validation_rc.value()));
37    });
38    let log_rc = log.clone();
39    let _clicked_sub = clicked_value.observe(move |value| {
40        log_rc.borrow_mut().push((*value, "sampled"));
41    });
42
43    count.set(5);
44    clicks.send(());
45    count.set(12);
46
47    assert_eq!(*doubled.value(), 24);
48    assert_eq!(*validation.value(), "too large");
49    assert_eq!(
50        *log.borrow(),
51        vec![(0, "ok"), (5, "ok"), (5, "sampled"), (12, "too large")]
52    );
53    println!(
54        "counter={} doubled={} validation={}",
55        count.value(),
56        doubled.value(),
57        validation.value()
58    );
59}
Source

pub fn value(&self) -> Rc<T>

The current value.

Examples found in repository?
examples/validation.rs (line 55)
11fn main() {
12    // Counter state.
13    let count = Var::new(0);
14
15    // Derived state: a doubled value and a validation message.
16    let doubled = count.signal().map(|c| c * 2);
17    let validation = count.signal().map(|c| {
18        if *c < 0 {
19            "negative"
20        } else if *c > 10 {
21            "too large"
22        } else {
23            "ok"
24        }
25    });
26
27    // Event sampling: what was the count when a "click" happened?
28    let clicks = Sink::new();
29    let clicked_value = clicks.stream().sample(&count.signal());
30
31    let log: Rc<RefCell<Vec<(i32, &str)>>> = Rc::new(RefCell::new(Vec::new()));
32    let log_rc = log.clone();
33    let count_rc = count.clone();
34    let validation_rc = validation.clone();
35    let _count_sub = count_rc.signal().observe(move |value| {
36        log_rc.borrow_mut().push((*value, *validation_rc.value()));
37    });
38    let log_rc = log.clone();
39    let _clicked_sub = clicked_value.observe(move |value| {
40        log_rc.borrow_mut().push((*value, "sampled"));
41    });
42
43    count.set(5);
44    clicks.send(());
45    count.set(12);
46
47    assert_eq!(*doubled.value(), 24);
48    assert_eq!(*validation.value(), "too large");
49    assert_eq!(
50        *log.borrow(),
51        vec![(0, "ok"), (5, "ok"), (5, "sampled"), (12, "too large")]
52    );
53    println!(
54        "counter={} doubled={} validation={}",
55        count.value(),
56        doubled.value(),
57        validation.value()
58    );
59}
Source

pub fn signal(&self) -> Signal<T>

This Var as a Signal.

Examples found in repository?
examples/validation.rs (line 16)
11fn main() {
12    // Counter state.
13    let count = Var::new(0);
14
15    // Derived state: a doubled value and a validation message.
16    let doubled = count.signal().map(|c| c * 2);
17    let validation = count.signal().map(|c| {
18        if *c < 0 {
19            "negative"
20        } else if *c > 10 {
21            "too large"
22        } else {
23            "ok"
24        }
25    });
26
27    // Event sampling: what was the count when a "click" happened?
28    let clicks = Sink::new();
29    let clicked_value = clicks.stream().sample(&count.signal());
30
31    let log: Rc<RefCell<Vec<(i32, &str)>>> = Rc::new(RefCell::new(Vec::new()));
32    let log_rc = log.clone();
33    let count_rc = count.clone();
34    let validation_rc = validation.clone();
35    let _count_sub = count_rc.signal().observe(move |value| {
36        log_rc.borrow_mut().push((*value, *validation_rc.value()));
37    });
38    let log_rc = log.clone();
39    let _clicked_sub = clicked_value.observe(move |value| {
40        log_rc.borrow_mut().push((*value, "sampled"));
41    });
42
43    count.set(5);
44    clicks.send(());
45    count.set(12);
46
47    assert_eq!(*doubled.value(), 24);
48    assert_eq!(*validation.value(), "too large");
49    assert_eq!(
50        *log.borrow(),
51        vec![(0, "ok"), (5, "ok"), (5, "sampled"), (12, "too large")]
52    );
53    println!(
54        "counter={} doubled={} validation={}",
55        count.value(),
56        doubled.value(),
57        validation.value()
58    );
59}

Trait Implementations§

Source§

impl<T> Clone for Var<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Default + 'static + PartialEq> Default for Var<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Var<T>

§

impl<T> !Send for Var<T>

§

impl<T> !Sync for Var<T>

§

impl<T> !UnwindSafe for Var<T>

§

impl<T> Freeze for Var<T>
where Rc<SignalSourceNode<T>>: Freeze,

§

impl<T> Unpin for Var<T>
where Rc<SignalSourceNode<T>>: Unpin,

§

impl<T> UnsafeUnpin for Var<T>
where Rc<SignalSourceNode<T>>: UnsafeUnpin,

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.