diff --git a/src/libstd/either.rs b/src/libstd/either.rs index fcbd98a79e7..4fb43e5157b 100644 --- a/src/libstd/either.rs +++ b/src/libstd/either.rs @@ -18,6 +18,7 @@ use cmp::Eq; use iterator::IteratorUtil; use result::Result; use result; +use str::StrSlice; use vec; use vec::{OwnedVector, ImmutableVector}; @@ -121,22 +122,35 @@ pub fn is_right(eith: &Either) -> bool { } } +/// Retrieves the value in the left branch. +/// Fails with a specified reason if the either is Right. +#[inline] +pub fn expect_left(eith: Either, reason: &str) -> T { + match eith { + Left(x) => x, + Right(_) => fail!(reason.to_owned()) + } +} + /// Retrieves the value in the left branch. Fails if the either is Right. #[inline] pub fn unwrap_left(eith: Either) -> T { + expect_left(eith, "either::unwrap_left Right") +} + +/// Retrieves the value in the right branch. +/// Fails with a specified reason if the either is Left. +#[inline] +pub fn expect_right(eith: Either, reason: &str) -> U { match eith { - Left(x) => x, - Right(_) => fail!("either::unwrap_left Right") + Right(x) => x, + Left(_) => fail!(reason.to_owned()) } } /// Retrieves the value in the right branch. Fails if the either is Left. -#[inline] pub fn unwrap_right(eith: Either) -> U { - match eith { - Right(x) => x, - Left(_) => fail!("either::unwrap_right Left") - } + expect_right(eith, "either::unwrap_right Left") } impl Either { @@ -157,9 +171,15 @@ impl Either { #[inline] pub fn is_right(&self) -> bool { is_right(self) } + #[inline] + pub fn expect_left(self, reason: &str) -> T { expect_left(self, reason) } + #[inline] pub fn unwrap_left(self) -> T { unwrap_left(self) } + #[inline] + pub fn expect_right(self, reason: &str) -> U { expect_right(self, reason) } + #[inline] pub fn unwrap_right(self) -> U { unwrap_right(self) } }