pub struct TwoWayBinding<T: Clone + 'static> { /* private fields */ }Expand description
A two-way binding between a signal and a property sink.
The forward direction works like OneWayBinding: signal changes
are pushed to the sink. The reverse direction is handled by calling
TwoWayBinding::write_back, which updates the signal.
An internal loop guard prevents infinite ping-pong when a write-back triggers a forward push that would trigger another write-back.
§Examples
ⓘ
let model = Signal::new(String::from("hello"));
let display = Rc::new(RefCell::new(String::new()));
let binding = TwoWayBinding::new(&model, {
let display = display.clone();
move |v: &String| *display.borrow_mut() = v.clone()
});
// Forward: model → display
model.set("world".into());
assert_eq!(*display.borrow(), "world");
// Reverse: display → model
binding.write_back("reverse".into());
assert_eq!(model.get(), "reverse");Implementations§
Source§impl<T: Clone + 'static> TwoWayBinding<T>
impl<T: Clone + 'static> TwoWayBinding<T>
Sourcepub fn new(source: &Signal<T>, sink: impl PropertySink<T> + 'static) -> Self
pub fn new(source: &Signal<T>, sink: impl PropertySink<T> + 'static) -> Self
Create a new two-way binding between source and sink.
Forward direction runs immediately and on every source change.
Sourcepub fn write_back(&self, value: T)
pub fn write_back(&self, value: T)
Write a value back from the sink to the source signal.
Uses the loop guard to prevent the forward effect from re-pushing the same value to the sink.
Trait Implementations§
Source§impl<T: Clone + 'static> Binding for TwoWayBinding<T>
impl<T: Clone + 'static> Binding for TwoWayBinding<T>
Auto Trait Implementations§
impl<T> Freeze for TwoWayBinding<T>
impl<T> !RefUnwindSafe for TwoWayBinding<T>
impl<T> !Send for TwoWayBinding<T>
impl<T> !Sync for TwoWayBinding<T>
impl<T> Unpin for TwoWayBinding<T>
impl<T> !UnwindSafe for TwoWayBinding<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more