Add either::unwrap_{left,right}

This commit is contained in:
Ben Blum 2012-08-24 21:08:02 -04:00
parent 4cfb92f459
commit 6277046fa0

View File

@ -109,6 +109,22 @@ fn partition<T: copy, U: copy>(eithers: &[Either<T, U>])
match *eith { Right(_) => true, _ => false }
}
pure fn unwrap_left<T,U>(+eith: Either<T,U>) -> T {
//! Retrieves the value in the left branch. Fails if the either is Right.
match move eith {
Left(move x) => x, Right(_) => fail ~"either::unwrap_left Right"
}
}
pure fn unwrap_right<T,U>(+eith: Either<T,U>) -> U {
//! Retrieves the value in the right branch. Fails if the either is Left.
match move eith {
Right(move x) => x, Left(_) => fail ~"either::unwrap_right Left"
}
}
#[test]
fn test_either_left() {
let val = Left(10);