Skip to main content

TwoWayBinding

Struct TwoWayBinding 

Source
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>

Source

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.

Source

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>

Source§

fn id(&self) -> BindingId

Unique identifier for this binding.
Source§

fn direction(&self) -> BindingDirection

Direction of data flow.
Source§

fn is_active(&self) -> bool

Whether this binding is still active.
Source§

fn dispose(&self)

Permanently deactivate this binding.

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