Skip to content

Commit 4483e76

Browse files
committed
---
yaml --- r: 80728 b: refs/heads/try c: 12e0d7e h: refs/heads/master v: v3
1 parent 6d33f0b commit 4483e76

File tree

4 files changed

+172
-13
lines changed

4 files changed

+172
-13
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: ff34740a29e3ba4f8c34fd05badf798e481e2257
5+
refs/heads/try: 12e0d7ecf061313d02a4647db8c1b30aad2ae53d
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ impl<L, R> Either<L, R> {
5454
}
5555
}
5656

57-
/// Converts a `Either` to a `Result`
58-
///
59-
/// Converts an `Either` type to a `Result` type, making the "right" choice
60-
/// an `Ok` result, and the "left" choice a `Err`
61-
#[inline]
62-
pub fn to_result(self) -> Result<R, L> {
63-
match self {
64-
Right(r) => result::Ok(r),
65-
Left(l) => result::Err(l)
66-
}
67-
}
68-
6957
/// Checks whether the given value is a `Left`
7058
#[inline]
7159
pub fn is_left(&self) -> bool {
@@ -147,6 +135,36 @@ impl<L, R> option::AsOption<R> for Either<L, R> {
147135
}
148136
}
149137

138+
impl<L: Clone, R: Clone> result::ToResult<R, L> for Either<L, R> {
139+
#[inline]
140+
fn to_result(&self)-> result::Result<R, L> {
141+
match *self {
142+
Left(ref l) => result::Err(l.clone()),
143+
Right(ref r) => result::Ok(r.clone()),
144+
}
145+
}
146+
}
147+
148+
impl<L, R> result::IntoResult<R, L> for Either<L, R> {
149+
#[inline]
150+
fn into_result(self)-> result::Result<R, L> {
151+
match self {
152+
Left(l) => result::Err(l),
153+
Right(r) => result::Ok(r),
154+
}
155+
}
156+
}
157+
158+
impl<L, R> result::AsResult<R, L> for Either<L, R> {
159+
#[inline]
160+
fn as_result<'a>(&'a self) -> result::Result<&'a R, &'a L> {
161+
match *self {
162+
Left(ref l) => result::Err(l),
163+
Right(ref r) => result::Ok(r),
164+
}
165+
}
166+
}
167+
150168
/// An iterator yielding the `Left` values of its source
151169
pub type Lefts<L, R, Iter> = FilterMap<'static, Either<L, R>, L, Iter>;
152170

@@ -200,6 +218,8 @@ mod tests {
200218

201219
use option::{IntoOption, ToOption, AsOption};
202220
use option;
221+
use result::{IntoResult, ToResult, AsResult};
222+
use result;
203223

204224
#[test]
205225
fn test_either_left() {
@@ -320,4 +340,34 @@ mod tests {
320340
assert_eq!(right.as_option().unwrap(), &100);
321341
assert_eq!(left.as_option(), option::None);
322342
}
343+
344+
#[test]
345+
pub fn test_to_result() {
346+
let right: Either<int, int> = Right(100);
347+
let left: Either<int, int> = Left(404);
348+
349+
assert_eq!(right.to_result(), result::Ok(100));
350+
assert_eq!(left.to_result(), result::Err(404));
351+
}
352+
353+
#[test]
354+
pub fn test_into_result() {
355+
let right: Either<int, int> = Right(100);
356+
let left: Either<int, int> = Left(404);
357+
358+
assert_eq!(right.into_result(), result::Ok(100));
359+
assert_eq!(left.into_result(), result::Err(404));
360+
}
361+
362+
#[test]
363+
pub fn test_as_result() {
364+
let right: Either<int, int> = Right(100);
365+
let left: Either<int, int> = Left(404);
366+
367+
let x = 100;
368+
assert_eq!(right.as_result(), result::Ok(&x));
369+
370+
let x = 404;
371+
assert_eq!(left.as_result(), result::Err(&x));
372+
}
323373
}

branches/try/src/libstd/option.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use util;
4848
use num::Zero;
4949
use iter;
5050
use iter::{Iterator, DoubleEndedIterator, ExactSize};
51+
use result;
5152
use str::{StrSlice, OwnedStr};
5253
use to_str::ToStr;
5354
use clone::DeepClone;
@@ -426,6 +427,26 @@ impl<T> AsOption<T> for Option<T> {
426427
}
427428
}
428429

