Skip to content

Commit 6d33f0b

Browse files
committed
---
yaml --- r: 80727 b: refs/heads/try c: ff34740 h: refs/heads/master i: 80725: 1575717 80723: 9419308 80719: b0a358b v: v3
1 parent b028624 commit 6d33f0b

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
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: b8a284e873125097dcfa5cec5d7135789d6673b3
5+
refs/heads/try: ff34740a29e3ba4f8c34fd05badf798e481e2257
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#[allow(missing_doc)];
1414

1515
use option::{Some, None};
16+
use option;
1617
use clone::Clone;
1718
use container::Container;
1819
use cmp::Eq;
@@ -116,6 +117,36 @@ impl<L, R> Either<L, R> {
116117
}
117118
}
118119

120+
impl<L, R: Clone> option::ToOption<R> for Either<L, R> {
121+
#[inline]
122+
fn to_option(&self)-> option::Option<R> {
123+
match *self {
124+
Left(_) => None,
125+
Right(ref r) => Some(r.clone()),
126+
}
127+
}
128+
}
129+
130+
impl<L, R> option::IntoOption<R> for Either<L, R> {
131+
#[inline]
132+
fn into_option(self)-> option::Option<R> {
133+
match self {
134+
Left(_) => None,
135+
Right(r) => Some(r),
136+
}
137+
}
138+
}
139+
140+
impl<L, R> option::AsOption<R> for Either<L, R> {
141+
#[inline]
142+
fn as_option<'a>(&'a self) -> option::Option<&'a R> {
143+
match *self {
144+
Left(_) => None,
145+
Right(ref r) => Some(r),
146+
}
147+
}
148+
}
149+
119150
/// An iterator yielding the `Left` values of its source
120151
pub type Lefts<L, R, Iter> = FilterMap<'static, Either<L, R>, L, Iter>;
121152

@@ -167,6 +198,9 @@ pub fn partition<L, R>(eithers: ~[Either<L, R>]) -> (~[L], ~[R]) {
167198
mod tests {
168199
use super::*;
169200

201+
use option::{IntoOption, ToOption, AsOption};
202+
use option;
203+
170204
#[test]
171205
fn test_either_left() {
172206
let val = Left(10);
@@ -260,4 +294,30 @@ mod tests {
260294
assert_eq!(rights.len(), 0u);
261295
}
262296

297+
#[test]
298+
pub fn test_to_option() {
299+
let right: Either<int, int> = Right(100);
300+
let left: Either<int, int> = Left(404);
301+
302+
assert_eq!(right.to_option(), option::Some(100));
303+
assert_eq!(left.to_option(), option::None);
304+
}
305+
306+
#[test]
307+
pub fn test_into_option() {
308+
let right: Either<int, int> = Right(100);
309+
let left: Either<int, int> = Left(404);
310+
311+
assert_eq!(right.into_option(), option::Some(100));
312+
assert_eq!(left.into_option(), option::None);
313+
}
314+
315+
#[test]
316+
pub fn test_as_option() {
317+
let right: Either<int, int> = Right(100);
318+
let left: Either<int, int> = Left(404);
319+
320+
assert_eq!(right.as_option().unwrap(), &100);
321+
assert_eq!(left.as_option(), option::None);
322+
}
263323
}

branches/try/src/libstd/option.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,44 @@ impl<T> Option<T> {
388388
}
389389
}
390390

391+
/// A generic trait for converting a value to a `Option`
392+
pub trait ToOption<T> {
393+
/// Convert to the `option` type
394+
fn to_option(&self) -> Option<T>;
395+
}
396+
397+
/// A generic trait for converting a value to a `Option`
398+
pub trait IntoOption<T> {
399+
/// Convert to the `option` type
400+
fn into_option(self) -> Option<T>;
401+
}
402+
403+
/// A generic trait for converting a value to a `Option`
404+
pub trait AsOption<T> {
405+
/// Convert to the `option` type
406+
fn as_option<'a>(&'a self) -> Option<&'a T>;
407+
}
408+
409+
impl<T: Clone> ToOption<T> for Option<T> {
410+
#[inline]
411+
fn to_option(&self) -> Option<T> { self.clone() }
412+
}
413+
414+
impl<T> IntoOption<T> for Option<T> {
415+
#[inline]
416+
fn into_option(self) -> Option<T> { self }
417+
}
418+
419+
impl<T> AsOption<T> for Option<T> {
420+
#[inline]
421+
fn as_option<'a>(&'a self) -> Option<&'a T> {
422+
match *self {
423+
Some(ref x) => Some(x),
424+
None => None,
425+
}
426+
}
427+
}
428+
391429
impl<T: Default> Option<T> {
392430
/// Returns the contained value or default (for this type)
393431
#[inline]
@@ -711,4 +749,31 @@ mod tests {
711749
assert!(!x.mutate_default(0i, |i| i+1));
712750
assert_eq!(x, Some(0i));
713751
}
752+
753+
#[test]
754+
pub fn test_to_option() {
755+
let some: Option<int> = Some(100);
756+
let none: Option<int> = None;
757+
758+
assert_eq!(some.to_option(), Some(100));
759+
assert_eq!(none.to_option(), None);
760+
}
761+
762+
#[test]
763+
pub fn test_into_option() {
764+
let some: Option<int> = Some(100);
765+
let none: Option<int> = None;
766+
767+
assert_eq!(some.into_option(), Some(100));
768+
assert_eq!(none.into_option(), None);
769+
}
770+
771+
#[test]
772+
pub fn test_as_option() {
773+
let some: Option<int> = Some(100);
774+
let none: Option<int> = None;
775+
776+
assert_eq!(some.as_option().unwrap(), &100);
777+
assert_eq!(none.as_option(), None);
778+
}
714779
}

