Skip to main content

Property

Struct Property 

Source
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>

Source

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.
Source

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");
Source

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}");
}
Source

pub fn has_subscribers(&self) -> bool

Whether any .watch() streams are alive.

Source

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<T: Clone + Clone + Send + Sync + 'static> Clone for Property<T>

Source§

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

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: Clone + Send + Sync + Debug + 'static> Debug for Property<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Clone + Send + Sync + Serialize + 'static> Serialize for Property<T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. 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> 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.