-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.rs
320 lines (292 loc) · 9.58 KB
/
convert.rs
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
308
309
310
311
312
313
314
315
316
317
318
319
320
use std::iter::Map;
use std::str::FromStr;
/// This trait allows to convert a stream of `str`'s, into a stream or collection of type U.
/// Return an Error when the conversion fails, but is able to produce the next value that has no Error.
/// # Examples
/// ```
/// extern crate libaoc;
///
/// use libaoc::convert::TryConvert;
///
/// fn main () {
/// let s = "1, 2, 3, 4, 5";
/// assert_eq!(vec![1, 2, 3, 4, 5], s.split(", ").try_convert().unwrap());
///
/// let s = String::from("1\n2\n3\n4\n5\n6");
/// assert_eq!(vec![1, 2, 3, 4, 5, 6], s.lines().try_convert().unwrap());
///
/// let my_str = "1, 2, 3,4, 5";
/// let mut iter = my_str.split(", ").try_convert_iter();
///
/// assert_eq!(Some(Ok(1)), iter.next());
/// assert_eq!(Some(Ok(2)), iter.next());
///
/// //the next would be 3, but that gives an error. assert_NE.
/// assert_ne!(Some(Ok(3)), iter.next());
///
/// //due to the split call, 4 isn't part of the stream, so next one is 5.
/// assert_eq!(Some(Ok(5)), iter.next());
/// }
/// ```
pub trait TryConvert<U, S, I>
where
U: FromStr,
S: AsRef<str>,
I: Iterator<Item = S>,
{
/// The Errortype that gets returned on an error.
type Error;
/// The Iterator that gets returned from [try_convert_iter()](trait.TryConvert.html#tymethod.try_convert_iter)
type Iterable: Iterator<Item = Result<U, Self::Error>>;
/// On succes, returns a vector of all completed conversions. When an error occures, returns an error instead.
fn try_convert(self) -> Result<Vec<U>, Self::Error>;
/// Tries to convert a stream of T into a slice of U.
/// On an error, returns how many items where converted.
/// # Examples
///
/// ```
/// extern crate libaoc;
///
/// use libaoc::convert::TryConvert;
///
/// fn main() {
/// let s = "1, 2, 3, 4, 5,6";
/// let mut buff = [0i64; 6];
///
/// let succeded = s.split(", ").try_convert_into_slice(&mut buff);
///
/// assert_eq!([1, 2, 3, 4, 0, 0], buff);
/// assert_eq!(Err(4), succeded);
///
/// let s = "1, 2, 3, 4, 5, 6, 7, 8, 9";
/// let written = s.split(", ").try_convert_into_slice(&mut buff);
/// assert_eq!(Ok(6), written);
///
/// let written = s.split(", ").take(2).try_convert_into_slice(&mut buff);
/// assert_eq!(Ok(2), written);
/// }
/// ```
fn try_convert_into_slice(self, slice: &mut [U]) -> Result<usize, usize>;
/// Returns an iterator over the converted items. Returns an error if an item can not be converted. Continue's after the error.
fn try_convert_iter(self) -> Self::Iterable;
}
type MapConvert<I, S, U, E> = Map<I, fn(S) -> Result<U, E>>;
impl<U, S, I> TryConvert<U, S, I> for I
where
U: FromStr,
S: AsRef<str>,
I: Iterator<Item = S>,
{
type Error = <U as FromStr>::Err;
type Iterable = MapConvert<I, S, U, Self::Error>;
#[inline]
fn try_convert(self) -> Result<Vec<U>, Self::Error> {
self.try_convert_iter().collect()
}
#[inline]
fn try_convert_into_slice(self, slice: &mut [U]) -> Result<usize, usize> {
let mut number_of_writes = 0;
for (dst, src) in slice.iter_mut().zip(self.try_convert_iter()) {
if let Ok(converted) = src {
*dst = converted;
number_of_writes += 1;
} else {
return Err(number_of_writes);
}
}
Ok(number_of_writes)
}
#[inline]
fn try_convert_iter(self) -> Self::Iterable {
self.map(|item| item.as_ref().parse())
}
}
/// This trait allows to convert a stream with items of type T into a stream or collection with items of type U.
///
/// # Examples
/// ```
/// extern crate libaoc;
///
/// use libaoc::convert::Convert;
/// use libaoc::movement::Position;
///
/// fn main() {
/// let tuple1 = (0, 0);
/// let tuple2 = (1, 1);
/// let tuple3 = (2, 2);
///
/// let tuples = vec![tuple1, tuple2, tuple3];
///
/// let Positions: Vec<Position<usize>> = tuples.into_iter().convert_into_vec();
///
/// assert_eq!(vec![Position::new(0, 0), Position::new(1, 1), Position::new(2, 2)], Positions);
///
/// let tups = vec![(4, 4), (5, 5), (3, 4)];
///
/// let mut convert_iter = tups.into_iter().rev().convert_iter();
///
/// assert_eq!(Some(Position::new(3, 4)), convert_iter.next());
/// }
/// ```
pub trait Convert<T, U, I>
where
U: From<T>,
I: Iterator<Item = T>,
{
/// The Iterator that gets returned from [convert_iter()](trait.Convert.html#tymethod.convert_iter)
type Iterable: Iterator<Item = U>;
/// Returns a vector of all completed conversions.
fn convert_into_vec(self) -> Vec<U>;
/// Converts the stream, and writes the items into `slice`. Returns how many elements where written to the slice.
///
/// # Examples
/// ```
/// extern crate libaoc;
///
/// use libaoc::convert::Convert;
///
/// fn main() {
/// let chars = vec![97, 98, 99, 100, 101];
/// let mut slice: [char; 5] = ['-'; 5];
///
/// let written = chars.into_iter().convert_into_slice(&mut slice);
///
/// assert_eq!(['a', 'b', 'c', 'd', 'e'], slice);
/// assert_eq!(5, written);
/// }
/// ```
fn convert_into_slice(self, slice: &mut [U]) -> usize;
/// Returns an iterator that performs the conversions.
fn convert_iter(self) -> Self::Iterable;
}
impl<T, U, I> Convert<T, U, I> for I
where
U: From<T>,
I: Iterator<Item = T>,
{
type Iterable = Map<I, fn(T) -> U>;
#[inline]
fn convert_into_vec(self) -> Vec<U> {
self.convert_iter().collect()
}
#[inline]
fn convert_into_slice(self, slice: &mut [U]) -> usize {
slice
.iter_mut()
.zip(self.convert_iter())
.map(|(dst, src)| *dst = src)
.count()
}
#[inline]
fn convert_iter(self) -> Self::Iterable {
self.map(U::from)
}
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum FillError {
FillError,
}
impl ::std::error::Error for FillError {
#[inline]
fn description(&self) -> &str {
match *self {
FillError::FillError => "The array was partially filled, and therefore dropped.",
}
}
}
impl ::std::fmt::Display for FillError {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}", self)
}
}
/// This macro makes it easy to convert an Iterator into an array.
/// The `type` of the array has to be specified when this macro is called.
///
/// The array that get's build uses mem::unitialized to prevent unnecessary allocation,
/// however if the Iterator has less items than the lenght of the array, this means there is still
/// unitialized memory. In this case, the macro will return an error, and drop the array that was build.
/// # Examples
/// ```
/// #[macro_use]
/// extern crate libaoc;
///
/// use libaoc::movement::Position;
/// use libaoc::convert::Convert;
///
/// #[derive(Debug, PartialEq)]
/// struct noncopy{item: i64}
///
/// impl From<i64> for noncopy {
/// fn from(num: i64) -> noncopy {
/// noncopy{item: num}
/// }
/// }
/// fn main() {
///
/// let ss = vec![1, 2, 3];
/// let arr = arraycollect!(ss.into_iter().convert_iter() => [noncopy; 2]);
/// assert_eq!(Ok([noncopy{item: 1}, noncopy{item: 2}]), arr);
/// }
/// ```
#[macro_export]
macro_rules! arraycollect {
($iter:expr => [$tgt:ty; $num:tt]) => (
{
use ::std::mem;
struct PartialArray<T> {
data: mem::ManuallyDrop<[T; $num]>,
fill: usize,
}
impl <T>PartialArray<T> {
#[inline]
fn new() -> PartialArray<T> {
unsafe {
PartialArray {
data: mem::ManuallyDrop::new(mem::uninitialized()),
fill: 0,
}
}
}
#[inline]
fn fill_array<I: Iterator<Item = T>>(mut self, iter: I) -> Result<[T; $num], $crate::convert::FillError>
{
for (dst, src) in self.data.iter_mut().zip(iter) {
unsafe {
::std::ptr::write(dst, src);
}
self.fill += 1;
}
//if the number of items filled is not equal to the number of items that should have been written,
//return an error.
if self.fill != $num {
Err($crate::convert::FillError::FillError)
}
else {
Ok(self.finish())
}
}
#[inline]
fn finish(mut self) -> [T; $num] {
unsafe {
let rd = ::std::ptr::read(&mut self.data);
let ret = mem::ManuallyDrop::into_inner(rd);
mem::forget(self);
ret
}
}
}
impl <T>Drop for PartialArray<T> {
#[inline]
fn drop(&mut self) {
unsafe {
::std::ptr::drop_in_place::<[T]>(&mut self.data[0..self.fill]);
}
}
}
//pass in $tgt as generic paremeter, fill_array takes an array with items of $tgt.
let array: PartialArray<$tgt> = PartialArray::new();
array.fill_array($iter)
}
)
}