diff options
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/either.rs | 248 | ||||
| -rw-r--r-- | src/libstd/fmt/mod.rs | 14 | ||||
| -rw-r--r-- | src/libstd/fmt/parse.rs | 35 | ||||
| -rw-r--r-- | src/libstd/fmt/rt.rs | 8 | ||||
| -rw-r--r-- | src/libstd/lib.rs | 1 | ||||
| -rw-r--r-- | src/libstd/prelude.rs | 1 | ||||
| -rw-r--r-- | src/libstd/util.rs | 10 |
7 files changed, 35 insertions, 282 deletions
diff --git a/src/libstd/either.rs b/src/libstd/either.rs deleted file mode 100644 index d07006aa6de..00000000000 --- a/src/libstd/either.rs +++ /dev/null @@ -1,248 +0,0 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Representing values with two possibilities (`Either` type) - -#[allow(missing_doc)]; - -use option::{Some, None}; -use clone::Clone; -use container::Container; -use cmp::Eq; -use iter::{Iterator, FilterMap}; -use str::StrSlice; -use vec; -use vec::{OwnedVector, ImmutableVector}; - -/// `Either` is a type that represents one of two alternatives -#[deriving(Clone, Eq, IterBytes)] -pub enum Either<L, R> { - Left(L), - Right(R) -} - -impl<L, R> Either<L, R> { - /// Applies a function based on the given either value - /// - /// If `value` is `Left(L)` then `f_left` is applied to its contents, if - /// `value` is `Right(R)` then `f_right` is applied to its contents, and the - /// result is returned. - #[inline] - pub fn either<T>(&self, f_left: |&L| -> T, f_right: |&R| -> T) -> T { - match *self { - Left(ref l) => f_left(l), - Right(ref r) => f_right(r) - } - } - - /// Flips between left and right of a given `Either` - #[inline] - pub fn flip(self) -> Either<R, L> { - match self { - Right(r) => Left(r), - Left(l) => Right(l) - } - } - - /// Checks whether the given value is a `Left` - #[inline] - pub fn is_left(&self) -> bool { - match *self { - Left(_) => true, - _ => false - } - } - - /// Checks whether the given value is a `Right` - #[inline] - pub fn is_right(&self) -> bool { - match *self { - Right(_) => true, - _ => false - } - } - - /// Retrieves the value from a `Left`. - /// Fails with a specified reason if the `Either` is `Right`. - #[inline] - pub fn expect_left(self, reason: &str) -> L { - match self { - Left(x) => x, - Right(_) => fail!("{}", reason.to_owned()) - } - } - - /// Retrieves the value from a `Left`. Fails if the `Either` is `Right`. - #[inline] - pub fn unwrap_left(self) -> L { - self.expect_left("called Either::unwrap_left()` on `Right` value") - } - - /// Retrieves the value from a `Right`. - /// Fails with a specified reason if the `Either` is `Left`. - #[inline] - pub fn expect_right(self, reason: &str) -> R { - match self { - Right(x) => x, - Left(_) => fail!("{}", reason.to_owned()) - } - } - - /// Retrieves the value from a `Right`. Fails if the `Either` is `Left`. - #[inline] - pub fn unwrap_right(self) -> R { - self.expect_right("called Either::unwrap_right()` on `Left` value") - } -} - -/// An iterator yielding the `Left` values of its source -pub type Lefts<L, R, Iter> = FilterMap<'static, Either<L, R>, L, Iter>; - -/// An iterator yielding the `Right` values of its source -pub type Rights<L, R, Iter> = FilterMap<'static, Either<L, R>, R, Iter>; - -/// Extracts all the left values -pub fn lefts<L, R, Iter: Iterator<Either<L, R>>>(eithers: Iter) - -> Lefts<L, R, Iter> { - eithers.filter_map(|elt| { - match elt { - Left(x) => Some(x), - _ => None, - } - }) -} - -/// Extracts all the right values -pub fn rights<L, R, Iter: Iterator<Either<L, R>>>(eithers: Iter) - -> Rights<L, R, Iter> { - eithers.filter_map(|elt| { - match elt { - Right(x) => Some(x), - _ => None, - } - }) -} - - -// FIXME: #8228 Replaceable by an external iterator? -/// Extracts from a vector of either all the left values and right values -/// -/// Returns a structure containing a vector of left values and a vector of -/// right values. -pub fn partition<L, R>(eithers: ~[Either<L, R>]) -> (~[L], ~[R]) { - let n_lefts = eithers.iter().count(|elt| elt.is_left()); - let mut lefts = vec::with_capacity(n_lefts); - let mut rights = vec::with_capacity(eithers.len() - n_lefts); - for elt in eithers.move_iter() { - match elt { - Left(l) => lefts.push(l), - Right(r) => rights.push(r) - } - } - return (lefts, rights); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_either_left() { - let val = Left(10); - fn f_left(x: &int) -> bool { *x == 10 } - fn f_right(_x: &uint) -> bool { false } - assert!(val.either(f_left, f_right)); - } - - #[test] - fn test_either_right() { - let val = Right(10u); - fn f_left(_x: &int) -> bool { false } - fn f_right(x: &uint) -> bool { *x == 10u } - assert!(val.either(f_left, f_right)); - } - - #[test] - fn test_lefts() { - let input = ~[Left(10), Right(11), Left(12), Right(13), Left(14)]; - let result = lefts(input.move_iter()).to_owned_vec(); - assert_eq!(result, ~[10, 12, 14]); - } - - #[test] - fn test_lefts_none() { - let input: ~[Either<int, int>] = ~[Right(10), Right(10)]; - let result = lefts(input.move_iter()).to_owned_vec(); - assert_eq!(result.len(), 0u); - } - - #[test] - fn test_lefts_empty() { - let input: ~[Either<int, int>] = ~[]; - let result = lefts(input.move_iter()).to_owned_vec(); - assert_eq!(result.len(), 0u); - } - - #[test] - fn test_rights() { - let input = ~[Left(10), Right(11), Left(12), Right(13), Left(14)]; - let result = rights(input.move_iter()).to_owned_vec(); - assert_eq!(result, ~[11, 13]); - } - - #[test] - fn test_rights_none() { - let input: ~[Either<int, int>] = ~[Left(10), Left(10)]; - let result = rights(input.move_iter()).to_owned_vec(); - assert_eq!(result.len(), 0u); - } - - #[test] - fn test_rights_empty() { - let input: ~[Either<int, int>] = ~[]; - let result = rights(input.move_iter()).to_owned_vec(); - assert_eq!(result.len(), 0u); - } - - #[test] - fn test_partition() { - let input = ~[Left(10), Right(11), Left(12), Right(13), Left(14)]; - let (lefts, rights) = partition(input); - assert_eq!(lefts[0], 10); - assert_eq!(lefts[1], 12); - assert_eq!(lefts[2], 14); - assert_eq!(rights[0], 11); - assert_eq!(rights[1], 13); - } - - #[test] - fn test_partition_no_lefts() { - let input: ~[Either<int, int>] = ~[Right(10), Right(11)]; - let (lefts, rights) = partition(input); - assert_eq!(lefts.len(), 0u); - assert_eq!(rights.len(), 2u); - } - - #[test] - fn test_partition_no_rights() { - let input: ~[Either<int, int>] = ~[Left(10), Left(11)]; - let (lefts, rights) = partition(input); - assert_eq!(lefts.len(), 2u); - assert_eq!(rights.len(), 0u); - } - - #[test] - fn test_partition_empty() { - let input: ~[Either<int, int>] = ~[]; - let (lefts, rights) = partition(input); - assert_eq!(lefts.len(), 0u); - assert_eq!(rights.len(), 0u); - } -} diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index cd9c0f5d2b7..53eaf17c7f8 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -757,7 +757,7 @@ impl<'a> Formatter<'a> { // offsetted value for s in selectors.iter() { match s.selector { - Right(val) if value == val => { + rt::Literal(val) if value == val => { return self.runplural(value, s.result); } _ => {} @@ -769,17 +769,17 @@ impl<'a> Formatter<'a> { let value = value - match offset { Some(i) => i, None => 0 }; for s in selectors.iter() { let run = match s.selector { - Left(parse::Zero) => value == 0, - Left(parse::One) => value == 1, - Left(parse::Two) => value == 2, + rt::Keyword(parse::Zero) => value == 0, + rt::Keyword(parse::One) => value == 1, + rt::Keyword(parse::Two) => value == 2, // XXX: Few/Many should have a user-specified boundary // One possible option would be in the function // pointer of the 'arg: Argument' struct. - Left(parse::Few) => value < 8, - Left(parse::Many) => value >= 8, + rt::Keyword(parse::Few) => value < 8, + rt::Keyword(parse::Many) => value >= 8, - Right(..) => false + rt::Literal(..) => false }; if run { return self.runplural(value, s.result); diff --git a/src/libstd/fmt/parse.rs b/src/libstd/fmt/parse.rs index e9f7af181a7..0ac1aac2380 100644 --- a/src/libstd/fmt/parse.rs +++ b/src/libstd/fmt/parse.rs @@ -122,12 +122,21 @@ pub enum Method<'a> { Select(~[SelectArm<'a>], ~[Piece<'a>]), } +/// A selector for what pluralization a plural method should take +#[deriving(Eq, IterBytes)] +pub enum PluralSelector { + /// One of the plural keywords should be used + Keyword(PluralKeyword), + /// A literal pluralization should be used + Literal(uint), +} + /// Structure representing one "arm" of the `plural` function. #[deriving(Eq)] pub struct PluralArm<'a> { /// A selector can either be specified by a keyword or with an integer /// literal. - selector: Either<PluralKeyword, uint>, + selector: PluralSelector, /// Array of pieces which are the format of this arm result: ~[Piece<'a>], } @@ -504,29 +513,29 @@ impl<'a> Parser<'a> { let mut isother = false; let selector = if self.wsconsume('=') { match self.integer() { - Some(i) => Right(i), + Some(i) => Literal(i), None => { self.err("plural `=` selectors must be followed by an \ integer"); - Right(0) + Literal(0) } } } else { let word = self.word(); match word { - "other" => { isother = true; Left(Zero) } - "zero" => Left(Zero), - "one" => Left(One), - "two" => Left(Two), - "few" => Left(Few), - "many" => Left(Many), + "other" => { isother = true; Keyword(Zero) } + "zero" => Keyword(Zero), + "one" => Keyword(One), + "two" => Keyword(Two), + "few" => Keyword(Few), + "many" => Keyword(Many), word => { self.err(format!("unexpected plural selector `{}`", word)); if word == "" { break } else { - Left(Zero) + Keyword(Zero) } } } @@ -955,9 +964,9 @@ mod tests { position: ArgumentNext, format: fmtdflt(), method: Some(~Plural(Some(1), ~[ - PluralArm{ selector: Right(2), result: ~[String("2")] }, - PluralArm{ selector: Right(3), result: ~[String("3")] }, - PluralArm{ selector: Left(Many), result: ~[String("yes")] } + PluralArm{ selector: Literal(2), result: ~[String("2")] }, + PluralArm{ selector: Literal(3), result: ~[String("3")] }, + PluralArm{ selector: Keyword(Many), result: ~[String("yes")] } ], ~[String("haha")])) })]); } diff --git a/src/libstd/fmt/rt.rs b/src/libstd/fmt/rt.rs index c139a2f5734..89895f30585 100644 --- a/src/libstd/fmt/rt.rs +++ b/src/libstd/fmt/rt.rs @@ -17,7 +17,6 @@ #[allow(missing_doc)]; #[doc(hidden)]; -use either::Either; use fmt::parse; use option::Option; @@ -55,8 +54,13 @@ pub enum Method<'a> { Select(&'a [SelectArm<'a>], &'a [Piece<'a>]), } +pub enum PluralSelector { + Keyword(parse::PluralKeyword), + Literal(uint), +} + pub struct PluralArm<'a> { - selector: Either<parse::PluralKeyword, uint>, + selector: PluralSelector, result: &'a [Piece<'a>], } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index fa0d3f7fc2a..1d24c2c66f8 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -148,7 +148,6 @@ pub mod any; pub mod option; pub mod result; -pub mod either; pub mod hashmap; pub mod cell; pub mod trie; diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index 3c81ff661bc..e1478de9485 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -29,7 +29,6 @@ Rust's prelude has three main parts: */ // Reexported core operators -pub use either::{Either, Left, Right}; pub use kinds::{Freeze, Pod, Send, Sized}; pub use ops::{Add, Sub, Mul, Div, Rem, Neg, Not}; pub use ops::{BitAnd, BitOr, BitXor}; diff --git a/src/libstd/util.rs b/src/libstd/util.rs index d12d61ed46c..6c0424b50e1 100644 --- a/src/libstd/util.rs +++ b/src/libstd/util.rs @@ -82,7 +82,6 @@ mod tests { use clone::Clone; use ops::Drop; use option::{None, Some}; - use either::{Either, Left, Right}; use mem::size_of; #[test] @@ -112,15 +111,6 @@ mod tests { } #[test] - fn test_uninhabited() { - let could_only_be_coin : Either <Void, ()> = Right (()); - match could_only_be_coin { - Right (coin) => coin, - Left (is_void) => is_void.uninhabited () - } - } - - #[test] fn test_noncopyable() { assert_eq!(size_of::<NonCopyable>(), 0); |
