pub struct PropertyCollection { /* private fields */ }Expand description
Dynamic collection of properties.
Implementations§
Source§impl PropertyCollection
impl PropertyCollection
pub fn new() -> Self
Sourcepub fn assign<T: Any>(&mut self, value: T)
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 theAny
§Parameters
value: The value of typeTto assign to the property.
Sourcepub fn contains<T: Any>(&self) -> bool
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 typeTexists in the collection.false- If no property of the specified typeTis found in the collection.
Sourcepub fn subscribe<T: Any>(&mut self, callback: impl FnMut(&T) + 'static)
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));
}