Skip to content

Clean and tidy some traits #10967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ syn keyword rustEnumVariant Ok Err

" Types and traits {{{3
syn keyword rustTrait Any AnyOwnExt AnyRefExt AnyMutRefExt
syn keyword rustTrait Ascii AsciiCast OwnedAsciiCast AsciiStr ToBytesConsume
syn keyword rustTrait Ascii AsciiCast OwnedAsciiCast AsciiStr IntoBytes
syn keyword rustTrait Bool
syn keyword rustTrait ToCStr
syn keyword rustTrait Char
Expand Down Expand Up @@ -94,7 +94,7 @@ syn keyword rustTrait Buffer Writer Reader Seek
syn keyword rustTrait SendStr SendStrOwned SendStrStatic IntoSendStr
syn keyword rustTrait Str StrVector StrSlice OwnedStr
syn keyword rustTrait IterBytes
syn keyword rustTrait ToStr ToStrConsume
syn keyword rustTrait ToStr IntoStr
syn keyword rustTrait CopyableTuple ImmutableTuple
syn keyword rustTrait Tuple1 Tuple2 Tuple3 Tuple4
syn keyword rustTrait Tuple5 Tuple6 Tuple7 Tuple8
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//! Operations on ASCII strings and characters.

use to_str::{ToStr,ToStrConsume};
use to_str::{ToStr,IntoStr};
use str;
use str::StrSlice;
use str::OwnedStr;
Expand Down Expand Up @@ -294,7 +294,7 @@ impl<'a> AsciiStr for &'a [Ascii] {
}
}

impl ToStrConsume for ~[Ascii] {
impl IntoStr for ~[Ascii] {
#[inline]
fn into_str(self) -> ~str {
unsafe { cast::transmute(self) }
Expand All @@ -309,12 +309,12 @@ impl IterBytes for Ascii {
}

/// Trait to convert to a owned byte array by consuming self
pub trait ToBytesConsume {
pub trait IntoBytes {
/// Converts to a owned byte array by consuming self
fn into_bytes(self) -> ~[u8];
}

impl ToBytesConsume for ~[Ascii] {
impl IntoBytes for ~[Ascii] {
fn into_bytes(self) -> ~[u8] {
unsafe { cast::transmute(self) }
}
Expand Down
187 changes: 0 additions & 187 deletions src/libstd/either.rs
Original file line number Diff line number Diff line change
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);
}
}
Loading