Skip to main content

ListModel

Struct ListModel 

Source
pub struct ListModel<T> { /* private fields */ }
Expand description

An incremental list model.

Implementations§

Source§

impl<T> ListModel<T>
where T: Clone + 'static,

Source

pub fn new() -> Self

Create an empty model.

Examples found in repository?
examples/bench_large_table.rs (line 18)
14fn main() {
15    const ROWS: usize = 100_000;
16    const UPDATES: usize = 10_000;
17
18    let model = ListModel::new();
19    let events = Rc::new(Cell::new(0usize));
20    let events_rc = events.clone();
21    let _sub = model
22        .changes()
23        .observe(move |_| events_rc.set(events_rc.get() + 1));
24
25    let rows: Vec<String> = (0..ROWS).map(|i| format!("row-{i:05}")).collect();
26
27    let started = Instant::now();
28    model.replace_all(rows);
29    let load = started.elapsed();
30
31    let started = Instant::now();
32    for i in 0..UPDATES {
33        model.set(i % ROWS, format!("updated-{i:05}"));
34    }
35    let updates = started.elapsed();
36
37    let started = Instant::now();
38    model.sort_by(|a, b| a.cmp(b));
39    let sort = started.elapsed();
40
41    println!(
42        "rows={ROWS} updates={UPDATES} change-events={} ({} Reset + {UPDATES} Update, no rebuilds)\n\
43         load={load:?}  {UPDATES} per-row updates={updates:?}  sort={sort:?}",
44        events.get(),
45        events.get() - UPDATES
46    );
47}
Source

pub fn from(items: Vec<T>) -> Self

Create a model from items.

Source

pub fn len(&self) -> usize

Number of rows.

Source

pub fn is_empty(&self) -> bool

Whether the model is empty.

Source

pub fn changes(&self) -> EventStream<ListChange<T>>

A stream of structural changes.

Examples found in repository?
examples/bench_large_table.rs (line 22)
14fn main() {
15    const ROWS: usize = 100_000;
16    const UPDATES: usize = 10_000;
17
18    let model = ListModel::new();
19    let events = Rc::new(Cell::new(0usize));
20    let events_rc = events.clone();
21    let _sub = model
22        .changes()
23        .observe(move |_| events_rc.set(events_rc.get() + 1));
24
25    let rows: Vec<String> = (0..ROWS).map(|i| format!("row-{i:05}")).collect();
26
27    let started = Instant::now();
28    model.replace_all(rows);
29    let load = started.elapsed();
30
31    let started = Instant::now();
32    for i in 0..UPDATES {
33        model.set(i % ROWS, format!("updated-{i:05}"));
34    }
35    let updates = started.elapsed();
36
37    let started = Instant::now();
38    model.sort_by(|a, b| a.cmp(b));
39    let sort = started.elapsed();
40
41    println!(
42        "rows={ROWS} updates={UPDATES} change-events={} ({} Reset + {UPDATES} Update, no rebuilds)\n\
43         load={load:?}  {UPDATES} per-row updates={updates:?}  sort={sort:?}",
44        events.get(),
45        events.get() - UPDATES
46    );
47}
Source

pub fn get(&self, index: usize) -> Option<T>

The row at index, if any.

Source

pub fn snapshot(&self) -> Vec<T>

A snapshot of the current rows.

Source

pub fn push(&self, item: T)

Append item.

Source

pub fn extend(&self, items: Vec<T>)

Append every item in items.

Source

pub fn insert(&self, index: usize, item: T)

Insert item at index.

Source

pub fn remove(&self, index: usize) -> bool

Remove the row at index; returns whether a row was removed.

Source

pub fn remove_range(&self, index: usize, len: usize) -> bool

Remove len rows starting at index; returns whether anything was removed.

Source

pub fn set(&self, index: usize, item: T) -> bool

Replace the row at index; returns whether the row existed.

