Crate dynprops

Source
Expand description

Creating and extending objects with typed dynamic properties.

§Example

use dynprops::{Subject, Dynamic};

let subject = Subject::new();
let prop_a = subject.new_prop_const_init(5);
let prop_b = subject.new_prop_const_init("Foo");
let mut obj = Dynamic::new(&subject);
assert_eq!(obj[&prop_a], 5);
assert_eq!(obj[&prop_b], "Foo");

// Properties can be changed on a mutable object
obj[&prop_b] = "Foobar";
assert_eq!(obj[&prop_b], "Foobar");

// New properties can be introduced after an object is already created
let prop_c = subject.new_prop_default_init::<u32>();
assert_eq!(obj[&prop_c], 0u32);

// Properties can be initialized based on a function of other properties on the object
let prop_d = subject.new_prop_fn_init(|obj| obj[&prop_b].len());
assert_eq!(obj[&prop_d], 6);

Structs§

ConstInit
An Init which initializes values by cloning a given value.
DefaultInit
An Init which initializes values using Default::default().
Extended
Extends a value of type T with properties defined in a particular Subject<T>.
FnInit
An Init which initializes values by executing a closure.
Property
Identifies a property that is present on objects of the appropriate Subject.
Subject
Identifies a category of objects and a dynamic set of Propertys that apply to those objects. New properties can be introduced into the subject at any time using Subject::new_prop and its derivatives. When accessing a property of an object, the subject of the property must match the subject of the object.

Traits§

Init
Defines how a Property is initialized when first accessed.

Type Aliases§

ConstInitProperty
A shortcut for a Property that is initialized by a ConstInit.
DefaultInitProperty
A shortcut for a Property that is initialized by a DefaultInit.
DynInit
An Init that uses dynamic dispatch to defer to another Init at runtime.
DynInitProperty
A shortcut for a Property that is initialized by a DynInit. Any property can be converted into a DynInitProperty using Property::into_dyn_init.
Dynamic
An object consisting entirely of dynamic properties defined in a particular Subject.
FnInitProperty
A shortcut for a Property that is initialized by a FnInit.