pub struct BindingScope { /* private fields */ }Expand description
Collects subscriptions and bindings for a logical scope (e.g., a widget).
When the scope is dropped, all held subscriptions are released, cleanly disconnecting all reactive bindings associated with that scope.
§Usage
ⓘ
let mut scope = BindingScope::new();
let obs = Observable::new(42);
scope.subscribe(&obs, |v| println!("value: {v}"));
scope.bind(&obs, |v| format!("display: {v}"));
// When scope drops, all subscriptions are released.§Invariants
- Subscriptions are released in reverse registration order on drop.
- After drop, no callbacks from this scope will fire.
clear()releases all subscriptions immediately (reusable scope).- Binding count is always accurate.
Implementations§
Source§impl BindingScope
impl BindingScope
Sourcepub fn hold(&mut self, sub: Subscription)
pub fn hold(&mut self, sub: Subscription)
Add a subscription to this scope. The subscription will be held alive
until the scope is dropped or clear() is called.
Sourcepub fn subscribe<T: Clone + PartialEq + 'static>(
&mut self,
source: &Observable<T>,
callback: impl Fn(&T) + 'static,
) -> &mut Self
pub fn subscribe<T: Clone + PartialEq + 'static>( &mut self, source: &Observable<T>, callback: impl Fn(&T) + 'static, ) -> &mut Self
Subscribe to an observable within this scope.
Returns a reference to the scope for chaining.
Sourcepub fn bind<T: Clone + PartialEq + 'static>(
&mut self,
source: &Observable<T>,
) -> Binding<T>
pub fn bind<T: Clone + PartialEq + 'static>( &mut self, source: &Observable<T>, ) -> Binding<T>
Create a one-way binding within this scope.
The binding’s underlying subscription is held by the scope.
Returns the Binding<T> for reading the value.
Sourcepub fn bind_map<S: Clone + PartialEq + 'static, T: 'static>(
&mut self,
source: &Observable<S>,
map: impl Fn(&S) -> T + 'static,
) -> Binding<T>
pub fn bind_map<S: Clone + PartialEq + 'static, T: 'static>( &mut self, source: &Observable<S>, map: impl Fn(&S) -> T + 'static, ) -> Binding<T>
Create a mapped binding within this scope.
Sourcepub fn binding_count(&self) -> usize
pub fn binding_count(&self) -> usize
Number of active subscriptions/bindings in this scope.
Trait Implementations§
Source§impl Debug for BindingScope
impl Debug for BindingScope
Auto Trait Implementations§
impl Freeze for BindingScope
impl !RefUnwindSafe for BindingScope
impl !Send for BindingScope
impl !Sync for BindingScope
impl Unpin for BindingScope
impl !UnwindSafe for BindingScope
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