1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Impls for data traits

use super::*;
use crate::WidgetId;
use std::fmt::Debug;

macro_rules! impl_list_data {
    ($ty:ty) => {
        impl<T: Clone + Debug + 'static> SharedData for $ty {
            type Key = usize;
            type Item = T;
            type ItemRef<'b> = &'b T;

            fn version(&self) -> u64 {
                1
            }

            fn contains_key(&self, key: &Self::Key) -> bool {
                *key < self.len()
            }
            fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>> {
                self.get(*key)
            }
            fn get_cloned(&self, key: &usize) -> Option<Self::Item> {
                self.get(*key).cloned()
            }
        }
        impl<T: Clone + Debug + 'static> ListData for $ty {
            type KeyIter<'b> = std::ops::Range<usize>;

            fn is_empty(&self) -> bool {
                (*self).is_empty()
            }

            fn len(&self) -> usize {
                (*self).len()
            }

            fn make_id(&self, parent: &WidgetId, key: &Self::Key) -> WidgetId {
                parent.make_child(*key)
            }
            fn reconstruct_key(&self, parent: &WidgetId, child: &WidgetId) -> Option<Self::Key> {
                child.next_key_after(parent)
            }

            fn iter_from(&self, start: usize, limit: usize) -> Self::KeyIter<'_> {
                let len = (*self).len();
                start.min(len)..(start + limit).min(len)
            }
        }
    };
}

impl_list_data!([T]);
impl_list_data!(Vec<T>);