From 529f91572870cffdd9f13ddae12bb1f4e03186ab Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Sat, 14 Dec 2013 23:22:01 +1100 Subject: [PATCH 1/2] Remove {As,Into,To}{Option,Either,Result} traits. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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` → `Result` was never really useful and `Result` → `Option` 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. --- src/libstd/either.rs | 187 ------------------------------------------- src/libstd/option.rs | 122 ---------------------------- src/libstd/result.rs | 132 ------------------------------ 3 files changed, 441 deletions(-) diff --git a/src/libstd/either.rs b/src/libstd/either.rs index 6d152d8c179a8..96f4e524852d1 100644 --- a/src/libstd/either.rs +++ b/src/libstd/either.rs @@ -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}; @@ -105,101 +102,6 @@ impl Either { } } -/// A generic trait for converting a value to a `Either` -pub trait ToEither { - /// Convert to the `either` type - fn to_either(&self) -> Either; -} - -/// A generic trait for converting a value to a `Either` -pub trait IntoEither { - /// Convert to the `either` type - fn into_either(self) -> Either; -} - -/// A generic trait for converting a value to a `Either` -pub trait AsEither { - /// Convert to the `either` type - fn as_either<'a>(&'a self) -> Either<&'a L, &'a R>; -} - -impl option::ToOption for Either { - #[inline] - fn to_option(&self)-> option::Option { - match *self { - Left(_) => None, - Right(ref r) => Some(r.clone()), - } - } -} - -impl option::IntoOption for Either { - #[inline] - fn into_option(self)-> option::Option { - match self { - Left(_) => None, - Right(r) => Some(r), - } - } -} - -impl option::AsOption for Either { - #[inline] - fn as_option<'a>(&'a self) -> option::Option<&'a R> { - match *self { - Left(_) => None, - Right(ref r) => Some(r), - } - } -} - -impl result::ToResult for Either { - #[inline] - fn to_result(&self)-> result::Result { - match *self { - Left(ref l) => result::Err(l.clone()), - Right(ref r) => result::Ok(r.clone()), - } - } -} - -impl result::IntoResult for Either { - #[inline] - fn into_result(self)-> result::Result { - match self { - Left(l) => result::Err(l), - Right(r) => result::Ok(r), - } - } -} - -impl result::AsResult for Either { - #[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 ToEither for Either { - fn to_either(&self) -> Either { self.clone() } -} - -impl IntoEither for Either { - fn into_either(self) -> Either { self } -} - -impl AsEither for Either { - 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 = FilterMap<'static, Either, L, Iter>; @@ -251,11 +153,6 @@ pub fn partition(eithers: ~[Either]) -> (~[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); @@ -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 = Right(100); - let left: Either = 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 = Right(100); - let left: Either = 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 = Right(100); - let left: Either = 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 = Right(100); - let left: Either = 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 = Right(100); - let left: Either = 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 = Right(100); - let left: Either = 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 = Right(100); - let left: Either = 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 = Right(100); - let left: Either = 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 = Right(100); - let left: Either = Left(404); - - assert_eq!(right.as_either().unwrap_right(), &100); - assert_eq!(left.as_either().unwrap_left(), &404); - } } diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 715072653a7ab..87f6c8608fdfb 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -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; @@ -359,83 +357,10 @@ impl Option { } } -///////////////////////////////////////////////////////////////////////////// -// Constructor extension trait -///////////////////////////////////////////////////////////////////////////// - -/// A generic trait for converting a value to a `Option` -pub trait ToOption { - /// Convert to the `option` type - fn to_option(&self) -> Option; -} - -/// A generic trait for converting a value to a `Option` -pub trait IntoOption { - /// Convert to the `option` type - fn into_option(self) -> Option; -} - -/// A generic trait for converting a value to a `Option` -pub trait AsOption { - /// Convert to the `option` type - fn as_option<'a>(&'a self) -> Option<&'a T>; -} - -impl ToOption for Option { - #[inline] - fn to_option(&self) -> Option { self.clone() } -} - -impl IntoOption for Option { - #[inline] - fn into_option(self) -> Option { self } -} - -impl AsOption for Option { - #[inline] - fn as_option<'a>(&'a self) -> Option<&'a T> { - match *self { - Some(ref x) => Some(x), - None => None, - } - } -} - ///////////////////////////////////////////////////////////////////////////// // Trait implementations ///////////////////////////////////////////////////////////////////////////// -impl ToResult for Option { - #[inline] - fn to_result(&self) -> Result { - match *self { - Some(ref x) => Ok(x.clone()), - None => Err(()), - } - } -} - -impl IntoResult for Option { - #[inline] - fn into_result(self) -> Result { - match self { - Some(x) => Ok(x), - None => Err(()), - } - } -} - -impl AsResult for Option { - #[inline] - fn as_result<'a>(&'a self) -> Result<&'a T, &'a ()> { - static UNIT: () = (); - match *self { - Some(ref t) => Ok(t), - None => Err(&UNIT), - } - } -} - impl fmt::Default for Option { #[inline] fn fmt(s: &Option, f: &mut fmt::Formatter) { @@ -493,8 +418,6 @@ impl ExactSize for OptionIterator {} mod tests { use super::*; - use result::{IntoResult, ToResult}; - use result::{Ok, Err}; use str::StrSlice; use util; @@ -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 = Some(100); - let none: Option = None; - - assert_eq!(some.to_option(), Some(100)); - assert_eq!(none.to_option(), None); - } - - #[test] - pub fn test_into_option() { - let some: Option = Some(100); - let none: Option = None; - - assert_eq!(some.into_option(), Some(100)); - assert_eq!(none.into_option(), None); - } - - #[test] - pub fn test_as_option() { - let some: Option = Some(100); - let none: Option = None; - - assert_eq!(some.as_option().unwrap(), &100); - assert_eq!(none.as_option(), None); - } - - #[test] - pub fn test_to_result() { - let some: Option = Some(100); - let none: Option = None; - - assert_eq!(some.to_result(), Ok(100)); - assert_eq!(none.to_result(), Err(())); - } - - #[test] - pub fn test_into_result() { - let some: Option = Some(100); - let none: Option = None; - - assert_eq!(some.into_result(), Ok(100)); - assert_eq!(none.into_result(), Err(())); - } } diff --git a/src/libstd/result.rs b/src/libstd/result.rs index 690b4e06d4999..79198d0314f87 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -15,7 +15,6 @@ use cmp::Eq; use fmt; use iter::Iterator; use option::{None, Option, Some}; -use option::{ToOption, IntoOption, AsOption}; use str::OwnedStr; use to_str::ToStr; use vec::OwnedVector; @@ -204,82 +203,10 @@ impl Result { } } -///////////////////////////////////////////////////////////////////////////// -// Constructor extension trait -///////////////////////////////////////////////////////////////////////////// - -/// A generic trait for converting a value to a `Result` -pub trait ToResult { - /// Convert to the `result` type - fn to_result(&self) -> Result; -} - -/// A generic trait for converting a value to a `Result` -pub trait IntoResult { - /// Convert to the `result` type - fn into_result(self) -> Result; -} - -/// A generic trait for converting a value to a `Result` -pub trait AsResult { - /// Convert to the `result` type - fn as_result<'a>(&'a self) -> Result<&'a T, &'a E>; -} - -impl ToResult for Result { - #[inline] - fn to_result(&self) -> Result { self.clone() } -} - -impl IntoResult for Result { - #[inline] - fn into_result(self) -> Result { self } -} - -impl AsResult for Result { - #[inline] - fn as_result<'a>(&'a self) -> Result<&'a T, &'a E> { - match *self { - Ok(ref t) => Ok(t), - Err(ref e) => Err(e), - } - } -} - ///////////////////////////////////////////////////////////////////////////// // Trait implementations ///////////////////////////////////////////////////////////////////////////// -impl ToOption for Result { - #[inline] - fn to_option(&self) -> Option { - match *self { - Ok(ref t) => Some(t.clone()), - Err(_) => None, - } - } -} - -impl IntoOption for Result { - #[inline] - fn into_option(self) -> Option { - match self { - Ok(t) => Some(t), - Err(_) => None, - } - } -} - -impl AsOption for Result { - #[inline] - fn as_option<'a>(&'a self) -> Option<&'a T> { - match *self { - Ok(ref t) => Some(t), - Err(_) => None, - } - } -} - impl fmt::Default for Result { #[inline] fn fmt(s: &Result, f: &mut fmt::Formatter) { @@ -364,8 +291,6 @@ mod tests { use super::*; use iter::range; - use option::{IntoOption, ToOption, AsOption}; - use option::{Some, None}; use vec::ImmutableVector; use to_str::ToStr; @@ -460,63 +385,6 @@ mod tests { Err(1)); } - #[test] - pub fn test_to_option() { - let ok: Result = Ok(100); - let err: Result = Err(404); - - assert_eq!(ok.to_option(), Some(100)); - assert_eq!(err.to_option(), None); - } - - #[test] - pub fn test_into_option() { - let ok: Result = Ok(100); - let err: Result = Err(404); - - assert_eq!(ok.into_option(), Some(100)); - assert_eq!(err.into_option(), None); - } - - #[test] - pub fn test_as_option() { - let ok: Result = Ok(100); - let err: Result = Err(404); - - assert_eq!(ok.as_option().unwrap(), &100); - assert_eq!(err.as_option(), None); - } - - #[test] - pub fn test_to_result() { - let ok: Result = Ok(100); - let err: Result = Err(404); - - assert_eq!(ok.to_result(), Ok(100)); - assert_eq!(err.to_result(), Err(404)); - } - - #[test] - pub fn test_into_result() { - let ok: Result = Ok(100); - let err: Result = Err(404); - - assert_eq!(ok.into_result(), Ok(100)); - assert_eq!(err.into_result(), Err(404)); - } - - #[test] - pub fn test_as_result() { - let ok: Result = Ok(100); - let err: Result = Err(404); - - let x = 100; - assert_eq!(ok.as_result(), Ok(&x)); - - let x = 404; - assert_eq!(err.as_result(), Err(&x)); - } - #[test] pub fn test_to_str() { let ok: Result = Ok(100); From b76997f3a947b296ca04be184da88ff1c0959dcb Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Sun, 15 Dec 2013 01:04:22 +1100 Subject: [PATCH 2/2] Rename To{Str,Bytes}Consume traits to Into*. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That is: - `ToStrConsume` → `IntoStr`; - `ToBytesConsume` → `IntoBytes`. --- src/etc/vim/syntax/rust.vim | 4 ++-- src/libstd/ascii.rs | 8 ++++---- src/libstd/prelude.rs | 4 ++-- src/libstd/to_str.rs | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/etc/vim/syntax/rust.vim b/src/etc/vim/syntax/rust.vim index 9131c4faa8021..aa0549a861c89 100644 --- a/src/etc/vim/syntax/rust.vim +++ b/src/etc/vim/syntax/rust.vim @@ -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 @@ -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 diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index 2242ded94f53f..4cdcbbdb9e8e1 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -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; @@ -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) } @@ -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) } } diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index 83439d4c90305..6407b34c576f0 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -45,7 +45,7 @@ pub use io::stdio::{print, println}; // Reexported types and traits pub use any::{Any, AnyOwnExt, AnyRefExt, AnyMutRefExt}; -pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr, ToBytesConsume}; +pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr, IntoBytes}; pub use bool::Bool; pub use c_str::ToCStr; pub use char::Char; @@ -71,7 +71,7 @@ pub use io::{Buffer, Writer, Reader, Seek}; pub use send_str::{SendStr, SendStrOwned, SendStrStatic, IntoSendStr}; pub use str::{Str, StrVector, StrSlice, OwnedStr}; pub use to_bytes::IterBytes; -pub use to_str::{ToStr, ToStrConsume}; +pub use to_str::{ToStr, IntoStr}; pub use tuple::{CopyableTuple, ImmutableTuple}; pub use tuple::{ImmutableTuple1, ImmutableTuple2, ImmutableTuple3, ImmutableTuple4}; pub use tuple::{ImmutableTuple5, ImmutableTuple6, ImmutableTuple7, ImmutableTuple8}; diff --git a/src/libstd/to_str.rs b/src/libstd/to_str.rs index 41c8aef18f47c..a58b09d8ecde3 100644 --- a/src/libstd/to_str.rs +++ b/src/libstd/to_str.rs @@ -30,7 +30,7 @@ pub trait ToStr { } /// Trait for converting a type to a string, consuming it in the process. -pub trait ToStrConsume { +pub trait IntoStr { /// Consume and convert to a string. fn into_str(self) -> ~str; }