Skip to content

Commit 200602f

Browse files
committed
---
yaml --- r: 80729 b: refs/heads/try c: e03d60e h: refs/heads/master i: 80727: 6d33f0b v: v3
1 parent 4483e76 commit 200602f

File tree

4 files changed

+163
-22
lines changed

4 files changed

+163
-22
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 4c6bf4872012c010f84dc7fa2cdfe87522533f89
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cbd1eefbd350797b783df119fed7956d7e1c74ad
5-
refs/heads/try: 12e0d7ecf061313d02a4647db8c1b30aad2ae53d
5+
refs/heads/try: e03d60e9ebf2dbc2d18ab9919f905c17b967fcde
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/either.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ impl<L, R> Either<L, R> {
105105
}
106106
}
107107

108+
/// A generic trait for converting a value to a `Either`
109+
pub trait ToEither<L, R> {
110+
/// Convert to the `either` type
111+
fn to_either(&self) -> Either<L, R>;
112+
}
113+
114+
/// A generic trait for converting a value to a `Either`
115+
pub trait IntoEither<L, R> {
116+
/// Convert to the `either` type
117+
fn into_either(self) -> Either<L, R>;
118+
}
119+
120+
/// A generic trait for converting a value to a `Either`
121+
pub trait AsEither<L, R> {
122+
/// Convert to the `either` type
123+
fn as_either<'a>(&'a self) -> Either<&'a L, &'a R>;
124+
}
125+
108126
impl<L, R: Clone> option::ToOption<R> for Either<L, R> {
109127
#[inline]
110128
fn to_option(&self)-> option::Option<R> {
@@ -165,6 +183,23 @@ impl<L, R> result::AsResult<R, L> for Either<L, R> {
165183
}
166184
}
167185

186+
impl<L: Clone, R: Clone> ToEither<L, R> for Either<L, R> {
187+
fn to_either(&self) -> Either<L, R> { self.clone() }
188+
}
189+
190+
impl<L, R> IntoEither<L, R> for Either<L, R> {
191+
fn into_either(self) -> Either<L, R> { self }
192+
}
193+
194+
impl<L, R> AsEither<L, R> for Either<L, R> {
195+
fn as_either<'a>(&'a self) -> Either<&'a L, &'a R> {
196+
match *self {
197+
Left(ref l) => Left(l),
198+
Right(ref r) => Right(r),
199+
}
200+
}
201+
}
202+
168203
/// An iterator yielding the `Left` values of its source
169204
pub type Lefts<L, R, Iter> = FilterMap<'static, Either<L, R>, L, Iter>;
170205

@@ -370,4 +405,31 @@ mod tests {
370405
let x = 404;
371406
assert_eq!(left.as_result(), result::Err(&x));
372407
}
408+
409+
#[test]
410+
pub fn test_to_either() {
411+
let right: Either<int, int> = Right(100);
412+
let left: Either<int, int> = Left(404);
413+
414+
assert_eq!(right.to_either(), Right(100));
415+
assert_eq!(left.to_either(), Left(404));
416+
}
417+
418+
#[test]
419+
pub fn test_into_either() {
420+
let right: Either<int, int> = Right(100);
421+
let left: Either<int, int> = Left(404);
422+
423+
assert_eq!(right.into_either(), Right(100));
424+
assert_eq!(left.into_either(), Left(404));
425+
}
426+
427+
#[test]
428+
pub fn test_as_either() {
429+
let right: Either<int, int> = Right(100);
430+
let left: Either<int, int> = Left(404);
431+
432+
assert_eq!(right.as_either().unwrap_right(), &100);
433+
assert_eq!(left.as_either().unwrap_left(), &404);
434+
}
373435
}

branches/try/src/libstd/option.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ let unwrapped_msg = match msg {
4444
use clone::Clone;
4545
use cmp::{Eq,Ord};
4646
use default::Default;
47+
use either;
4748
use util;
4849
use num::Zero;
4950
use iter;
@@ -447,6 +448,26 @@ impl<T> result::IntoResult<T, ()> for Option<T> {
447448
}
448449
}
449450

