1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use std::hash::Hash;
use crate::Iterable;
/// Non-consuming transform operations.
///
/// Methods have the following properties:
///
/// - Requires collection elements to implement [`Clone`]
/// - Does not consume the collection or its elements
/// - Creates a new collection
///
pub trait Transform<Item> {
/// Transforms this collection into specified collection type.
///
/// `collect()` can take any collection, and turn it into a relevant
/// collection. This can be used in a variety of contexts.
///
/// `collect()` can also create instances of types that are not typical
/// collections. For example, a [`String`] can be built from [`char`]s,
/// and a collection of [`Result<T, E>`][`Result`] items can be collected
/// into `Result<Collection<T>, E>`. See the examples below for more.
///
/// Because `collect()` is so general, it can cause problems with type
/// inference. As such, `collect()` is one of the few times you'll see
/// the syntax affectionately known as the 'turbofish': `::<>`. This
/// helps the inference algorithm understand specifically which collection
/// you're trying to collect into.
///
/// This is a non-consuming variant of [`collect_to()`].
///
/// [`collect_to()`]: crate::CollectionInto::collect_to
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use cantrip::*;
/// use std::collections::{LinkedList, VecDeque};
///
/// let a = VecDeque::from([1, 2, 3]);
///
/// let collected: LinkedList<i32> = a.collect_to();
///
/// assert_eq!(collected, LinkedList::from([1, 2, 3]));
/// ```
///
/// Note that we needed the `::LinkedList<i32>` on the left-hand side. This is because
/// we could collect into, for example, a [`VecDeque<T>`] instead:
///
/// ```
/// use cantrip::*;
/// use std::collections::{LinkedList, VecDeque};
///
/// let a = LinkedList::from([1, 2, 3]);
///
/// let collected: VecDeque<i32> = a.collect_to();
///
/// assert_eq!(collected, VecDeque::from([1, 2, 3]));
/// ```
///
/// Using the 'turbofish' instead of annotating `collected`:
///
/// ```
/// use cantrip::*;
/// use std::collections::{LinkedList, VecDeque};
///
/// let a = LinkedList::from([1, 2, 3]);
///
/// assert_eq!(a.collect_to::<VecDeque<i32>>(), VecDeque::from([1, 2, 3]));
/// ```
///
/// Because `collect()` only cares about what you're collecting into, you can
/// still use a partial type hint, `_`, with the turbofish:
///
/// ```
/// use cantrip::*;
/// use std::collections::{LinkedList, VecDeque};
///
/// let a = LinkedList::from([1, 2, 3]);
///
/// assert_eq!(a.collect_to::<VecDeque<_>>(), VecDeque::from([1, 2, 3]));
/// ```
///
/// Using `collect()` to make a [`String`]:
///
/// ```
/// use cantrip::*;
/// use std::collections::LinkedList;
///
/// let a = LinkedList::from(['h', 'e', 'l', 'l', 'o']);
///
/// let hello: String = a.collect_to();
///
/// assert_eq!("hello", hello);
/// ```
///
/// If you have a list of [`Result<T, E>`][`Result`]s, you can use `collect()` to
/// see if any of them failed:
///
/// ```
/// use cantrip::*;
/// use std::collections::LinkedList;
///
/// let a = LinkedList::from([Ok(1), Err("nope"), Ok(3), Err("bad")]);
///
/// let result: Result<Vec<_>, &str> = a.collect_to();
///
/// // gives us the first error
/// assert_eq!(Err("nope"), result);
///
/// let b = LinkedList::from([Ok(1), Ok(3)]);
///
/// let result: Result<Vec<_>, &str> = b.collect_to();
///
/// // gives us the list of answers
/// assert_eq!(Ok(vec![1, 3]), result);
/// ```
///
/// [`VecDeque<T>`]: ../../std/collections/struct.VecDeque.html
/// [`iter`]: Iterator::next
/// [`String`]: ../../std/string/struct.String.html
/// [`char`]: type@char
fn collect<B>(&self) -> B
where
Item: Clone,
B: FromIterator<Item>;
/// Creates a new ordered set from the elements of this collection.
///
/// This is a non-consuming equivalent of [`Iterator::collect`].
///
/// This is a non-consuming variant of [`into_bset()`].
///
/// [`into_bset()`]: crate::TransformInto::into_bset
///
/// # Example
///
/// ```
/// use cantrip::*;
/// use std::collections::{BTreeSet, LinkedList};
///
/// let a = LinkedList::from([1, 2, 3]);
///
/// assert_eq!(a.to_bset(), BTreeSet::from([1, 2, 3]));
/// ```
fn to_bset(&self) -> BTreeSet<Item>
where
Item: Ord + Clone;
/// Creates a new ordered map from the elements of this collection.
///
/// This is a non-consuming equivalent of [`Iterator::collect`].
///
/// This is a non-consuming variant of [`into_bmap()`].
///
/// [`into_bmap()`]: crate::TransformInto::into_bmap
///
/// # Example
///
/// ```
/// use cantrip::*;
/// use std::collections::{BTreeMap, LinkedList};
///
/// let a = LinkedList::from([(1, 1), (2, 2), (3, 3)]);
///
/// assert_eq!(a.to_bmap(), BTreeMap::from([
/// (1, 1),
/// (2, 2),
/// (3, 3)
/// ]));
/// ```
#[inline]
fn to_bmap<'a, K, V>(&'a self) -> BTreeMap<K, V>
where
K: Ord + Clone + 'a,
V: Clone + 'a,
Self: Iterable<Item<'a> = &'a (K, V)> + 'a,
{
self.iterator().cloned().collect()
}
/// Creates a new double-ended queue from the elements of this collection.
///
/// This is a non-consuming equivalent of [`Iterator::collect`].
///
/// This is a non-consuming variant of [`into_deque()`].
///
/// [`into_deque()`]: crate::TransformInto::into_deque
///
/// # Example
///
/// ```
/// use cantrip::*;
/// use std::collections::{LinkedList, VecDeque};
///
/// let a = LinkedList::from([1, 2, 3]);
///
/// assert_eq!(a.to_deque(), VecDeque::from([1, 2, 3]));
/// ```
fn to_deque(&self) -> VecDeque<Item>
where
Item: Clone;
/// Creates a new priority queue from the elements of this collection.
///
/// This is a non-consuming equivalent of [`Iterator::collect`].
///
/// This is a non-consuming variant of [`into_heap()`].
///
/// [`into_heap()`]: crate::TransformInto::into_heap
///
/// # Example
///
/// ```
/// use cantrip::*;
/// use std::collections::{BinaryHeap, HashSet, LinkedList};
///
/// let a = LinkedList::from([1, 2, 3]);
///
/// assert_eq!(
/// a.to_heap().into_iter().collect::<HashSet<_>>(),
/// BinaryHeap::from([1, 2, 3]).into_iter().collect()
/// );
/// ```
fn to_heap(&self) -> BinaryHeap<Item>
where
Item: Ord + Clone;
/// Creates a new doubly-linked list from the elements of this collection.
///
/// This is a non-consuming equivalent of [`Iterator::collect`].
///
/// This is a non-consuming variant of [`into_list()`].
///
/// [`into_list()`]: crate::TransformInto::into_list
///
/// # Example
///
/// ```
/// use cantrip::*;
/// use std::collections::{LinkedList, VecDeque};
///
/// let a = VecDeque::from([1, 2, 3]);
///
/// assert_eq!(a.to_list(), LinkedList::from([1, 2, 3]));
/// ```
fn to_list(&self) -> LinkedList<Item>
where
Item: Clone;
/// Creates a new hash map from the elements of this collection.
///
/// This is a non-consuming equivalent of [`Iterator::collect`].
///
/// This is a non-consuming variant of [`into_map()`].
///
/// [`into_map()`]: crate::TransformInto::into_map
///
/// # Example
///
/// ```
/// use cantrip::*;
/// use std::collections::{HashMap, LinkedList};
///
/// let a = LinkedList::from([(1, 1), (2, 2), (3, 3)]);
///
/// assert_eq!(a.to_map(), HashMap::from([
/// (1, 1),
/// (2, 2),
/// (3, 3)
/// ]));
/// ```
#[inline]
fn to_map<'a, K, V>(&'a self) -> HashMap<K, V>
where
K: Eq + Hash + Clone + 'a,
V: Clone + 'a,
Self: Iterable<Item<'a> = &'a (K, V)> + 'a,
{
self.iterator().cloned().collect()
}
/// Creates a new hash set from the elements of this collection.
///
/// This is a non-consuming equivalent of [`Iterator::collect`].
///
/// This is a non-consuming variant of [`into_set()`].
///
/// [`into_set()`]: crate::TransformInto::into_set
///
/// # Example
///
/// ```
/// use cantrip::*;
/// use std::collections::{HashSet, LinkedList};
///
/// let a = LinkedList::from([1, 2, 3]);
///
/// assert_eq!(a.to_set(), HashSet::from([1, 2, 3]));
/// ```
fn to_set(&self) -> HashSet<Item>
where
Item: Eq + Hash + Clone;
}