Skip to main content

traiter/collections/
traits.rs

1pub trait Capacitary {
2    type Capacity;
3
4    /// Returns capacity of a collection.
5    #[cfg_attr(
6        feature = "std",
7        doc = r##"
8```
9use traiter::collections::Capacitary;
10let collection = Vec::<i8>::with_capacity(10);
11assert_eq!(Capacitary::capacity(&collection), 10);
12```
13"##
14    )]
15    fn capacity(self) -> Self::Capacity;
16}
17
18pub trait Clearable {
19    /// Removes all elements from a collection.
20    #[cfg_attr(
21        feature = "std",
22        doc = r##"
23```
24use traiter::collections::{Clearable, Emptiable};
25let mut collection = vec![0];
26assert!(!Emptiable::is_empty(&collection));
27Clearable::clear(&mut collection);
28assert!(Emptiable::is_empty(&collection));
29```
30"##
31    )]
32    fn clear(self);
33}
34
35pub trait Emptiable {
36    /// Checks if collection is empty.
37    /// ```
38    /// use traiter::collections::Emptiable;
39    /// assert!(Emptiable::is_empty(&[0; 0]));
40    /// assert!(!Emptiable::is_empty(&[0]));
41    /// ```
42    fn is_empty(self) -> bool;
43}
44
45pub trait ItemInsertable {
46    type Key;
47    type Output;
48    type Value;
49
50    /// Inserts item into a collection.
51    #[cfg_attr(
52        feature = "std",
53        doc = r##"
54```
55use traiter::collections::ItemInsertable;
56assert_eq!(ItemInsertable::insert_item(&mut vec![10], 0, 20), ());
57```
58"##
59    )]
60    fn insert_item(self, key: Self::Key, value: Self::Value) -> Self::Output;
61}
62
63pub trait ItemRemovable {
64    type Key;
65    type Output;
66
67    /// Removes item from a collection by key.
68    #[cfg_attr(
69        feature = "std",
70        doc = r##"
71```
72use traiter::collections::ItemRemovable;
73assert_eq!(ItemRemovable::remove_item(&mut vec![10], 0), 10);
74```
75"##
76    )]
77    fn remove_item(self, key: Self::Key) -> Self::Output;
78}
79
80pub trait Iterable {
81    type Item;
82    type Output: Iterator<Item = Self::Item>;
83
84    /// Returns an iterator over elements in a collection.
85    /// ```
86    /// use traiter::collections::Iterable;
87    /// assert_eq!(Iterable::iter(&[0; 0]).next(), None);
88    /// ```
89    fn iter(self) -> Self::Output;
90}
91
92pub trait KeyContainer {
93    type Key;
94
95    /// Checks if collection contains a key.
96    #[cfg_attr(
97        feature = "std",
98        doc = r##"
99```
100use std::collections::HashMap;
101use traiter::collections::{KeyContainer, ItemInsertable};
102let mut collection = HashMap::new();
103assert!(!KeyContainer::contains_key(&collection, &10));
104assert_eq!(ItemInsertable::insert_item(&mut collection, 10, 20), None);
105assert!(KeyContainer::contains_key(&collection, &10));
106```
107"##
108    )]
109    fn contains_key(self, key: Self::Key) -> bool;
110}
111
112pub trait Lengthsome {
113    type Length;
114
115    /// Returns number of elements in a collection.
116    /// ```
117    /// use traiter::collections::Lengthsome;
118    /// assert_eq!(Lengthsome::len([0; 0]), 0);
119    /// assert_eq!(Lengthsome::len([0]), 1);
120    /// ```
121    fn len(self) -> Self::Length;
122}
123
124pub trait MutablyIterable {
125    type Item;
126    type Output: Iterator<Item = Self::Item>;
127
128    /// Returns an iterator over mutable elements in a collection.
129    /// ```
130    /// use traiter::collections::MutablyIterable;
131    /// assert_eq!(MutablyIterable::iter_mut(&mut [0; 0]).next(), None);
132    /// ```
133    fn iter_mut(self) -> Self::Output;
134}
135
136pub trait Reservable: Capacitary {
137    /// Reserves capacity for at least given number of elements for a collection.
138    #[cfg_attr(
139        feature = "std",
140        doc = r##"
141```
142use traiter::collections::{Capacitary, Reservable};
143let mut collection = Vec::<i8>::with_capacity(10);
144assert_eq!(Capacitary::capacity(&collection), 10);
145Reservable::reserve(&mut collection, 20);
146assert_eq!(Capacitary::capacity(&collection), 20);
147```
148"##
149    )]
150    fn reserve(self, additional: Self::Capacity);
151}
152
153pub trait TryReservable: Capacitary {
154    type Error;
155
156    /// Tries to reserve capacity for at least given number of elements for a collection.
157    #[cfg_attr(
158        feature = "std",
159        doc = r##"
160```
161use traiter::collections::{Capacitary, Reservable};
162let mut collection = Vec::<i8>::with_capacity(10);
163assert_eq!(Capacitary::capacity(&collection), 10);
164Reservable::reserve(&mut collection, 20);
165assert_eq!(Capacitary::capacity(&collection), 20);
166```
167"##
168    )]
169    fn try_reserve(
170        self,
171        additional: Self::Capacity,
172    ) -> Result<(), Self::Error>;
173}
174
175pub trait ValueContainer {
176    type Value;
177
178    /// Checks if collection contains a value.
179    /// ```
180    /// use traiter::collections::ValueContainer;
181    /// assert!(ValueContainer::contains_value(&[0], &0));
182    /// assert!(!ValueContainer::contains_value(&[0], &1));
183    /// ```
184    fn contains_value(self, value: Self::Value) -> bool;
185}
186
187pub trait ValueInsertable {
188    type Output;
189    type Value;
190
191    /// Inserts value into a collection.
192    #[cfg_attr(
193        feature = "std",
194        doc = r##"
195```
196use std::collections::HashSet;
197use traiter::collections::{ValueContainer, ValueInsertable};
198let mut collection = HashSet::new();
199assert!(!ValueContainer::contains_value(&collection, &10));
200assert!(ValueInsertable::insert_value(&mut collection, 10));
201assert!(ValueContainer::contains_value(&collection, &10));
202```
203"##
204    )]
205    fn insert_value(self, value: Self::Value) -> Self::Output;
206}
207
208pub trait ValueRemovable {
209    type Output;
210    type Value;
211
212    /// Removes value from a collection.
213    #[cfg_attr(
214        feature = "std",
215        doc = r##"
216```
217use std::collections::HashSet;
218use traiter::collections::ValueRemovable;
219let mut collection = HashSet::new();
220collection.insert(10);
221assert!(ValueRemovable::remove_value(&mut collection, &10));
222assert!(!ValueRemovable::remove_value(&mut collection, &10));
223```
224"##
225    )]
226    fn remove_value(self, value: Self::Value) -> Self::Output;
227}