Binding

Trait Binding 

Source
pub trait Binding<S>:
    Clone
    + Copy
    + 'static {
    // Required methods
    fn get<'a>(&self, cx: &'a Context) -> &'a S;
    fn get_mut<'a>(&self, cx: &'a mut Context) -> &'a mut S;

    // Provided methods
    fn with<T>(&self, cx: &Context, f: impl FnOnce(&S) -> T) -> T { ... }
    fn with_mut<T>(&self, cx: &mut Context, f: impl FnOnce(&mut S) -> T) -> T { ... }
}
Expand description

Reads or writes a value owned by a source-of-truth.

Required Methods§

Source

fn get<'a>(&self, cx: &'a Context) -> &'a S

Source

fn get_mut<'a>(&self, cx: &'a mut Context) -> &'a mut S

Provided Methods§

Source

fn with<T>(&self, cx: &Context, f: impl FnOnce(&S) -> T) -> T

Examples found in repository?
examples/todo_list.rs (line 19)
17fn todo_list(todos: impl Binding<Vec<String>>) -> impl View {
18    with_cx(move |cx| {
19        let len = todos.with(cx, |todos| todos.len());
20        let ids = (0usize..len).collect();
21
22        list(ids, move |id| {
23            let id = *id;
24            with_cx(move |cx| todos.with(cx, |todos| todos[id].clone()))
25        })
26    })
27}
Source

fn with_mut<T>(&self, cx: &mut Context, f: impl FnOnce(&mut S) -> T) -> T

Examples found in repository?
examples/todo_list.rs (line 9)
3fn add_button(todos: impl Binding<Vec<String>>) -> impl View {
4    state(String::new, move |name, _| {
5        hstack((
6            text_editor(name),
7            button(text("Add Item"), move |cx| {
8                let name_str = cx[name].clone();
9                todos.with_mut(cx, |todos| todos.push(name_str));
10                // Gotta fix a bug in text_editor!
11                // cx[name] = String::new();
12            }),
13        ))
14    })
15}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<S, B, L, T> Binding<S> for Map<B, L, S, T>
where B: Binding<T>, L: Lens<T, S>, S: 'static, T: 'static,

Source§

impl<S: 'static> Binding<S> for StateHandle<S>