kas_view/
data_impls.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Impls for data traits
7
8use crate::{ListData, SharedData};
9use std::fmt::Debug;
10
11macro_rules! impl_list_data {
12    ($ty:ty) => {
13        impl<T: Clone + Debug + 'static> SharedData for $ty {
14            type Key = usize;
15            type Item = T;
16            type ItemRef<'b> = &'b T;
17
18            fn contains_key(&self, key: &Self::Key) -> bool {
19                *key < self.len()
20            }
21            fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>> {
22                self.get(*key)
23            }
24            fn get_cloned(&self, key: &usize) -> Option<Self::Item> {
25                self.get(*key).cloned()
26            }
27        }
28        impl<T: Clone + Debug + 'static> ListData for $ty {
29            fn is_empty(&self) -> bool {
30                (*self).is_empty()
31            }
32
33            fn len(&self) -> usize {
34                (*self).len()
35            }
36
37            fn iter_from(&self, start: usize, limit: usize) -> impl Iterator<Item = Self::Key> {
38                let len = (*self).len();
39                start.min(len)..(start + limit).min(len)
40            }
41        }
42    };
43}
44
45impl_list_data!([T]);
46impl_list_data!(Vec<T>);