valid/
std_types.rs

1use crate::property::{HasCharCount, HasCheckedValue, HasEmptyValue, HasLength, HasMember};
2use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
3use std::hash::{BuildHasher, Hash};
4
5impl HasCheckedValue for bool {
6    fn is_checked_value(&self) -> bool {
7        *self
8    }
9}
10
11impl HasEmptyValue for String {
12    fn is_empty_value(&self) -> bool {
13        self.is_empty()
14    }
15}
16
17impl HasEmptyValue for &str {
18    fn is_empty_value(&self) -> bool {
19        self.is_empty()
20    }
21}
22
23impl<T> HasEmptyValue for Vec<T> {
24    fn is_empty_value(&self) -> bool {
25        self.is_empty()
26    }
27}
28
29impl<T> HasEmptyValue for &[T] {
30    fn is_empty_value(&self) -> bool {
31        self.is_empty()
32    }
33}
34
35impl<T> HasEmptyValue for VecDeque<T> {
36    fn is_empty_value(&self) -> bool {
37        self.is_empty()
38    }
39}
40
41impl<T> HasEmptyValue for LinkedList<T> {
42    fn is_empty_value(&self) -> bool {
43        self.is_empty()
44    }
45}
46
47impl<T, S> HasEmptyValue for HashSet<T, S> {
48    fn is_empty_value(&self) -> bool {
49        self.is_empty()
50    }
51}
52
53impl<K, V, S> HasEmptyValue for HashMap<K, V, S> {
54    fn is_empty_value(&self) -> bool {
55        self.is_empty()
56    }
57}
58
59impl<T> HasEmptyValue for BTreeSet<T> {
60    fn is_empty_value(&self) -> bool {
61        self.is_empty()
62    }
63}
64
65impl<K, V> HasEmptyValue for BTreeMap<K, V> {
66    fn is_empty_value(&self) -> bool {
67        self.is_empty()
68    }
69}
70
71impl<T> HasEmptyValue for Option<T>
72where
73    T: HasEmptyValue,
74{
75    fn is_empty_value(&self) -> bool {
76        match self {
77            Some(value) => value.is_empty_value(),
78            None => true,
79        }
80    }
81}
82
83impl HasLength for String {
84    fn length(&self) -> usize {
85        self.len()
86    }
87}
88
89impl HasLength for &str {
90    fn length(&self) -> usize {
91        self.len()
92    }
93}
94
95impl<T> HasLength for Vec<T> {
96    fn length(&self) -> usize {
97        self.len()
98    }
99}
100
101impl<T> HasLength for &[T] {
102    fn length(&self) -> usize {
103        self.len()
104    }
105}
106
107impl<T> HasLength for VecDeque<T> {
108    fn length(&self) -> usize {
109        self.len()
110    }
111}
112
113impl<T> HasLength for LinkedList<T> {
114    fn length(&self) -> usize {
115        self.len()
116    }
117}
118
119impl<T> HasLength for BTreeSet<T> {
120    fn length(&self) -> usize {
121        self.len()
122    }
123}
124
125impl<K, V> HasLength for BTreeMap<K, V> {
126    fn length(&self) -> usize {
127        self.len()
128    }
129}
130
131impl HasCharCount for String {
132    fn char_count(&self) -> usize {
133        self.chars().count()
134    }
135}
136
137impl HasCharCount for &str {
138    fn char_count(&self) -> usize {
139        self.chars().count()
140    }
141}
142
143impl HasCharCount for Vec<char> {
144    fn char_count(&self) -> usize {
145        self.len()
146    }
147}
148
149impl HasCharCount for &[char] {
150    fn char_count(&self) -> usize {
151        self.len()
152    }
153}
154
155impl HasMember<String> for String {
156    fn has_member(&self, element: &String) -> bool {
157        self.contains(element)
158    }
159}
160
161impl<T> HasMember<T> for VecDeque<T>
162where
163    T: PartialEq,
164{
165    fn has_member(&self, element: &T) -> bool {
166        self.contains(element)
167    }
168}
169
170impl<T> HasMember<T> for LinkedList<T>
171where
172    T: PartialEq,
173{
174    fn has_member(&self, element: &T) -> bool {
175        self.contains(element)
176    }
177}
178
179impl<T, S> HasMember<T> for HashSet<T, S>
180where
181    T: Eq + Hash,
182    S: BuildHasher,
183{
184    fn has_member(&self, element: &T) -> bool {
185        self.contains(element)
186    }
187}
188
189impl<K, V, S> HasMember<K> for HashMap<K, V, S>
190where
191    K: Eq + Hash,
192    S: BuildHasher,
193{
194    fn has_member(&self, element: &K) -> bool {
195        self.contains_key(element)
196    }
197}
198
199impl<T> HasMember<T> for BTreeSet<T>
200where
201    T: Ord,
202{
203    fn has_member(&self, element: &T) -> bool {
204        self.contains(element)
205    }
206}
207
208impl<K, V> HasMember<K> for BTreeMap<K, V>
209where
210    K: Ord,
211{
212    fn has_member(&self, element: &K) -> bool {
213        self.contains_key(element)
214    }
215}
216
217#[cfg(not(feature = "num-traits"))]
218mod without_num_traits {
219    use crate::property::HasZeroValue;
220
221    impl HasZeroValue for i8 {
222        fn is_zero_value(&self) -> bool {
223            *self == 0
224        }
225    }
226
227    impl HasZeroValue for i16 {
228        fn is_zero_value(&self) -> bool {
229            *self == 0
230        }
231    }
232
233    impl HasZeroValue for i32 {
234        fn is_zero_value(&self) -> bool {
235            *self == 0
236        }
237    }
238
239    impl HasZeroValue for i64 {
240        fn is_zero_value(&self) -> bool {
241            *self == 0
242        }
243    }
244
245    impl HasZeroValue for i128 {
246        fn is_zero_value(&self) -> bool {
247            *self == 0
248        }
249    }
250
251    impl HasZeroValue for u8 {
252        fn is_zero_value(&self) -> bool {
253            *self == 0
254        }
255    }
256
257    impl HasZeroValue for u16 {
258        fn is_zero_value(&self) -> bool {
259            *self == 0
260        }
261    }
262
263    impl HasZeroValue for u32 {
264        fn is_zero_value(&self) -> bool {
265            *self == 0
266        }
267    }
268
269    impl HasZeroValue for u64 {
270        fn is_zero_value(&self) -> bool {
271            *self == 0
272        }
273    }
274
275    impl HasZeroValue for u128 {
276        fn is_zero_value(&self) -> bool {
277            *self == 0
278        }
279    }
280
281    impl HasZeroValue for isize {
282        fn is_zero_value(&self) -> bool {
283            *self == 0
284        }
285    }
286
287    impl HasZeroValue for usize {
288        fn is_zero_value(&self) -> bool {
289            *self == 0
290        }
291    }
292
293    impl HasZeroValue for f32 {
294        fn is_zero_value(&self) -> bool {
295            *self == 0.
296        }
297    }
298
299    impl HasZeroValue for f64 {
300        fn is_zero_value(&self) -> bool {
301            *self == 0.
302        }
303    }
304}