pub struct Property<T: Clone + Send + Sync + 'static> { /* private fields */ }Expand description
A value you can .get() or .watch() for changes.
ⓘ
let volume = device.volume.get();
let mut changes = device.volume.watch();
while let Some(vol) = changes.next().await {
println!("{vol:?}");
}Implementations§
Source§impl<T: Clone + Send + Sync + 'static> Property<T>
impl<T: Clone + Send + Sync + 'static> Property<T>
Sourcepub fn replace(&self, new_value: T)
pub fn replace(&self, new_value: T)
Unconditional set. Always notifies watchers, and
doesn’t require PartialEq.
use wayle_core::Property;
let data = Property::new(vec![1, 2, 3]);
data.replace(vec![1, 2, 3]);
// Watchers fire even though the Vec is equal.
// Use `set` instead if you want to skip duplicates.Sourcepub fn get(&self) -> T
pub fn get(&self) -> T
Snapshot of the current value (cloned).
use wayle_core::Property;
let name = Property::new(String::from("default"));
assert_eq!(name.get(), "default");Sourcepub fn watch(&self) -> impl Stream<Item = T> + Send + 'static
pub fn watch(&self) -> impl Stream<Item = T> + Send + 'static
Yields the current value immediately, then each subsequent change.
Each call returns an independent stream. Multiple consumers can watch the same property concurrently.
use futures::stream::StreamExt;
use wayle_core::Property;
let score = Property::new(0_u32);
let mut stream = score.watch();
while let Some(points) = stream.next().await {
println!("score: {points}");
}Sourcepub fn has_subscribers(&self) -> bool
pub fn has_subscribers(&self) -> bool
Whether any .watch() streams are alive.
Sourcepub async fn wait_for_subscribers(&self)
pub async fn wait_for_subscribers(&self)
Suspends until at least one consumer calls .watch().
Pair with has_subscribers in a loop to
pause expensive work whenever nobody is listening, and resume
when someone subscribes again.
use wayle_core::Property;
let cpu_usage = Property::new(0.0_f64);
loop {
if !cpu_usage.has_subscribers() {
cpu_usage.wait_for_subscribers().await;
}
let usage = 42.0; // poll_cpu();
cpu_usage.set(usage);
}Trait Implementations§
Source§impl<'de, T: Clone + Send + Sync + Deserialize<'de> + 'static> Deserialize<'de> for Property<T>
impl<'de, T: Clone + Send + Sync + Deserialize<'de> + 'static> Deserialize<'de> for Property<T>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Property<T>
impl<T> !UnwindSafe for Property<T>
impl<T> Freeze for Property<T>
impl<T> Send for Property<T>
impl<T> Sync for Property<T>
impl<T> Unpin for Property<T>
impl<T> UnsafeUnpin for Property<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