len_trait/
impls.rs

1macro_rules! do_impl {
2    (Empty for $name:ident; $($gen:tt)*) => {
3        do_impl!(Empty for $name, $name; $($gen)*);
4    };
5    (Empty for $name:ident, $impl_for:ty; $($gen:tt)*) => {
6        impl<$($gen)*> $crate::len::Empty for $impl_for {
7            fn is_empty(&self) -> bool {
8                $name::is_empty(self)
9            }
10        }
11    };
12    (Len for $name:ident; $($gen:tt)*) => {
13        do_impl!(Len for $name, $name; $($gen)*);
14    };
15    (Len for $name:ident, $impl_for:ty; $($gen:tt)*) => {
16        do_impl!(Empty for $name, $impl_for; $($gen)*);
17        impl<$($gen)*> $crate::len::Len for $impl_for {
18            fn len(&self) -> usize {
19                $name::len(self)
20            }
21        }
22    };
23    (Clear for $name:ident; $($gen:tt)*) => {
24        do_impl!(Clear for $name, $name; $($gen)*);
25    };
26    (Clear for $name:ident, $impl_for:ty; $($gen:tt)*) => {
27        impl<$($gen)*> $crate::len::Clear for $impl_for {
28            fn clear(&mut self) {
29                $name::clear(self)
30            }
31        }
32    };
33    (LenMut for $name:ident; $($gen:tt)*) => {
34        do_impl!(LenMut for $name, $name; $($gen)*);
35    };
36    (LenMut for $name:ident, $impl_for:ty; $($gen:tt)*) => {
37        do_impl!(Clear for $name, $impl_for; $($gen)*);
38        impl<$($gen)*> $crate::len::LenMut for $impl_for {
39            fn truncate(&mut self, len: usize) {
40                $name::truncate(self, len)
41            }
42            fn split_off(&mut self, index: usize) -> Self {
43                $name::split_off(self, index)
44            }
45        }
46    };
47    (Capacity for $name:ident; $($gen:tt)*) => {
48        do_impl!(Capacity for $name, $name; $($gen)*);
49    };
50    (Capacity for $name:ident, $impl_for:ty; $($gen:tt)*) => {
51        impl<$($gen)*> $crate::capacity::Capacity for $impl_for {
52            fn capacity(&self) -> usize {
53                $name::capacity(self)
54            }
55        }
56    };
57    (WithCapacity for $name:ident; $($gen:tt)*) => {
58        do_impl!(WithCapacity for $name, $name; $($gen)*);
59    };
60    (WithCapacity for $name:ident, $impl_for:ty; $($gen:tt)*) => {
61        do_impl!(Capacity for $name, $impl_for; $($gen)*);
62        impl<$($gen)*> $crate::capacity::WithCapacity for $impl_for {
63            fn with_capacity(capacity: usize) -> Self {
64                $name::with_capacity(capacity)
65            }
66        }
67    };
68    (CapacityMut for $name:ident; $($gen:tt)*) => {
69        do_impl!(CapacityMut for $name, $name; $($gen)*);
70    };
71    (CapacityMut for $name:ident, $impl_for:ty; $($gen:tt)*) => {
72        do_impl!(WithCapacity for $name, $impl_for; $($gen)*);
73        impl<$($gen)*> $crate::capacity::CapacityMut for $impl_for {
74            fn reserve(&mut self, additional: usize) {
75                $name::reserve(self, additional)
76            }
77            fn reserve_exact(&mut self, additional: usize) {
78                $name::reserve_exact(self, additional)
79            }
80            fn shrink_to_fit(&mut self) {
81                $name::shrink_to_fit(self)
82            }
83        }
84    };
85    (inexact CapacityMut for $name:ident, $impl_for:ty; $($gen:tt)*) => {
86        do_impl!(WithCapacity for $name, $impl_for; $($gen)*);
87        impl<$($gen)*> $crate::capacity::CapacityMut for $impl_for {
88            fn reserve(&mut self, additional: usize) {
89                $name::reserve(self, additional)
90            }
91            fn shrink_to_fit(&mut self) {
92                $name::shrink_to_fit(self)
93            }
94        }
95    };
96    (noshrink CapacityMut for $name:ident, $impl_for:ty; $($gen:tt)*) => {
97        do_impl!(WithCapacity for $name, $impl_for; $($gen)*);
98        impl<$($gen)*> $crate::capacity::CapacityMut for $impl_for {
99            fn reserve(&mut self, additional: usize) {
100                $name::reserve(self, additional)
101            }
102            fn reserve_exact(&mut self, additional: usize) {
103                $name::reserve_exact(self, additional)
104            }
105        }
106    };
107    (SplitAt for $name:ident; $($gen:tt)*) => {
108        do_impl!(SplitAt for $name, $name; $($gen)*);
109    };
110    (SplitAt for $name:ident, $impl_for:ty; $($gen:tt)*) => {
111        impl<$($gen)*> $crate::index::SplitAt<usize> for $impl_for {
112            fn split_at(&self, index: usize) -> (&Self, &Self) {
113                $name::split_at(self, index)
114            }
115        }
116    };
117    (SplitAtMut for $name:ident; $($gen:tt)*) => {
118        do_impl!(SplitAtMut for $name, $name; $($gen)*);
119    };
120    (SplitAtMut for $name:ident, $impl_for:ty; $($gen:tt)*) => {
121        do_impl!(SplitAt for $name, $impl_for; $($gen)*);
122        impl<$($gen)*> $crate::index::SplitAtMut<usize> for $impl_for {
123            fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) {
124                $name::split_at_mut(self, index)
125            }
126        }
127    };
128}
129
130cfg_if! {
131    if #[cfg(feature = "std")] {
132        type Slice<T> = [T];
133    } else {
134        use core::slice::SliceExt as Slice;
135    }
136}
137do_impl!(Len for Slice, [T]; T);
138do_impl!(SplitAtMut for Slice, [T]; T);
139
140cfg_if! {
141    if #[cfg(feature = "std")] {
142        type Str = str;
143    } else {
144        use core::str::StrExt as Str;
145    }
146}
147do_impl!(Len for Str, str; );
148do_impl!(SplitAtMut for Str, str; );
149
150cfg_if! {
151    if #[cfg(feature = "alloc")] {
152        cfg_if! {
153            if #[cfg(feature = "std")] {
154                use std::boxed::Box;
155                use std::rc::Rc;
156                use std::sync::Arc;
157                use std::collections;
158            } else {
159                use alloc::boxed::Box;
160                use alloc::rc::Rc;
161                use alloc::arc::Arc;
162                use alloc::{self as collections, String, Vec};
163            }
164        }
165        use self::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
166
167
168        impl<T: ?Sized + super::len::Empty> super::len::Empty for Box<T> {
169            fn is_empty(&self) -> bool {
170                T::is_empty(self)
171            }
172        }
173        impl<T: ?Sized + super::len::Len> super::len::Len for Box<T> {
174            fn len(&self) -> usize {
175                T::len(self)
176            }
177        }
178        impl<T: ?Sized + super::capacity::Capacity> super::capacity::Capacity for Box<T> {
179            fn capacity(&self) -> usize {
180                T::capacity(self)
181            }
182        }
183
184        impl<T: ?Sized + super::len::Empty> super::len::Empty for Rc<T> {
185            fn is_empty(&self) -> bool {
186                T::is_empty(self)
187            }
188        }
189        impl<T: ?Sized + super::len::Len> super::len::Len for Rc<T> {
190            fn len(&self) -> usize {
191                T::len(self)
192            }
193        }
194        impl<T: ?Sized + super::capacity::Capacity> super::capacity::Capacity for Rc<T> {
195            fn capacity(&self) -> usize {
196                T::capacity(self)
197            }
198        }
199
200        impl<T: ?Sized + super::len::Empty> super::len::Empty for Arc<T> {
201            fn is_empty(&self) -> bool {
202                T::is_empty(self)
203            }
204        }
205        impl<T: ?Sized + super::len::Len> super::len::Len for Arc<T> {
206            fn len(&self) -> usize {
207                T::len(self)
208            }
209        }
210        impl<T: ?Sized + super::capacity::Capacity> super::capacity::Capacity for Arc<T> {
211            fn capacity(&self) -> usize {
212                T::capacity(self)
213            }
214        }
215
216
217        do_impl!(Len for BTreeMap, BTreeMap<K, V>; K: Ord, V);
218        do_impl!(Clear for BTreeMap, BTreeMap<K, V>; K: Ord, V);
219
220        do_impl!(Len for BTreeSet, BTreeSet<T>; T: Ord);
221        do_impl!(Clear for BTreeSet, BTreeSet<T>; T: Ord);
222
223        do_impl!(Len for BinaryHeap, BinaryHeap<T>; T: Ord);
224        do_impl!(Clear for BinaryHeap, BinaryHeap<T>; T: Ord);
225        do_impl!(CapacityMut for BinaryHeap, BinaryHeap<T>; T: Ord);
226
227        do_impl!(Len for LinkedList, LinkedList<T>; T);
228        do_impl!(Clear for LinkedList, LinkedList<T>; T);
229
230        do_impl!(Len for str, String; );
231        do_impl!(LenMut for String; );
232        do_impl!(CapacityMut for String; );
233
234        do_impl!(Len for Self, Vec<T>; T);
235        do_impl!(LenMut for Vec, Vec<T>; T);
236        do_impl!(CapacityMut for Vec, Vec<T>; T);
237
238        do_impl!(Len for VecDeque, VecDeque<T>; T);
239        do_impl!(LenMut for VecDeque, VecDeque<T>; T);
240        do_impl!(CapacityMut for VecDeque, VecDeque<T>; T);
241    }
242}
243
244cfg_if! {
245    if #[cfg(feature = "std")] {
246        use std::ffi;
247
248        impl super::len::Empty for ffi::CStr {
249            fn is_empty(&self) -> bool {
250                self.to_bytes().is_empty()
251            }
252        }
253        impl super::len::Len for ffi::CStr {
254            fn len(&self) -> usize {
255                self.to_bytes().len()
256            }
257        }
258
259        impl super::len::Empty for ffi::CString {
260            fn is_empty(&self) -> bool {
261                self.as_bytes().is_empty()
262            }
263        }
264        impl super::len::Len for ffi::CString {
265            fn len(&self) -> usize {
266                self.as_bytes().len()
267            }
268        }
269        // TODO: Clear for CString
270        // TODO: LenMut for CString
271
272        use self::ffi::{OsStr, OsString};
273        do_impl!(Len for OsStr; );
274        do_impl!(Len for OsStr, OsString; );
275        do_impl!(Clear for OsString; );
276        // TODO: LenMut for OsString
277        do_impl!(CapacityMut for OsString; );
278
279        use std::hash::Hash;
280        use std::collections::HashMap;
281        do_impl!(Len for HashMap, HashMap<K, V>; K: Eq + Hash, V);
282        do_impl!(Clear for HashMap, HashMap<K, V>; K: Eq + Hash, V);
283        do_impl!(inexact CapacityMut for HashMap, HashMap<K, V>; K: Eq + Hash, V);
284
285        use std::collections::HashSet;
286        do_impl!(Len for HashSet, HashSet<T>; T: Eq + Hash);
287        do_impl!(Clear for HashSet, HashSet<T>; T: Eq + Hash);
288        do_impl!(inexact CapacityMut for HashSet, HashSet<T>; T: Eq + Hash);
289    }
290}