| |
1 /*! |
| |
2 Abstract collections of objects. |
| |
3 */ |
| |
4 |
| |
5 use crate::loc::Loc; |
| |
6 |
| |
7 /// An abstract collection of elements. |
| |
8 pub trait Collection : IntoIterator<Item = Self::Element> { |
| |
9 /// Type of elements of the collection |
| |
10 type Element; |
| |
11 /// Iterator over references to elements of the collection |
| |
12 type RefsIter<'a> : Iterator<Item=&'a Self::Element> where Self : 'a; |
| |
13 |
| |
14 /// Returns an iterator over references to elements of the collection. |
| |
15 fn iter_refs(&self) -> Self::RefsIter<'_>; |
| |
16 } |
| |
17 |
| |
18 /// An abstract collection of mutable elements. |
| |
19 pub trait CollectionMut : Collection { |
| |
20 /// Iterator over references to elements of the collection |
| |
21 type RefsIterMut<'a> : Iterator<Item=&'a mut Self::Element> where Self : 'a; |
| |
22 |
| |
23 /// Returns an iterator over references to elements of the collection. |
| |
24 fn iter_refs_mut(&mut self) -> Self::RefsIterMut<'_>; |
| |
25 } |
| |
26 |
| |
27 /// Helps implement Collection and CollectionMut for slice-like collections. |
| |
28 #[macro_export] |
| |
29 macro_rules! slice_like_collection { |
| |
30 ($type : ty where $($where:tt)*) => { |
| |
31 impl<$($where)*> Collection for $type { |
| |
32 type Element = E; |
| |
33 type RefsIter<'_a> = std::slice::Iter<'_a, E> where Self : '_a; |
| |
34 |
| |
35 #[inline] |
| |
36 fn iter_refs(&self) -> Self::RefsIter<'_> { |
| |
37 self.iter() |
| |
38 } |
| |
39 } |
| |
40 |
| |
41 impl<$($where)*> CollectionMut for $type { |
| |
42 type RefsIterMut<'_a> = std::slice::IterMut<'_a, E> where Self : '_a; |
| |
43 |
| |
44 #[inline] |
| |
45 fn iter_refs_mut(&mut self) -> Self::RefsIterMut<'_> { |
| |
46 self.iter_mut() |
| |
47 } |
| |
48 } |
| |
49 } |
| |
50 } |
| |
51 |
| |
52 slice_like_collection!(Vec<E> where E); |
| |
53 slice_like_collection!([E; N] where E, const N : usize); |
| |
54 slice_like_collection!(Loc<E, N> where E, const N : usize); |