Expand description
A simple, zero-dependency property-binding framework. It was originally designed to use imgui-rs without drowning in mutable references to everything and constantly fighting with the borrow checker.
§Usage
bind() can be called on an immutable &Property
to get a mutable binding.
The PropertyBinding returned by bind()
Derefs to &T
and
DerefMuts to &mut T
. The binding needs to be referenced mutably for
DerefMut, so rust’s borrow checker enforces exclusive mutable access
XOR muliple immutable access to the binding itself. The binding unbinds itself when
Dropped, so it is automatically freed when it exits scope.
§Example
pub struct PropHaver {
pub prop: binder::Property<f32>
}
fn use_prop(p: &PropHaver, ui: &imgui::Ui) {
ui.slider("wow what a cool slider", &mut p.prop.bind());
}
§Safety
Property
owns its value and maintains its own invariants over that value. Properties cannot
be bound more than once at the same time. The thread-safe AtomicBool
is used to synchronize access to the binding, so it should be fully thread-safe as well.
Properties CANNOT be cloned to get more references to the same value. You can use
Rc<Property>
or Arc<Property>
for that.
§Panic
bind() will panic if called on a Property
that’s already been bound
elsewhere. Use try_bind() for a non-panicking version.
Structs§
- Property
- Used to define a bindable property. See crate-level documentation for more details.
- Property
Binding - A binding to a Property. Allows mutable and immutable access to the value via dereferencing.
Exclusive access is enforced by the borrow checker like any other mutable object.
PropertyBinding
automatically unbinds itself when Dropped, and allowing the binding to leave scope and be Dropped is the preferred usage.