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
use std::marker::PhantomData;
use {IntoTransaction, Transaction};
pub fn or_else<Ctx, A, F, B>(a: A, f: F) -> OrElse<A::Tx, F, B>
where
A: IntoTransaction<Ctx>,
B: IntoTransaction<Ctx, Item = A::Item>,
F: Fn(A::Err) -> B,
{
OrElse {
tx: a.into_transaction(),
f: f,
_phantom: PhantomData,
}
}
#[derive(Debug)]
#[must_use]
pub struct OrElse<Tx1, F, Tx2> {
tx: Tx1,
f: F,
_phantom: PhantomData<Tx2>,
}
impl<Tx, Tx2, F> Transaction for OrElse<Tx, F, Tx2>
where
Tx2: IntoTransaction<
Tx::Ctx,
Item = Tx::Item,
Err = Tx::Err,
>,
Tx: Transaction,
F: Fn(Tx::Err) -> Tx2,
{
type Ctx = Tx::Ctx;
type Item = Tx::Item;
type Err = Tx::Err;
fn run(&self, ctx: &mut Self::Ctx) -> Result<Self::Item, Self::Err> {
let &OrElse { ref tx, ref f, .. } = self;
tx.run(ctx).or_else(
|item| f(item).into_transaction().run(ctx),
)
}
}