diff options
Diffstat (limited to 'crates/ra_hir/src/either.rs')
-rw-r--r-- | crates/ra_hir/src/either.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/crates/ra_hir/src/either.rs b/crates/ra_hir/src/either.rs new file mode 100644 index 000000000..4073cc82e --- /dev/null +++ b/crates/ra_hir/src/either.rs | |||
@@ -0,0 +1,28 @@ | |||
1 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
2 | pub enum Either<A, B> { | ||
3 | A(A), | ||
4 | B(B), | ||
5 | } | ||
6 | |||
7 | impl<A, B> Either<A, B> { | ||
8 | pub fn either<R, F1, F2>(self, f1: F1, f2: F2) -> R | ||
9 | where | ||
10 | F1: FnOnce(A) -> R, | ||
11 | F2: FnOnce(B) -> R, | ||
12 | { | ||
13 | match self { | ||
14 | Either::A(a) => f1(a), | ||
15 | Either::B(b) => f2(b), | ||
16 | } | ||
17 | } | ||
18 | pub fn map<U, V, F1, F2>(self, f1: F1, f2: F2) -> Either<U, V> | ||
19 | where | ||
20 | F1: FnOnce(A) -> U, | ||
21 | F2: FnOnce(B) -> V, | ||
22 | { | ||
23 | match self { | ||
24 | Either::A(a) => Either::A(f1(a)), | ||
25 | Either::B(b) => Either::B(f2(b)), | ||
26 | } | ||
27 | } | ||
28 | } | ||