430+
impl<T: Clone> result::ToResult<T, ()> for Option<T> {
431+
#[inline]
432+
fn to_result(&self) -> result::Result<T, ()> {
433+
match *self {
434+
Some(ref x) => result::Ok(x.clone()),
435+
None => result::Err(()),
436+
}
437+
}
438+
}
439+
440+
impl<T> result::IntoResult<T, ()> for Option<T> {
441+
#[inline]
442+
fn into_result(self) -> result::Result<T, ()> {
443+
match self {
444+
Some(x) => result::Ok(x),
445+
None => result::Err(()),
446+
}
447+
}
448+
}
449+
429450
impl<T: Default> Option<T> {
430451
/// Returns the contained value or default (for this type)
431452
#[inline]
@@ -508,6 +529,8 @@ impl<A> ExactSize<A> for OptionIterator<A> {}
508529
#[cfg(test)]
509530
mod tests {
510531
use super::*;
532+
use result::{IntoResult, ToResult};
533+
use result;
511534
use util;
512535

513536
#[test]
@@ -776,4 +799,22 @@ mod tests {
776799
assert_eq!(some.as_option().unwrap(), &100);
777800
assert_eq!(none.as_option(), None);
778801
}
802+
803+
#[test]
804+
pub fn test_to_result() {
805+
let some: Option<int> = Some(100);
806+
let none: Option<int> = None;
807+
808+
assert_eq!(some.to_result(), result::Ok(100));
809+
assert_eq!(none.to_result(), result::Err(()));
810+
}
811+
812+
#[test]
813+
pub fn test_into_result() {
814+
let some: Option<int> = Some(100);
815+
let none: Option<int> = None;
816+
817+
assert_eq!(some.into_result(), result::Ok(100));
818+
assert_eq!(none.into_result(), result::Err(()));
819+
}
779820
}

branches/try/src/libstd/result.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,24 @@ impl<T, E: Clone + ToStr> Result<T, E> {
256256
}
257257
}
258258

259+
/// A generic trait for converting a value to a `Result`
260+
pub trait ToResult<T, E> {
261+
/// Convert to the `result` type
262+
fn to_result(&self) -> Result<T, E>;
263+
}
264+
265+
/// A generic trait for converting a value to a `Result`
266+
pub trait IntoResult<T, E> {
267+
/// Convert to the `result` type
268+
fn into_result(self) -> Result<T, E>;
269+
}
270+
271+
/// A generic trait for converting a value to a `Result`
272+
pub trait AsResult<T, E> {
273+
/// Convert to the `result` type
274+
fn as_result<'a>(&'a self) -> Result<&'a T, &'a E>;
275+
}
276+
259277
impl<T: Clone, E> option::ToOption<T> for Result<T, E> {
260278
#[inline]
261279
fn to_option(&self)-> Option<T> {
@@ -286,6 +304,26 @@ impl<T, E> option::AsOption<T> for Result<T, E> {
286304
}
287305
}
288306

307+
impl<T: Clone, E: Clone> ToResult<T, E> for Result<T, E> {
308+
#[inline]
309+
fn to_result(&self) -> Result<T, E> { self.clone() }
310+
}
311+
312+
impl<T, E> IntoResult<T, E> for Result<T, E> {
313+
#[inline]
314+
fn into_result(self) -> Result<T, E> { self }
315+
}
316+
317+
impl<T, E> AsResult<T, E> for Result<T, E> {
318+
#[inline]
319+
fn as_result<'a>(&'a self) -> Result<&'a T, &'a E> {
320+
match *self {
321+
Ok(ref t) => Ok(t),
322+
Err(ref e) => Err(e),
323+
}
324+
}
325+
}
326+
289327
#[inline]
290328
#[allow(missing_doc)]
291329
pub fn map_opt<T, U: ToStr, V>(o_t: &Option<T>,
@@ -520,4 +558,34 @@ mod tests {
520558
assert_eq!(ok.as_option().unwrap(), &100);
521559
assert_eq!(err.as_option(), option::None);
522560
}
561+
562+
#[test]
563+
pub fn test_to_result() {
564+
let ok: Result<int, int> = Ok(100);
565+
let err: Result<int, int> = Err(404);
566+
567+
assert_eq!(ok.to_result(), Ok(100));
568+
assert_eq!(err.to_result(), Err(404));
569+
}
570+
571+
#[test]
572+
pub fn test_into_result() {
573+
let ok: Result<int, int> = Ok(100);
574+
let err: Result<int, int> = Err(404);
575+
576+
assert_eq!(ok.into_result(), Ok(100));
577+
assert_eq!(err.into_result(), Err(404));
578+
}
579+
580+
#[test]
581+
pub fn test_as_result() {
582+
let ok: Result<int, int> = Ok(100);
583+
let err: Result<int, int> = Err(404);
584+
585+
let x = 100;
586+
assert_eq!(ok.as_result(), Ok(&x));
587+
588+
let x = 404;
589+
assert_eq!(err.as_result(), Err(&x));
590+
}
523591
}

0 commit comments

Comments
 (0)