WriteableReactiveValue

Struct WriteableReactiveValue 

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

Reactive value that is explicitly set, rather than being derived from a stream.

§Examples

use epoxy_streams::ReactiveValue;

let writeable_value = ReactiveValue::new(5);
assert_eq!(*writeable_value.get(), 5);

writeable_value.set(50);
assert_eq!(*writeable_value.get(), 50);

Implementations§

Source§

impl<T> WriteableReactiveValue<T>
where T: 'static,

Source

pub fn set(&self, value: T)

Sets the value of the ReactiveValue, using a mutex to ensure thread safety.

Note: use set_rc if your new value is already an Arc, as this will prevent the value from being unnecessarily copied.

Examples found in repository?
examples/reactive_scores.rs (line 30)
6fn main() {
7    let player_1_points = epoxy::ReactiveValue::new(4);
8    let player_1_multiplier = epoxy::ReactiveValue::new(1.0_f32);
9
10    let player_2_points = epoxy::ReactiveValue::new(5);
11    let player_2_multiplier = epoxy::ReactiveValue::new(1.0_f32);
12
13    let player_1_score = computed!(*player_1_points as f32 * player_1_multiplier);
14    let player_2_score = computed!(*player_2_points as f32 * player_2_multiplier);
15
16    let winner = computed! {
17        if (player_1_score == player_2_score) {
18            "Tie"
19        } else if (player_1_score > player_2_score) {
20            "Player 1"
21        } else {
22            "Player 2"
23        }
24    };
25
26    assert_eq!(*player_1_score.get(), 4_f32);
27    assert_eq!(*player_2_score.get(), 5_f32);
28    assert_eq!(*winner.get(), "Player 2");
29
30    player_1_multiplier.set(2.5_f32);
31
32    assert_eq!(*player_1_score.get(), 10_f32);
33    assert_eq!(*player_2_score.get(), 5_f32);
34    assert_eq!(*winner.get(), "Player 1");
35
36    player_2_points.set(9);
37
38    assert_eq!(*player_1_score.get(), 10_f32);
39    assert_eq!(*player_2_score.get(), 9_f32);
40    assert_eq!(*winner.get(), "Player 1");
41
42    player_2_points.set(10);
43
44    assert_eq!(*player_1_score.get(), 10_f32);
45    assert_eq!(*player_2_score.get(), 10_f32);
46    assert_eq!(*winner.get(), "Tie");
47}
Source

pub fn set_rc(&self, value: Arc<T>)

Sets the value of the ReactiveValue, using a mutex to ensure thread safety.

Source

pub fn as_readonly(&self) -> ReadonlyReactiveValue<T>

Returns a ReadonlyReactiveValue whose value matches this one. This is helpful when exposing ReactiveValues to public APIs, so that the consumer cannot alter the state of your component.

Trait Implementations§

Source§

impl<T> Clone for WriteableReactiveValue<T>

Source§

fn clone(&self) -> WriteableReactiveValue<T>

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

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

Performs copy-assignment from source. Read more
Source§

impl<T> ReactiveValue<T> for WriteableReactiveValue<T>

Source§

fn as_stream(&self) -> Stream<T>

Returns a Stream that represents the changing value over time. Use this function to subscribe to changes in the ReactiveValue.ReadonlyReactiveValue Read more
Source§

fn get(&self) -> Arc<T>

Returns the current value of the ReactiveValue.

Auto Trait Implementations§

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.