451+
impl<T: Clone> either::ToEither<(), T> for Option<T> {
452+
#[inline]
453+
fn to_either(&self) -> either::Either<(), T> {
454+
match *self {
455+
Some(ref x) => either::Right(x.clone()),
456+
None => either::Left(()),
457+
}
458+
}
459+
}
460+
461+
impl<T> either::IntoEither<(), T> for Option<T> {
462+
#[inline]
463+
fn into_either(self) -> either::Either<(), T> {
464+
match self {
465+
Some(x) => either::Right(x),
466+
None => either::Left(()),
467+
}
468+
}
469+
}
470+
450471
impl<T: Default> Option<T> {
451472
/// Returns the contained value or default (for this type)
452473
#[inline]
@@ -529,6 +550,9 @@ impl<A> ExactSize<A> for OptionIterator<A> {}
529550
#[cfg(test)]
530551
mod tests {
531552
use super::*;
553+
554+
use either::{IntoEither, ToEither};
555+
use either;
532556
use result::{IntoResult, ToResult};
533557
use result;
534558
use util;
@@ -817,4 +841,22 @@ mod tests {
817841
assert_eq!(some.into_result(), result::Ok(100));
818842
assert_eq!(none.into_result(), result::Err(()));
819843
}
844+
845+
#[test]
846+
pub fn test_to_either() {
847+
let some: Option<int> = Some(100);
848+
let none: Option<int> = None;
849+
850+
assert_eq!(some.to_either(), either::Right(100));
851+
assert_eq!(none.to_either(), either::Left(()));
852+
}
853+
854+
#[test]
855+
pub fn test_into_either() {
856+
let some: Option<int> = Some(100);
857+
let none: Option<int> = None;
858+
859+
assert_eq!(some.into_either(), either::Right(100));
860+
assert_eq!(none.into_either(), either::Left(()));
861+
}
820862
}

branches/try/src/libstd/result.rs

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ pub enum Result<T, E> {
3737
}
3838

3939
impl<T, E: ToStr> Result<T, E> {
40-
/// Convert to the `either` type
41-
///
42-
/// `Ok` result variants are converted to `either::Right` variants, `Err`
43-
/// result variants are converted to `either::Left`.
44-
#[inline]
45-
pub fn to_either(self)-> either::Either<E, T>{
46-
match self {
47-
Ok(t) => either::Right(t),
48-
Err(e) => either::Left(e),
49-
}
50-
}
51-
5240
/// Get a reference to the value out of a successful result
5341
///
5442
/// # Failure
@@ -324,6 +312,36 @@ impl<T, E> AsResult<T, E> for Result<T, E> {
324312
}
325313
}
326314

315+
impl<T: Clone, E: Clone> either::ToEither<E, T> for Result<T, E> {
316+
#[inline]
317+
fn to_either(&self)-> either::Either<E, T> {
318+
match *self {
319+
Ok(ref t) => either::Right(t.clone()),
320+
Err(ref e) => either::Left(e.clone()),
321+
}
322+
}
323+
}
324+
325+
impl<T, E> either::IntoEither<E, T> for Result<T, E> {
326+
#[inline]
327+
fn into_either(self)-> either::Either<E, T> {
328+
match self {
329+
Ok(t) => either::Right(t),
330+
Err(e) => either::Left(e),
331+
}
332+
}
333+
}
334+
335+
impl<T, E> either::AsEither<E, T> for Result<T, E> {
336+
#[inline]
337+
fn as_either<'a>(&'a self)-> either::Either<&'a E, &'a T> {
338+
match *self {
339+
Ok(ref t) => either::Right(t),
340+
Err(ref e) => either::Left(e),
341+
}
342+
}
343+
}
344+
327345
#[inline]
328346
#[allow(missing_doc)]
329347
pub fn map_opt<T, U: ToStr, V>(o_t: &Option<T>,
@@ -403,6 +421,7 @@ pub fn fold_<T, E, Iter: Iterator<Result<T, E>>>(
403421
mod tests {
404422
use super::*;
405423

424+
use either::{IntoEither, ToEither, AsEither};
406425
use either;
407426
use iter::range;
408427
use option::{IntoOption, ToOption, AsOption};
@@ -483,15 +502,6 @@ mod tests {
483502
assert_eq!(*foo.get_ref(), 100);
484503
}
485504

486-
#[test]
487-
pub fn test_to_either() {
488-
let r: Result<int, ()> = Ok(100);
489-
let err: Result<(), int> = Err(404);
490-
491-
assert_eq!(r.to_either(), either::Right(100));
492-
assert_eq!(err.to_either(), either::Left(404));
493-
}
494-
495505
#[test]
496506
fn test_collect() {
497507
assert_eq!(collect(range(0, 0)
@@ -588,4 +598,31 @@ mod tests {
588598
let x = 404;
589599
assert_eq!(err.as_result(), Err(&x));
590600
}
601+
602+
#[test]
603+
pub fn test_to_either() {
604+
let ok: Result<int, int> = Ok(100);
605+
let err: Result<int, int> = Err(404);
606+
607+
assert_eq!(ok.to_either(), either::Right(100));
608+
assert_eq!(err.to_either(), either::Left(404));
609+
}
610+
611+
#[test]
612+
pub fn test_into_either() {
613+
let ok: Result<int, int> = Ok(100);
614+
let err: Result<int, int> = Err(404);
615+
616+
assert_eq!(ok.into_either(), either::Right(100));
617+
assert_eq!(err.into_either(), either::Left(404));
618+
}
619+
620+
#[test]
621+
pub fn test_as_either() {
622+
let ok: Result<int, int> = Ok(100);
623+
let err: Result<int, int> = Err(404);
624+
625+
assert_eq!(ok.as_either().unwrap_right(), &100);
626+
assert_eq!(err.as_either().unwrap_left(), &404);
627+
}
591628
}

0 commit comments

Comments
 (0)