composable_indexes/index/
foreign.rs

1use crate::{
2    Index, ShallowClone,
3    core::{Insert, Remove, Seal, Update},
4};
5
6impl<T> Index<T> for () {
7    #[inline]
8    fn insert(&mut self, _seal: Seal, _op: &Insert<T>) {}
9
10    #[inline]
11    fn remove(&mut self, _seal: Seal, _op: &Remove<T>) {}
12
13    #[inline]
14    fn update(&mut self, _seal: Seal, _op: &Update<T>) {}
15}
16
17impl ShallowClone for () {}
18
19impl<T, Inner, const N: usize> Index<T> for [Inner; N]
20where
21    Inner: Index<T>,
22{
23    #[inline]
24    fn insert(&mut self, seal: Seal, op: &Insert<T>) {
25        for inner in self.iter_mut() {
26            inner.insert(seal, op);
27        }
28    }
29
30    #[inline]
31    fn remove(&mut self, seal: Seal, op: &Remove<T>) {
32        for inner in self.iter_mut() {
33            inner.remove(seal, op);
34        }
35    }
36
37    #[inline]
38    fn update(&mut self, seal: Seal, op: &Update<T>) {
39        for inner in self.iter_mut() {
40            inner.update(seal, op);
41        }
42    }
43}
44
45impl<T, Inner> Index<T> for alloc::vec::Vec<Inner>
46where
47    Inner: Index<T>,
48{
49    #[inline]
50    fn insert(&mut self, seal: Seal, op: &Insert<T>) {
51        for inner in self.iter_mut() {
52            inner.insert(seal, op);
53        }
54    }
55
56    #[inline]
57    fn remove(&mut self, seal: Seal, op: &Remove<T>) {
58        for inner in self.iter_mut() {
59            inner.remove(seal, op);
60        }
61    }
62
63    #[inline]
64    fn update(&mut self, seal: Seal, op: &Update<T>) {
65        for inner in self.iter_mut() {
66            inner.update(seal, op);
67        }
68    }
69}
70
71impl<T, Inner> Index<T> for Option<Inner>
72where
73    Inner: Index<T>,
74{
75    #[inline]
76    fn insert(&mut self, seal: Seal, op: &Insert<T>) {
77        if let Some(inner) = self.as_mut() {
78            inner.insert(seal, op);
79        }
80    }
81
82    #[inline]
83    fn remove(&mut self, seal: Seal, op: &Remove<T>) {
84        if let Some(inner) = self.as_mut() {
85            inner.remove(seal, op);
86        }
87    }
88
89    #[inline]
90    fn update(&mut self, seal: Seal, op: &Update<T>) {
91        if let Some(inner) = self.as_mut() {
92            inner.update(seal, op);
93        }
94    }
95}
96
97impl<Inner: ShallowClone> ShallowClone for Option<Inner> {}
98
99impl<T, Left, Right> Index<T> for Result<Left, Right>
100where
101    Left: Index<T>,
102    Right: Index<T>,
103{
104    #[inline]
105    fn insert(&mut self, seal: Seal, op: &Insert<T>) {
106        match self {
107            Ok(left) => left.insert(seal, op),
108            Err(right) => right.insert(seal, op),
109        }
110    }
111
112    #[inline]
113    fn remove(&mut self, seal: Seal, op: &Remove<T>) {
114        match self {
115            Ok(left) => left.remove(seal, op),
116            Err(right) => right.remove(seal, op),
117        }
118    }
119
120    #[inline]
121    fn update(&mut self, seal: Seal, op: &Update<T>) {
122        match self {
123            Ok(left) => left.update(seal, op),
124            Err(right) => right.update(seal, op),
125        }
126    }
127}
128
129impl<Left: ShallowClone, Right: ShallowClone> ShallowClone for Result<Left, Right> {}
130
131// Tuples
132
133macro_rules! tuple_impl {
134    ($n:literal) => {
135        seq_macro::seq!(N in 0..$n {
136            impl<In, #( Ix~N, )*> Index<In> for ( #( Ix~N, )* )
137            where
138                #( Ix~N: Index<In>, )*
139            {
140                #[inline]
141                fn insert(&mut self, seal: Seal, op: &Insert<In>) {
142                    #(self.N.insert(seal, op);)*
143                }
144
145                #[inline]
146                fn remove(&mut self, seal: Seal, op: &Remove<In>) {
147                    #(self.N.remove(seal, op);)*
148                }
149            }
150
151            impl< #( Ix~N: ShallowClone, )* > ShallowClone for ( #( Ix~N, )* ) {}
152        });
153    };
154}
155
156tuple_impl!(2);
157tuple_impl!(3);
158tuple_impl!(4);
159tuple_impl!(5);
160tuple_impl!(6);
161tuple_impl!(7);
162tuple_impl!(8);
163tuple_impl!(9);
164tuple_impl!(10);
165tuple_impl!(11);
166tuple_impl!(12);
167tuple_impl!(13);
168tuple_impl!(14);
169tuple_impl!(15);
170tuple_impl!(16);