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
use std::marker::PhantomData;

use {IntoTransaction, Transaction};

pub fn repeat<Ctx, F, Tx>(n: usize, f: F) -> Repeat<Ctx, F, Tx>
where
    Tx: IntoTransaction<Ctx>,
    F: Fn(usize) -> Tx,
{
    Repeat {
        n: n,
        f: f,
        _phantom: PhantomData,
    }
}

/// The result of `repeat`
#[derive(Debug)]
#[must_use]
pub struct Repeat<Ctx, F, Tx> {
    n: usize,
    f: F,
    _phantom: PhantomData<(Tx, Ctx)>,
}

impl<Ctx, F, Tx> Transaction for Repeat<Ctx, F, Tx>
where
    F: Fn(usize) -> Tx,
    Tx: IntoTransaction<Ctx>,
{
    type Ctx = Ctx;
    type Item = Vec<Tx::Item>;
    type Err = Tx::Err;
    fn run(&self, ctx: &mut Self::Ctx) -> Result<Self::Item, Self::Err> {
        let Repeat { ref n, ref f, .. } = *self;
        let mut ret = Vec::new();
        for i in 0..*n {
            let t = f(i).into_transaction().run(ctx)?;
            ret.push(t);
        }
        Ok(ret)
    }
}