branches/try/src/libstd/result.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use cmp::Eq;
1717
use either;
1818
use iter::Iterator;
1919
use option::{None, Option, Some, OptionIterator};
20+
use option;
2021
use vec;
2122
use vec::OwnedVector;
2223
use to_str::ToStr;
@@ -255,6 +256,36 @@ impl<T, E: Clone + ToStr> Result<T, E> {
255256
}
256257
}
257258

259+
impl<T: Clone, E> option::ToOption<T> for Result<T, E> {
260+
#[inline]
261+
fn to_option(&self)-> Option<T> {
262+
match *self {
263+
Ok(ref t) => Some(t.clone()),
264+
Err(_) => None,
265+
}
266+
}
267+
}
268+
269+
impl<T, E> option::IntoOption<T> for Result<T, E> {
270+
#[inline]
271+
fn into_option(self)-> Option<T> {
272+
match self {
273+
Ok(t) => Some(t),
274+
Err(_) => None,
275+
}
276+
}
277+
}
278+
279+
impl<T, E> option::AsOption<T> for Result<T, E> {
280+
#[inline]
281+
fn as_option<'a>(&'a self)-> Option<&'a T> {
282+
match *self {
283+
Ok(ref t) => Some(t),
284+
Err(_) => None,
285+
}
286+
}
287+
}
288+
258289
#[inline]
259290
#[allow(missing_doc)]
260291
pub fn map_opt<T, U: ToStr, V>(o_t: &Option<T>,
@@ -336,6 +367,8 @@ mod tests {
336367

337368
use either;
338369
use iter::range;
370+
use option::{IntoOption, ToOption, AsOption};
371+
use option;
339372
use str::OwnedStr;
340373
use vec::ImmutableVector;
341374

@@ -460,4 +493,31 @@ mod tests {
460493
.map(|f| (*f)())),
461494
Err(1));
462495
}
496+
497+
#[test]
498+
pub fn test_to_option() {
499+
let ok: Result<int, int> = Ok(100);
500+
let err: Result<int, int> = Err(404);
501+
502+
assert_eq!(ok.to_option(), option::Some(100));
503+
assert_eq!(err.to_option(), option::None);
504+
}
505+
506+
#[test]
507+
pub fn test_into_option() {
508+
let ok: Result<int, int> = Ok(100);
509+
let err: Result<int, int> = Err(404);
510+
511+
assert_eq!(ok.into_option(), option::Some(100));
512+
assert_eq!(err.into_option(), option::None);
513+
}
514+
515+
#[test]
516+
pub fn test_as_option() {
517+
let ok: Result<int, int> = Ok(100);
518+
let err: Result<int, int> = Err(404);
519+
520+
assert_eq!(ok.as_option().unwrap(), &100);
521+
assert_eq!(err.as_option(), option::None);
522+
}
463523
}

0 commit comments

Comments
 (0)