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
//! A collection of various utility helpers.

mod comparison;
pub use self::comparison::compare_floats;
pub use self::comparison::compare_shared;

mod iterators;
pub use self::iterators::CollectGroupBy;

mod mutability;
pub use self::mutability::*;

mod parallel;
pub use self::parallel::*;

mod random;
pub use self::random::DefaultRandom;
pub use self::random::Random;

mod time_quota;
pub use self::time_quota::TimeQuota;

mod timing;
pub use self::timing::Timer;

/// A trait to get tuple from collection items.
/// See https://stackoverflow.com/questions/38863781/how-to-create-a-tuple-from-a-vector
pub trait TryCollect<T> {
    fn try_collect(&mut self) -> Option<T>;
}

#[macro_export]
macro_rules! impl_try_collect_tuple {
    () => { };
    ($A:ident $($I:ident)*) => {
        impl_try_collect_tuple!($($I)*);

        impl<$A: Iterator> TryCollect<($A::Item, $($I::Item),*)> for $A {
            fn try_collect(&mut self) -> Option<($A::Item, $($I::Item),*)> {
                let r = (try_opt!(self.next()),
                         // hack: we need to use $I in the expansion
                         $({ let a: $I::Item = try_opt!(self.next()); a}),* );
                Some(r)
            }
        }
    }
}

#[macro_export]
macro_rules! try_opt {
    ($e:expr) => {
        match $e {
            Some(e) => e,
            None => return None,
        }
    };
}

// implement TryCollect<T> where T is a tuple with size 1, 2, .., 10
impl_try_collect_tuple!(A A A A A A A A A A);