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,
impl<T> WriteableReactiveValue<T>where
T: 'static,
Sourcepub fn set(&self, value: T)
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}Sourcepub fn set_rc(&self, value: Arc<T>)
pub fn set_rc(&self, value: Arc<T>)
Sets the value of the ReactiveValue, using a mutex to ensure thread safety.
Sourcepub fn as_readonly(&self) -> ReadonlyReactiveValue<T>
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>
impl<T> Clone for WriteableReactiveValue<T>
Source§fn clone(&self) -> WriteableReactiveValue<T>
fn clone(&self) -> WriteableReactiveValue<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T> ReactiveValue<T> for WriteableReactiveValue<T>
impl<T> ReactiveValue<T> for WriteableReactiveValue<T>
Auto Trait Implementations§
impl<T> Freeze for WriteableReactiveValue<T>
impl<T> RefUnwindSafe for WriteableReactiveValue<T>
impl<T> !Send for WriteableReactiveValue<T>
impl<T> !Sync for WriteableReactiveValue<T>
impl<T> Unpin for WriteableReactiveValue<T>
impl<T> UnwindSafe for WriteableReactiveValue<T>
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