Skip to content

Commit

Permalink
Remove {As,Into,To}{Option,Either,Result} traits.
Browse files Browse the repository at this point in the history
Expanded, that is:

- `AsOption`
- `IntoOption`
- `ToOption`
- `AsEither`
- `IntoEither`
- `ToEither`
- `AsResult`
- `IntoResult`
- `ToResult`

These were defined for each other but never *used* anywhere. They are
all trivial and so removal will have negligible effect upon anyone.
`Either` has fallen out of favour (and its implementation of these
traits of dubious semantics), `Option<T>` → `Result<T, ()>` was never
really useful and `Result<T, E>` → `Option<T>` should now be done with
`Result.ok()` (mirrored with `Result.err()` for even more usefulness).

In summary, there's really no point in any of these remaining.
  • Loading branch information
chris-morgan committed Dec 14, 2013
1 parent 8440036 commit 529f915
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 441 deletions.
187 changes: 0 additions & 187 deletions src/libstd/either.rs
Expand Up @@ -13,13 +13,10 @@
#[allow(missing_doc)];

use option::{Some, None};
use option;
use clone::Clone;
use container::Container;
use cmp::Eq;
use iter::{Iterator, FilterMap};
use result::Result;
use result;
use str::StrSlice;
use vec;
use vec::{OwnedVector, ImmutableVector};
Expand Down Expand Up @@ -105,101 +102,6 @@ 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> {
match *self {
Left(_) => None,
Right(ref r) => Some(r.clone()),
}
}
}

impl<L, R> option::IntoOption<R> for Either<L, R> {
#[inline]
fn into_option(self)-> option::Option<R> {
match self {
Left(_) => None,
Right(r) => Some(r),
}
}
}

impl<L, R> option::AsOption<R> for Either<L, R> {
#[inline]
fn as_option<'a>(&'a self) -> option::Option<&'a R> {
match *self {
Left(_) => None,
Right(ref r) => Some(r),
}
}
}

impl<L: Clone, R: Clone> result::ToResult<R, L> for Either<L, R> {
#[inline]
fn to_result(&self)-> result::Result<R, L> {
match *self {
Left(ref l) => result::Err(l.clone()),
Right(ref r) => result::Ok(r.clone()),
}
}
}

impl<L, R> result::IntoResult<R, L> for Either<L, R> {
#[inline]
fn into_result(self)-> result::Result<R, L> {
match self {
Left(l) => result::Err(l),
Right(r) => result::Ok(r),
}
}
}

impl<L, R> result::AsResult<R, L> for Either<L, R> {
#[inline]
fn as_result<'a>(&'a self) -> result::Result<&'a R, &'a L> {
match *self {
Left(ref l) => result::Err(l),
Right(ref r) => result::Ok(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>;

Expand Down Expand Up @@ -251,11 +153,6 @@ pub fn partition<L, R>(eithers: ~[Either<L, R>]) -> (~[L], ~[R]) {
mod tests {
use super::*;

use option::{IntoOption, ToOption, AsOption};
use option;
use result::{IntoResult, ToResult, AsResult};
use result;

#[test]
fn test_either_left() {
let val = Left(10);
Expand Down Expand Up @@ -348,88 +245,4 @@ mod tests {
assert_eq!(lefts.len(), 0u);
assert_eq!(rights.len(), 0u);
}

#[test]
pub fn test_to_option() {
let right: Either<int, int> = Right(100);
let left: Either<int, int> = Left(404);

assert_eq!(right.to_option(), option::Some(100));
assert_eq!(left.to_option(), option::None);
}

#[test]
pub fn test_into_option() {
let right: Either<int, int> = Right(100);
let left: Either<int, int> = Left(404);

assert_eq!(right.into_option(), option::Some(100));
assert_eq!(left.into_option(), option::None);
}

#[test]
pub fn test_as_option() {
let right: Either<int, int> = Right(100);
let left: Either<int, int> = Left(404);

assert_eq!(right.as_option().unwrap(), &100);
assert_eq!(left.as_option(), option::None);
}

#[test]
pub fn test_to_result() {
let right: Either<int, int> = Right(100);
let left: Either<int, int> = Left(404);

assert_eq!(right.to_result(), result::Ok(100));
assert_eq!(left.to_result(), result::Err(404));
}

#[test]
pub fn test_into_result() {
let right: Either<int, int> = Right(100);
let left: Either<int, int> = Left(404);

assert_eq!(right.into_result(), result::Ok(100));
assert_eq!(left.into_result(), result::Err(404));
}

#[test]
pub fn test_as_result() {
let right: Either<int, int> = Right(100);
let left: Either<int, int> = Left(404);

let x = 100;
assert_eq!(right.as_result(), result::Ok(&x));

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);
}
}
122 changes: 0 additions & 122 deletions src/libstd/option.rs
Expand Up @@ -45,8 +45,6 @@ use default::Default;
use fmt;
use iter::{Iterator, DoubleEndedIterator, ExactSize};
use kinds::Send;
use result::{IntoResult, ToResult, AsResult};
use result::{Result, Ok, Err};
use str::OwnedStr;
use to_str::ToStr;
use util;
Expand Down Expand Up @@ -359,83 +357,10 @@ impl<T: Default> Option<T> {
}
}

/////////////////////////////////////////////////////////////////////////////
// Constructor extension trait
/////////////////////////////////////////////////////////////////////////////

/// A generic trait for converting a value to a `Option`
pub trait ToOption<T> {
/// Convert to the `option` type
fn to_option(&self) -> Option<T>;
}

/// A generic trait for converting a value to a `Option`
pub trait IntoOption<T> {
/// Convert to the `option` type
fn into_option(self) -> Option<T>;
}

/// A generic trait for converting a value to a `Option`
pub trait AsOption<T> {
/// Convert to the `option` type
fn as_option<'a>(&'a self) -> Option<&'a T>;
}

impl<T: Clone> ToOption<T> for Option<T> {
#[inline]
fn to_option(&self) -> Option<T> { self.clone() }
}

impl<T> IntoOption<T> for Option<T> {
#[inline]
fn into_option(self) -> Option<T> { self }
}

impl<T> AsOption<T> for Option<T> {
#[inline]
fn as_option<'a>(&'a self) -> Option<&'a T> {
match *self {
Some(ref x) => Some(x),
None => None,
}
}
}

