Trait rui::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)
17
18
19
20
21
22
23
24
25
26
27
fn todo_list(todos: impl Binding<Vec<String>>) -> impl View {
    with_cx(move |cx| {
        let len = todos.with(cx, |todos| todos.len());
        let ids = (0usize..len).collect();

        list(ids, move |id| {
            let id = *id;
            with_cx(move |cx| todos.with(cx, |todos| todos[id].clone()))
        })
    })
}
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)
3
4
5
6
7
8
9
10
11
12
13
14
15
fn add_button(todos: impl Binding<Vec<String>>) -> impl View {
    state(String::new, move |name, _| {
        hstack((
            text_editor(name),
            button(text("Add Item"), move |cx| {
                let name_str = cx[name].clone();
                todos.with_mut(cx, |todos| todos.push(name_str));
                // Gotta fix a bug in text_editor!
                // cx[name] = String::new();
            }),
        ))
    })
}

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>