diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-12-11 15:16:36 -0800 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-12-13 16:14:28 -0800 |
| commit | d809e89c2682c3bb0ec0b58d7a8beb71060ae619 (patch) | |
| tree | 3e31eb1e518adf5f6c27dab2390b2973e95c7b6e /src/libstd | |
| parent | 742f354ffb08d81f6977aabc78a854cced22b9d3 (diff) | |
| download | rust-d809e89c2682c3bb0ec0b58d7a8beb71060ae619.tar.gz rust-d809e89c2682c3bb0ec0b58d7a8beb71060ae619.zip | |
Replace some Eq impls with deriving_eq
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/getopts.rs | 125 | ||||
| -rw-r--r-- | src/libstd/list.rs | 21 | ||||
| -rw-r--r-- | src/libstd/test.rs | 8 | ||||
| -rw-r--r-- | src/libstd/workcache.rs | 10 |
4 files changed, 13 insertions, 151 deletions
diff --git a/src/libstd/getopts.rs b/src/libstd/getopts.rs index e7851b3ecb9..38540524dac 100644 --- a/src/libstd/getopts.rs +++ b/src/libstd/getopts.rs @@ -79,16 +79,20 @@ use core::result::{Err, Ok}; use core::option; use core::option::{Some, None}; +#[deriving_eq] enum Name { Long(~str), Short(char), } +#[deriving_eq] enum HasArg { Yes, No, Maybe, } +#[deriving_eq] enum Occur { Req, Optional, Multi, } /// A description of a possible option +#[deriving_eq] pub struct Opt { name: Name, hasarg: HasArg, @@ -102,49 +106,6 @@ fn mkname(nm: &str) -> Name { } else { Long(unm) }; } -impl Name : Eq { - pure fn eq(&self, other: &Name) -> bool { - match (*self) { - Long(ref e0a) => { - match (*other) { - Long(ref e0b) => e0a == e0b, - _ => false - } - } - Short(e0a) => { - match (*other) { - Short(e0b) => e0a == e0b, - _ => false - } - } - } - } - pure fn ne(&self, other: &Name) -> bool { !(*self).eq(other) } -} - -impl Occur : Eq { - pure fn eq(&self, other: &Occur) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &Occur) -> bool { !(*self).eq(other) } -} - -impl HasArg : Eq { - pure fn eq(&self, other: &HasArg) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &HasArg) -> bool { !(*self).eq(other) } -} - -impl Opt : Eq { - pure fn eq(&self, other: &Opt) -> bool { - (*self).name == (*other).name && - (*self).hasarg == (*other).hasarg && - (*self).occur == (*other).occur - } - pure fn ne(&self, other: &Opt) -> bool { !(*self).eq(other) } -} - /// Create an option that is required and takes an argument pub fn reqopt(name: &str) -> Opt { return Opt {name: mkname(name), hasarg: Yes, occur: Req}; @@ -178,39 +139,20 @@ pub fn optmulti(name: &str) -> Opt { return Opt {name: mkname(name), hasarg: Yes, occur: Multi}; } +#[deriving_eq] enum Optval { Val(~str), Given, } /** * The result of checking command line arguments. Contains a vector * of matches and a vector of free strings. */ +#[deriving_eq] pub struct Matches { opts: ~[Opt], vals: ~[~[Optval]], free: ~[~str] } -impl Optval : Eq { - pure fn eq(&self, other: &Optval) -> bool { - match (*self) { - Val(ref s) => match *other { Val (ref os) => s == os, - Given => false }, - Given => match *other { Val(_) => false, - Given => true } - } - } - pure fn ne(&self, other: &Optval) -> bool { !(*self).eq(other) } -} - -impl Matches : Eq { - pure fn eq(&self, other: &Matches) -> bool { - (*self).opts == (*other).opts && - (*self).vals == (*other).vals && - (*self).free == (*other).free - } - pure fn ne(&self, other: &Matches) -> bool { !(*self).eq(other) } -} - fn is_arg(arg: &str) -> bool { return arg.len() > 1 && arg[0] == '-' as u8; } @@ -230,6 +172,7 @@ fn find_opt(opts: &[Opt], nm: Name) -> Option<uint> { * The type returned when the command line does not conform to the * expected format. Pass this value to <fail_str> to get an error message. */ +#[deriving_eq] pub enum Fail_ { ArgumentMissing(~str), UnrecognizedOption(~str), @@ -238,35 +181,6 @@ pub enum Fail_ { UnexpectedArgument(~str), } -impl Fail_ : Eq { - // this whole thing should be easy to infer... - pure fn eq(&self, other: &Fail_) -> bool { - match (*self) { - ArgumentMissing(ref s) => { - match *other { ArgumentMissing(ref so) => s == so, - _ => false } - } - UnrecognizedOption(ref s) => { - match *other { UnrecognizedOption(ref so) => s == so, - _ => false } - } - OptionMissing(ref s) => { - match *other { OptionMissing(ref so) => s == so, - _ => false } - } - OptionDuplicated(ref s) => { - match *other { OptionDuplicated(ref so) => s == so, - _ => false } - } - UnexpectedArgument(ref s) => { - match *other { UnexpectedArgument(ref so) => s == so, - _ => false } - } - } - } - pure fn ne(&self, other: &Fail_) -> bool { !(*self).eq(other) } -} - /// Convert a `fail_` enum into an error string pub fn fail_str(f: Fail_) -> ~str { return match f { @@ -523,6 +437,7 @@ pub fn opt_default(mm: &Matches, nm: &str, def: &str) -> Option<~str> { _ => Some::<~str>(str::from_slice(def)) } } +#[deriving_eq] enum FailType { ArgumentMissing_, UnrecognizedOption_, @@ -531,13 +446,6 @@ enum FailType { UnexpectedArgument_, } -impl FailType : Eq { - pure fn eq(&self, other: &FailType) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &FailType) -> bool { !(*self).eq(other) } -} - /** A module which provides a way to specify descriptions and * groups of short and long option names, together. */ @@ -546,6 +454,7 @@ pub mod groups { /** one group of options, e.g., both -h and --help, along with * their shared description and properties */ + #[deriving_eq] pub struct OptGroup { short_name: ~str, long_name: ~str, @@ -555,20 +464,6 @@ pub mod groups { occur: Occur } - impl OptGroup : Eq { - pure fn eq(&self, other: &OptGroup) -> bool { - (*self).short_name == (*other).short_name && - (*self).long_name == (*other).long_name && - (*self).hint == (*other).hint && - (*self).desc == (*other).desc && - (*self).hasarg == (*other).hasarg && - (*self).occur == (*other).occur - } - pure fn ne(&self, other: &OptGroup) -> bool { - !self.eq(other) - } - } - /// Create a long option that is required and takes an argument pub fn reqopt(short_name: &str, long_name: &str, desc: &str, hint: &str) -> OptGroup { @@ -733,7 +628,7 @@ mod tests { #[legacy_exports]; use opt = getopts; use result::{Err, Ok}; - use opt::groups::OptGroup; + use getopts::groups::OptGroup; fn check_fail_type(f: Fail_, ft: FailType) { match f { diff --git a/src/libstd/list.rs b/src/libstd/list.rs index 68f807f8403..6d2c10eb827 100644 --- a/src/libstd/list.rs +++ b/src/libstd/list.rs @@ -16,6 +16,7 @@ use core::option; use option::*; use option::{Some, None}; +#[deriving_eq] pub enum List<T> { Cons(T, @List<T>), Nil, @@ -157,26 +158,6 @@ pub fn each<T>(l: @List<T>, f: fn(&T) -> bool) { } } -impl<T:Eq> List<T> : Eq { - pure fn eq(&self, other: &List<T>) -> bool { - match (*self) { - Cons(ref e0a, e1a) => { - match (*other) { - Cons(ref e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - Nil => { - match (*other) { - Nil => true, - _ => false - } - } - } - } - pure fn ne(&self, other: &List<T>) -> bool { !(*self).eq(other) } -} - #[cfg(test)] mod tests { #[legacy_exports]; diff --git a/src/libstd/test.rs b/src/libstd/test.rs index b2dcf02a569..2bf735dd584 100644 --- a/src/libstd/test.rs +++ b/src/libstd/test.rs @@ -92,15 +92,9 @@ fn parse_opts(args: &[~str]) -> OptRes { return either::Left(test_opts); } +#[deriving_eq] pub enum TestResult { TrOk, TrFailed, TrIgnored, } -impl TestResult : Eq { - pure fn eq(&self, other: &TestResult) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &TestResult) -> bool { !(*self).eq(other) } -} - type ConsoleTestState = @{out: io::Writer, log_out: Option<io::Writer>, diff --git a/src/libstd/workcache.rs b/src/libstd/workcache.rs index 8b394443170..71151c33060 100644 --- a/src/libstd/workcache.rs +++ b/src/libstd/workcache.rs @@ -79,6 +79,7 @@ use serialization::{Serializer,Serializable, * */ +#[deriving_eq] struct WorkKey { kind: ~str, name: ~str @@ -100,15 +101,6 @@ impl WorkKey { } } -impl WorkKey: core::cmp::Eq { - pure fn eq(&self, other: &WorkKey) -> bool { - self.kind == other.kind && self.name == other.name - } - pure fn ne(&self, other: &WorkKey) -> bool { - self.kind != other.kind || self.name != other.name - } -} - type WorkMap = LinearMap<WorkKey, ~str>; struct Database { |