/////////////////////////////////////////////////////////////////////////////
// Trait implementations
/////////////////////////////////////////////////////////////////////////////

impl<T: Clone> ToResult<T, ()> for Option<T> {
#[inline]
fn to_result(&self) -> Result<T, ()> {
match *self {
Some(ref x) => Ok(x.clone()),
None => Err(()),
}
}
}

impl<T> IntoResult<T, ()> for Option<T> {
#[inline]
fn into_result(self) -> Result<T, ()> {
match self {
Some(x) => Ok(x),
None => Err(()),
}
}
}

impl<T> AsResult<T, ()> for Option<T> {
#[inline]
fn as_result<'a>(&'a self) -> Result<&'a T, &'a ()> {
static UNIT: () = ();
match *self {
Some(ref t) => Ok(t),
None => Err(&UNIT),
}
}
}

impl<T: fmt::Default> fmt::Default for Option<T> {
#[inline]
fn fmt(s: &Option<T>, f: &mut fmt::Formatter) {
Expand Down Expand Up @@ -493,8 +418,6 @@ impl<A> ExactSize<A> for OptionIterator<A> {}
mod tests {
use super::*;

use result::{IntoResult, ToResult};
use result::{Ok, Err};
use str::StrSlice;
use util;

Expand Down Expand Up @@ -732,49 +655,4 @@ mod tests {
assert!(!x.mutate_default(0i, |i| i+1));
assert_eq!(x, Some(0i));
}

#[test]
pub fn test_to_option() {
let some: Option<int> = Some(100);
let none: Option<int> = None;

assert_eq!(some.to_option(), Some(100));
assert_eq!(none.to_option(), None);
}

#[test]
pub fn test_into_option() {
let some: Option<int> = Some(100);
let none: Option<int> = None;

assert_eq!(some.into_option(), Some(100));
assert_eq!(none.into_option(), None);
}

#[test]
pub fn test_as_option() {
let some: Option<int> = Some(100);
let none: Option<int> = None;

assert_eq!(some.as_option().unwrap(), &100);
assert_eq!(none.as_option(), None);
}

#[test]
pub fn test_to_result() {
let some: Option<int> = Some(100);
let none: Option<int> = None;

assert_eq!(some.to_result(), Ok(100));
assert_eq!(none.to_result(), Err(()));
}

#[test]
pub fn test_into_result() {
let some: Option<int> = Some(100);
let none: Option<int> = None;

assert_eq!(some.into_result(), Ok(100));
assert_eq!(none.into_result(), Err(()));
}
}

0 comments on commit 529f915

Please sign in to comment.