about summary refs log tree commit diff
path: root/src/libstd/either.rs
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2013-09-11 09:33:45 -0700
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2013-09-12 18:54:12 -0700
commite03d60e9ebf2dbc2d18ab9919f905c17b967fcde (patch)
treed80ca6146eb66e930b1063c6929bb1b09e90b1e6 /src/libstd/either.rs
parent12e0d7ecf061313d02a4647db8c1b30aad2ae53d (diff)
downloadrust-e03d60e9ebf2dbc2d18ab9919f905c17b967fcde.tar.gz
rust-e03d60e9ebf2dbc2d18ab9919f905c17b967fcde.zip
std: Add ToEither/IntoEither/AsEither
Diffstat (limited to 'src/libstd/either.rs')
-rw-r--r--src/libstd/either.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/libstd/either.rs b/src/libstd/either.rs
index 526a5380dfb..27381f64ad4 100644
--- a/src/libstd/either.rs
+++ b/src/libstd/either.rs
@@ -105,6 +105,24 @@ impl<L, R> Either<L, R> {
     }
 }
 
+/// A generic trait for converting a value to a `Either`
+pub trait ToEither<L, R> {
+    /// Convert to the `either` type
+    fn to_either(&self) -> Either<L, R>;
+}
+
+/// A generic trait for converting a value to a `Either`
+pub trait IntoEither<L, R> {
+    /// Convert to the `either` type
+    fn into_either(self) -> Either<L, R>;
+}
+
+/// A generic trait for converting a value to a `Either`
+pub trait AsEither<L, R> {
+    /// Convert to the `either` type
+    fn as_either<'a>(&'a self) -> Either<&'a L, &'a R>;
+}
+
 impl<L, R: Clone> option::ToOption<R> for Either<L, R> {
     #[inline]
     fn to_option(&self)-> option::Option<R> {
@@ -165,6 +183,23 @@ impl<L, R> result::AsResult<R, L> for Either<L, R> {
     }
 }
 
+impl<L: Clone, R: Clone> ToEither<L, R> for Either<L, R> {
+    fn to_either(&self) -> Either<L, R> { self.clone() }
+}
+
+impl<L, R> IntoEither<L, R> for Either<L, R> {
+    fn into_either(self) -> Either<L, R> { self }
+}
+
+impl<L, R> AsEither<L, R> for Either<L, R> {
+    fn as_either<'a>(&'a self) -> Either<&'a L, &'a R> {
+        match *self {
+            Left(ref l) => Left(l),
+            Right(ref r) => Right(r),
+        }
+    }
+}
+
 /// An iterator yielding the `Left` values of its source
 pub type Lefts<L, R, Iter> = FilterMap<'static, Either<L, R>, L, Iter>;
 
@@ -370,4 +405,31 @@ mod tests {
         let x = 404;
         assert_eq!(left.as_result(), result::Err(&x));
     }
+
+    #[test]
+    pub fn test_to_either() {
+        let right: Either<int, int> = Right(100);
+        let left: Either<int, int> = Left(404);
+
+        assert_eq!(right.to_either(), Right(100));
+        assert_eq!(left.to_either(), Left(404));
+    }
+
+    #[test]
+    pub fn test_into_either() {
+        let right: Either<int, int> = Right(100);
+        let left: Either<int, int> = Left(404);
+
+        assert_eq!(right.into_either(), Right(100));
+        assert_eq!(left.into_either(), Left(404));
+    }
+
+    #[test]
+    pub fn test_as_either() {
+        let right: Either<int, int> = Right(100);
+        let left: Either<int, int> = Left(404);
+
+        assert_eq!(right.as_either().unwrap_right(), &100);
+        assert_eq!(left.as_either().unwrap_left(), &404);
+    }
 }