pub struct Binding<T>(/* private fields */)
where
T: 'static;Expand description
A Binding<T> represents a mutable value of type T that can be observed.
Bindings provide a reactive way to work with values. When a binding’s value changes, it can notify watchers that have registered interest in the value.
Implementations§
Source§impl<T> Binding<T>where
T: 'static,
impl<T> Binding<T>where
T: 'static,
Sourcepub fn custom(
custom: impl CustomBinding<Output = T> + Clone + 'static,
) -> Binding<T>
pub fn custom( custom: impl CustomBinding<Output = T> + Clone + 'static, ) -> Binding<T>
Creates a binding that uses a custom implementation of the CustomBinding trait.
Sourcepub fn get_mut(&self) -> BindingMutGuard<'_, T>
pub fn get_mut(&self) -> BindingMutGuard<'_, T>
Gets mutable access to the binding’s value through a guard.
When the guard is dropped, the binding is updated with the modified value.
Note: In rust,
let _ = binding.get_mut();DO NOT immediately drop the guard. Since it just binds to a variable named_, the guard will live until the end of the current scope. Please use*guardto modify the value in one line.
§Example
use nami::Binding;
let n = Binding::int(10);
*n.get_mut() += 5; // do not bind the guard to a let pattern...even it is `_`Tip: For better performance when modifying container bindings, consider using the with_mut method instead.
Sourcepub fn take(&self) -> T
pub fn take(&self) -> T
Takes the value out of the binding, replacing it with the default value.
This is equivalent to std::mem::take and notifies watchers of the change.
§Example
use nami::{binding, Binding};
let mut text: Binding<String> = binding("hello");
let taken = text.take();
assert_eq!(taken, "hello");
assert_eq!(text.get(), String::new());Sourcepub fn set_from(&self, value: impl Into<T>)
pub fn set_from(&self, value: impl Into<T>)
Sets the binding to a new value with automatic type conversion.
Accepts any value that implements Into<T>, providing the same ergonomic
benefits as the binding() constructor.
§Examples
use nami::{binding, Binding};
let mut text: Binding<String> = binding("initial");
// Direct &str usage - no .into() or .to_string() needed
text.set_from("updated");
assert_eq!(text.get(), "updated");
let mut count: Binding<i64> = binding(0);
count.set(42);
assert_eq!(count.get(), 42i64);Sourcepub fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> Rwhere
T: Clone,
pub fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> Rwhere
T: Clone,
Applies a function to mutably borrow the binding’s value.
This is more efficient than get_mut() for container bindings as it avoids
unnecessary cloning. The function receives a mutable reference to the value
and any changes will notify watchers when the function completes.
Sourcepub fn mapping<Output, Getter, Setter>(
source: &Binding<T>,
getter: Getter,
setter: Setter,
) -> Binding<Output>
pub fn mapping<Output, Getter, Setter>( source: &Binding<T>, getter: Getter, setter: Setter, ) -> Binding<Output>
Creates a bidirectional mapping between this binding and another type.
The getter transforms values from this binding’s type to the output type. The setter transforms values from the output type back to this binding’s type.
Sourcepub fn filter(
&self,
filter: impl Clone + Fn(&T) -> bool + 'static,
) -> Binding<T>where
T: 'static,
pub fn filter(
&self,
filter: impl Clone + Fn(&T) -> bool + 'static,
) -> Binding<T>where
T: 'static,
Creates a binding that only allows values passing a filter function.
When attempting to set a value that doesn’t pass the filter, the operation is ignored.
Sourcepub fn condition(
&self,
condition: impl Clone + Fn(&T) -> bool + 'static,
) -> Binding<bool>where
T: 'static,
pub fn condition(
&self,
condition: impl Clone + Fn(&T) -> bool + 'static,
) -> Binding<bool>where
T: 'static,
Creates a binding that maps this binding’s value to a boolean condition.
The resulting binding is read-only and reflects whether the condition is met.
§Example
let number = nami::binding(5i32);
let is_positive = number.condition(|&n: &i32| n > 0);
assert_eq!(is_positive.get(), true);Sourcepub fn equal_to(&self, other: T) -> Binding<bool>
pub fn equal_to(&self, other: T) -> Binding<bool>
Creates a binding that tracks whether this binding’s value equals a specific value.
The resulting binding is read-only.
§Example
let text = nami::binding("hello".to_string());
let is_hello = text.equal_to("hello".to_string());
assert_eq!(is_hello.get(), true);Source§impl<T> Binding<T>where
T: 'static,
impl<T> Binding<T>where
T: 'static,
Sourcepub fn mailbox_with_executor<E>(&self, executor: E) -> BindingMailbox<T>where
E: LocalExecutor,
pub fn mailbox_with_executor<E>(&self, executor: E) -> BindingMailbox<T>where
E: LocalExecutor,
Attaches this Binding to a mailbox using a provided executor.
Returns a BindingMailbox which can be cloned and used to send the
binding to other tasks for mutation or observation.
Sourcepub fn mailbox(&self) -> BindingMailbox<T>
pub fn mailbox(&self) -> BindingMailbox<T>
Attaches this Binding to a mailbox using the default native executor.
Source§impl<T> Binding<T>where
T: PartialOrd + 'static,
impl<T> Binding<T>where
T: PartialOrd + 'static,
Sourcepub fn range(&self, range: impl RangeBounds<T> + Clone + 'static) -> Binding<T>
pub fn range(&self, range: impl RangeBounds<T> + Clone + 'static) -> Binding<T>
Creates a binding that only allows values within a specified range.
Source§impl<T> Binding<T>where
T: Signed,
impl<T> Binding<T>where
T: Signed,
Sourcepub fn sign(&self) -> Binding<bool>
pub fn sign(&self) -> Binding<bool>
Creates a binding that tracks the sign of this binding’s value.
The resulting binding is true for positive values and false for negative values.
Setting true makes the value positive, setting false makes it negative.
Tip: Zero is considered positive.
§Example
use nami::Binding;
let number = Binding::int(-10i32);
let sign = number.sign();
assert_eq!(sign.get(), false);Source§impl<T> Binding<T>where
T: Clone,
impl<T> Binding<T>where
T: Clone,
Sourcepub fn append<Ele>(&self, ele: Ele)where
T: Extend<Ele>,
pub fn append<Ele>(&self, ele: Ele)where
T: Extend<Ele>,
Appends an element to the binding’s value and notifies watchers.
The binding’s value must implement Extend for the element type.
§Example
let mut text: nami::Binding<String> = nami::binding(String::from("Hello"));
text.append(" World");
assert_eq!(text.get(), "Hello World");Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn add_assign(&self, other: T)
pub fn add_assign(&self, other: T)
Applies the + operation to the binding’s current value and the provided value.
Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn sub_assign(&self, other: T)
pub fn sub_assign(&self, other: T)
Applies the - operation to the binding’s current value and the provided value.
Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn mul_assign(&self, other: T)
pub fn mul_assign(&self, other: T)
Applies the * operation to the binding’s current value and the provided value.
Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn div_assign(&self, other: T)
pub fn div_assign(&self, other: T)
Applies the / operation to the binding’s current value and the provided value.
Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn rem_assign(&self, other: T)
pub fn rem_assign(&self, other: T)
Applies the % operation to the binding’s current value and the provided value.
Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn bitand_assign(&self, other: T)
pub fn bitand_assign(&self, other: T)
Applies the & operation to the binding’s current value and the provided value.
Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn bitor_assign(&self, other: T)
pub fn bitor_assign(&self, other: T)
Applies the | operation to the binding’s current value and the provided value.
Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn bitxor_assign(&self, other: T)
pub fn bitxor_assign(&self, other: T)
Applies the ^ operation to the binding’s current value and the provided value.
Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn shl_assign(&self, other: T)
pub fn shl_assign(&self, other: T)
Applies the << operation to the binding’s current value and the provided value.
Source§impl<T> Binding<T>
impl<T> Binding<T>
Sourcepub fn shr_assign(&self, other: T)
pub fn shr_assign(&self, other: T)
Applies the >> operation to the binding’s current value and the provided value.
Source§impl<T> Binding<Option<T>>
impl<T> Binding<Option<T>>
Sourcepub fn unwrap_or_else(
&self,
default: impl Clone + Fn() -> T + 'static,
) -> Binding<T>where
T: Clone + 'static,
pub fn unwrap_or_else(
&self,
default: impl Clone + Fn() -> T + 'static,
) -> Binding<T>where
T: Clone + 'static,
Creates a binding that unwraps the option or uses a default value from a closure.
When setting values on the returned binding, they are wrapped in Some.
§Example
let maybe_text = nami::binding(None::<String>);
let text = maybe_text.unwrap_or_else(|| "default".to_string());
assert_eq!(text.get(), "default");Sourcepub fn unwrap_or(&self, default: T) -> Binding<T>where
T: Clone + 'static,
pub fn unwrap_or(&self, default: T) -> Binding<T>where
T: Clone + 'static,
Creates a binding that unwraps the option or uses a default value.
When setting values on the returned binding, they are wrapped in Some.
§Example
let maybe_number = nami::binding(None::<i32>);
let number = maybe_number.unwrap_or(42);
assert_eq!(number.get(), 42);Sourcepub fn unwrap_or_default(&self) -> Binding<T>
pub fn unwrap_or_default(&self) -> Binding<T>
Creates a binding that unwraps the option or uses the type’s default value.
When setting values on the returned binding, they are wrapped in Some.
§Example
let maybe_vec = nami::binding(None::<Vec<i32>>);
let vec: nami::Binding<Vec<i32>> = maybe_vec.unwrap_or_default();
assert!(vec.get().is_empty());Sourcepub fn some_equal_to(&self, equal: T) -> Binding<bool>
pub fn some_equal_to(&self, equal: T) -> Binding<bool>
Creates a binding that tracks whether this option contains a specific value.
The resulting binding is true when this option contains Some(equal),
and false when it contains Some(other_value) or None.
Setting true on the result sets this binding to Some(equal).
Setting false has no effect on the binding.
§Example
let maybe_text = nami::binding(Some("hello".to_string()));
let is_hello = maybe_text.some_equal_to("hello".to_string());
assert_eq!(is_hello.get(), true);Source§impl Binding<bool>
impl Binding<bool>
Sourcepub fn bool(value: bool) -> Binding<bool>
pub fn bool(value: bool) -> Binding<bool>
Creates a new boolean binding with the given value.
§Example
let flag = nami::Binding::bool(true);
assert_eq!(flag.get(), true);Sourcepub fn toggle(&self)
pub fn toggle(&self)
Toggles the boolean value and notifies watchers.
True becomes false, false becomes true.
§Example
let mut flag = nami::binding(false);
flag.toggle();
assert_eq!(flag.get(), true);Sourcepub fn then<T>(&self, if_true: T) -> Binding<Option<T>>where
T: Clone + 'static,
pub fn then<T>(&self, if_true: T) -> Binding<Option<T>>where
T: Clone + 'static,
Creates a conditional binding that returns Some(value) when true, None when false.
Setting Some(value) on the result sets this binding to true.
Setting None sets this binding to false.
§Example
let is_logged_in = nami::binding(true);
let username = is_logged_in.then("alice".to_string());
assert_eq!(username.get(), Some("alice".to_string()));Sourcepub fn then_some<T>(&self, if_true: T) -> Binding<Option<T>>where
T: Clone + 'static,
pub fn then_some<T>(&self, if_true: T) -> Binding<Option<T>>where
T: Clone + 'static,
Creates a conditional binding that returns Some(value) when true, None when false.
This is identical to then() but follows Rust’s Option::then_some() naming convention.
§Example
let enabled = nami::binding(false);
let button_text = enabled.then_some("Click me!".to_string());
assert_eq!(button_text.get(), None);Sourcepub fn select<T>(&self, if_true: T, if_false: T) -> Binding<T>
pub fn select<T>(&self, if_true: T, if_false: T) -> Binding<T>
Creates a binding that selects between two values based on this boolean.
Returns if_true when this binding is true, if_false when false.
Setting the if_true value on the result sets this binding to true.
Setting the if_false value sets this binding to false.
§Example
let dark_mode = nami::binding(false);
let theme = dark_mode.select("dark".to_string(), "light".to_string());
assert_eq!(theme.get(), "light");Sourcepub fn reverse(&self) -> Binding<bool>
pub fn reverse(&self) -> Binding<bool>
Creates a binding that returns the logical inverse of this boolean binding.
When this binding is true, the returned binding is false, and vice versa.
Setting a value on the returned binding will set the inverse value on this binding.
§Example
let enabled = nami::binding(true);
let disabled = enabled.reverse();
assert_eq!(disabled.get(), false);Source§impl<T> Binding<T>where
T: Project,
impl<T> Binding<T>where
T: Project,
Sourcepub fn project(&self) -> <T as Project>::Projected
pub fn project(&self) -> <T as Project>::Projected
Projects this binding into its component parts.
This method uses the Project trait implementation to decompose
the binding into separate reactive bindings for each component.
Changes to any projected binding will be reflected in the original
binding and vice versa.
§Examples
use nami::{Binding, binding};
let tuple_binding: Binding<(i32, i32, i32)> = binding((1, 2, 3));
let (mut a, mut b, c) = tuple_binding.project();
// Modify individual projections
a.set(10);
b.set(20);
// Original binding reflects changes
assert_eq!(tuple_binding.get(), (10, 20, 3));Trait Implementations§
Source§impl<T, RHS> Add<RHS> for Binding<T>
impl<T, RHS> Add<RHS> for Binding<T>
Source§impl<T, RHS> BitAnd<RHS> for Binding<T>
impl<T, RHS> BitAnd<RHS> for Binding<T>
Source§impl<T, RHS> BitOr<RHS> for Binding<T>
impl<T, RHS> BitOr<RHS> for Binding<T>
Source§impl<T, RHS> BitXor<RHS> for Binding<T>
impl<T, RHS> BitXor<RHS> for Binding<T>
Source§impl<T, RHS> Div<RHS> for Binding<T>
impl<T, RHS> Div<RHS> for Binding<T>
Source§impl<T, RHS> Mul<RHS> for Binding<T>
impl<T, RHS> Mul<RHS> for Binding<T>
Source§impl<T, RHS> Rem<RHS> for Binding<T>
impl<T, RHS> Rem<RHS> for Binding<T>
Source§impl<T, RHS> Shl<RHS> for Binding<T>
impl<T, RHS> Shl<RHS> for Binding<T>
Source§impl<T, RHS> Shr<RHS> for Binding<T>
impl<T, RHS> Shr<RHS> for Binding<T>
Source§impl<T> Signal for Binding<T>where
T: 'static,
impl<T> Signal for Binding<T>where
T: 'static,
Source§fn watch(
&self,
watcher: impl Fn(Context<<Binding<T> as Signal>::Output>) + 'static,
) -> <Binding<T> as Signal>::Guard
fn watch( &self, watcher: impl Fn(Context<<Binding<T> as Signal>::Output>) + 'static, ) -> <Binding<T> as Signal>::Guard
Registers a watcher to be notified when the binding’s value changes.
Source§type Guard = Box<dyn WatcherGuard>
type Guard = Box<dyn WatcherGuard>
Auto Trait Implementations§
impl<T> Freeze for Binding<T>
impl<T> !RefUnwindSafe for Binding<T>
impl<T> !Send for Binding<T>
impl<T> !Sync for Binding<T>
impl<T> Unpin for Binding<T>
impl<T> !UnwindSafe for Binding<T>
Blanket Implementations§
Source§impl<S> AnimationExt for Swhere
S: SignalExt,
impl<S> AnimationExt for Swhere
S: SignalExt,
Source§fn animated(self) -> WithMetadata<Self, Animation>where
Self: Sized,
fn animated(self) -> WithMetadata<Self, Animation>where
Self: Sized,
Source§fn with_animation(self, animation: Animation) -> WithMetadata<Self, Animation>where
Self: Sized,
fn with_animation(self, animation: Animation) -> WithMetadata<Self, Animation>where
Self: Sized,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<C, F, T1, T2, Output> FlattenMap<F, (T1, T2), Output> for C
impl<C, F, T1, T2, Output> FlattenMap<F, (T1, T2), Output> for C
Source§impl<C, F, T1, T2, T3, Output> FlattenMap<F, (T1, T2, T3), Output> for C
impl<C, F, T1, T2, T3, Output> FlattenMap<F, (T1, T2, T3), Output> for C
Source§impl<T> IdentifiableExt for T
impl<T> IdentifiableExt for T
Source§impl<C> SignalExt for Cwhere
C: Signal,
impl<C> SignalExt for Cwhere
C: Signal,
Source§fn map<F, Output>(self, f: F) -> Map<Self, F, Output>
fn map<F, Output>(self, f: F) -> Map<Self, F, Output>
Source§fn cached(self) -> Cached<Self>
fn cached(self) -> Cached<Self>
Source§fn computed(self) -> Computed<Self::Output>where
Self: 'static,
fn computed(self) -> Computed<Self::Output>where
Self: 'static,
Computed container.