From 16e620c052016010b2f17070a98bdc1e7e849ab3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 16:12:55 +0300 Subject: move raw_items to hir_def --- crates/ra_hir_def/src/either.rs | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 crates/ra_hir_def/src/either.rs (limited to 'crates/ra_hir_def/src/either.rs') diff --git a/crates/ra_hir_def/src/either.rs b/crates/ra_hir_def/src/either.rs new file mode 100644 index 000000000..83583ef8b --- /dev/null +++ b/crates/ra_hir_def/src/either.rs @@ -0,0 +1,54 @@ +//! FIXME: write short doc here + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Either { + A(A), + B(B), +} + +impl Either { + pub fn either(self, f1: F1, f2: F2) -> R + where + F1: FnOnce(A) -> R, + F2: FnOnce(B) -> R, + { + match self { + Either::A(a) => f1(a), + Either::B(b) => f2(b), + } + } + pub fn map(self, f1: F1, f2: F2) -> Either + where + F1: FnOnce(A) -> U, + F2: FnOnce(B) -> V, + { + match self { + Either::A(a) => Either::A(f1(a)), + Either::B(b) => Either::B(f2(b)), + } + } + pub fn map_a(self, f: F) -> Either + where + F: FnOnce(A) -> U, + { + self.map(f, |it| it) + } + pub fn a(self) -> Option { + match self { + Either::A(it) => Some(it), + Either::B(_) => None, + } + } + pub fn b(self) -> Option { + match self { + Either::A(_) => None, + Either::B(it) => Some(it), + } + } + pub fn as_ref(&self) -> Either<&A, &B> { + match self { + Either::A(it) => Either::A(it), + Either::B(it) => Either::B(it), + } + } +} -- cgit v1.2.3