PropertyCollection

Struct PropertyCollection 

Source
pub struct PropertyCollection { /* private fields */ }
Expand description

Dynamic collection of properties.

Implementations§

Source§

impl PropertyCollection

Source

pub fn new() -> Self

Source

pub fn get<T: Any>(&self) -> Option<&T>

Retrieves the value of a property of a specific type from the PropertyCollection.

§Type Parameters
  • T: The type of the property to retrieve. It should be a type that implements the Any
§Returns
  • Some(&T) if the property of type T exists.
  • None if the property is not found.
Source

pub fn assign<T: Any>(&mut self, value: T)

Assigns the value to a property or adds a new property with the specified value.

This function assigns the provided value to an existing property of the specified type T if it already exists in the collection. If no property of that type exists, a new property of that type is added to the collection with the specified value.

§Type Parameters
  • T: The type of the property to assign. It should be a type that implements the Any
§Parameters
  • value: The value of type T to assign to the property.
Source

pub fn contains<T: Any>(&self) -> bool

Checks if a property of a specified type exists in the collection.

§Returns
  • true - If a property of the specified type T exists in the collection.
  • false - If no property of the specified type T is found in the collection.
Source

pub fn subscribe<T: Any>(&mut self, callback: impl FnMut(&T) + 'static)

Subscribes to changes in a property of a specific type in the PropertyCollection.

This function attempts to subscribe to changes in a property of the specified type T in the PropertyCollection. If the property exists and its type matches T, it registers the provided callback function to be called when the property’s value changes. If the property does not exist in the collection yet, an early subscription is performed by adding the callback to ab empty property of type T. This allows the callback to be called when the property is assigned a value later.

§Arguments
  • callback - The callback function to be called when the property’s value changes.
§Example
use dynp::PropertyCollection;

// define a custom property using the Newtype pattern
#[derive(Copy, Clone, Debug)]
struct CustomProperty(i32);

fn main() {
    // create a new property collection
    let mut collection = PropertyCollection::new();
    collection.subscribe::<CustomProperty>(|value: &CustomProperty| {
        println!("Property changed: {:?}", value);
    });

    // assign a new property
    collection.assign(CustomProperty(42));
}

Trait Implementations§

Source§

impl Debug for PropertyCollection

Source§

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

Formats the value using the given formatter. Read more

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