Skip to main content

BindingScope

Struct BindingScope 

Source
pub struct BindingScope { /* private fields */ }
Expand description

A scope that owns bindings and disposes them on drop.

BindingScope provides a convenient way to manage the lifetime of multiple bindings. When the scope is dropped, all owned bindings are disposed.

§Examples

let count = Signal::new(0);
let display = Rc::new(RefCell::new(String::new()));

{
    let mut scope = BindingScope::new();
    scope.bind(&count, {
        let display = display.clone();
        move |v: &i32| *display.borrow_mut() = format!("{v}")
    });
    count.set(5);
    assert_eq!(*display.borrow(), "5");
}
// Scope dropped — binding disposed.
count.set(99);
// display still shows "5" because the binding is gone.

Implementations§

Source§

impl BindingScope

Source

pub fn new() -> Self

Create a new empty binding scope.

Source

pub fn bind<T: Clone + 'static>( &mut self, source: &Signal<T>, sink: impl PropertySink<T> + 'static, ) -> BindingId

Create a one-way binding from source to sink.

Returns the binding ID.

Source

pub fn bind_two_way<T: Clone + 'static>( &mut self, source: &Signal<T>, sink: impl PropertySink<T> + 'static, ) -> (TwoWayBinding<T>, BindingId)

Create a two-way binding between source and sink.

Returns the TwoWayBinding (for calling write_back) and the binding ID.

Source

pub fn bind_expression<S: Clone + 'static, T: Clone + 'static>( &mut self, source: &Signal<S>, transform: impl Fn(&S) -> T + 'static, sink: impl PropertySink<T> + 'static, ) -> BindingId

Create a binding expression (source → transform → sink).

Returns the binding ID.

Source

pub fn binding_count(&self) -> usize

Get the number of bindings in this scope.

Source

pub fn is_binding_active(&self, id: BindingId) -> bool

Check if a specific binding is still active.

Trait Implementations§

Source§

impl Default for BindingScope

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for BindingScope

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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.