Examples found in repository?
examples/bench_large_table.rs (line 33)
14fn main() {
15    const ROWS: usize = 100_000;
16    const UPDATES: usize = 10_000;
17
18    let model = ListModel::new();
19    let events = Rc::new(Cell::new(0usize));
20    let events_rc = events.clone();
21    let _sub = model
22        .changes()
23        .observe(move |_| events_rc.set(events_rc.get() + 1));
24
25    let rows: Vec<String> = (0..ROWS).map(|i| format!("row-{i:05}")).collect();
26
27    let started = Instant::now();
28    model.replace_all(rows);
29    let load = started.elapsed();
30
31    let started = Instant::now();
32    for i in 0..UPDATES {
33        model.set(i % ROWS, format!("updated-{i:05}"));
34    }
35    let updates = started.elapsed();
36
37    let started = Instant::now();
38    model.sort_by(|a, b| a.cmp(b));
39    let sort = started.elapsed();
40
41    println!(
42        "rows={ROWS} updates={UPDATES} change-events={} ({} Reset + {UPDATES} Update, no rebuilds)\n\
43         load={load:?}  {UPDATES} per-row updates={updates:?}  sort={sort:?}",
44        events.get(),
45        events.get() - UPDATES
46    );
47}
Source

pub fn clear(&self)

Remove every row.

Source

pub fn replace_all(&self, items: Vec<T>)

Replace the entire list.

Examples found in repository?
examples/bench_large_table.rs (line 28)
14fn main() {
15    const ROWS: usize = 100_000;
16    const UPDATES: usize = 10_000;
17
18    let model = ListModel::new();
19    let events = Rc::new(Cell::new(0usize));
20    let events_rc = events.clone();
21    let _sub = model
22        .changes()
23        .observe(move |_| events_rc.set(events_rc.get() + 1));
24
25    let rows: Vec<String> = (0..ROWS).map(|i| format!("row-{i:05}")).collect();
26
27    let started = Instant::now();
28    model.replace_all(rows);
29    let load = started.elapsed();
30
31    let started = Instant::now();
32    for i in 0..UPDATES {
33        model.set(i % ROWS, format!("updated-{i:05}"));
34    }
35    let updates = started.elapsed();
36
37    let started = Instant::now();
38    model.sort_by(|a, b| a.cmp(b));
39    let sort = started.elapsed();
40
41    println!(
42        "rows={ROWS} updates={UPDATES} change-events={} ({} Reset + {UPDATES} Update, no rebuilds)\n\
43         load={load:?}  {UPDATES} per-row updates={updates:?}  sort={sort:?}",
44        events.get(),
45        events.get() - UPDATES
46    );
47}
Source

pub fn sort_by<F>(&self, compare: F)
where F: FnMut(&T, &T) -> Ordering,

Sort the list in place with compare, emitting a single Reset.

Examples found in repository?
examples/bench_large_table.rs (line 38)
14fn main() {
15    const ROWS: usize = 100_000;
16    const UPDATES: usize = 10_000;
17
18    let model = ListModel::new();
19    let events = Rc::new(Cell::new(0usize));
20    let events_rc = events.clone();
21    let _sub = model
22        .changes()
23        .observe(move |_| events_rc.set(events_rc.get() + 1));
24
25    let rows: Vec<String> = (0..ROWS).map(|i| format!("row-{i:05}")).collect();
26
27    let started = Instant::now();
28    model.replace_all(rows);
29    let load = started.elapsed();
30
31    let started = Instant::now();
32    for i in 0..UPDATES {
33        model.set(i % ROWS, format!("updated-{i:05}"));
34    }
35    let updates = started.elapsed();
36
37    let started = Instant::now();
38    model.sort_by(|a, b| a.cmp(b));
39    let sort = started.elapsed();
40
41    println!(
42        "rows={ROWS} updates={UPDATES} change-events={} ({} Reset + {UPDATES} Update, no rebuilds)\n\
43         load={load:?}  {UPDATES} per-row updates={updates:?}  sort={sort:?}",
44        events.get(),
45        events.get() - UPDATES
46    );
47}
Source

pub fn retain<F>(&self, keep: F)
where F: FnMut(&T) -> bool,

Remove every row that does not satisfy keep, emitting one Remove change per contiguous removed run (in ascending index order).

Trait Implementations§

Source§

impl<T> Default for ListModel<T>
where T: Clone + 'static,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for ListModel<T>

§

impl<T> !RefUnwindSafe for ListModel<T>

§

impl<T> !Send for ListModel<T>

§

impl<T> !Sync for ListModel<T>

§

impl<T> !UnwindSafe for ListModel<T>

§

impl<T> Unpin for ListModel<T>
where RefCell<Vec<T>>: Unpin, Sink<ListChange<T>>: Unpin,

§

impl<T> UnsafeUnpin for ListModel<T>

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.