Replace some Eq impls with deriving_eq

This commit is contained in:
Brian Anderson 2012-12-11 15:16:36 -08:00
parent 742f354ffb
commit d809e89c26
13 changed files with 24 additions and 263 deletions

View File

@ -240,6 +240,8 @@ mod core {
pub const warn : u32 = 2_u32;
pub const info : u32 = 3_u32;
pub const debug : u32 = 4_u32;
pub use cmp;
}

View File

@ -18,6 +18,7 @@
use result::Result;
/// The either type
#[deriving_eq]
pub enum Either<T, U> {
Left(T),
Right(U)
@ -141,26 +142,6 @@ pub fn partition<T: Copy, U: Copy>(eithers: &[Either<T, U>])
}
}
impl<T:Eq,U:Eq> Either<T,U> : Eq {
pure fn eq(&self, other: &Either<T,U>) -> bool {
match (*self) {
Left(ref a) => {
match (*other) {
Left(ref b) => (*a).eq(b),
Right(_) => false
}
}
Right(ref a) => {
match (*other) {
Left(_) => false,
Right(ref b) => (*a).eq(b)
}
}
}
}
pure fn ne(&self, other: &Either<T,U>) -> bool { !(*self).eq(other) }
}
#[test]
fn test_either_left() {
let val = Left(10);

View File

@ -445,24 +445,9 @@ pub enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
};
}
#[deriving_eq]
pub enum PadMode { PadSigned, PadUnsigned, PadNozero, PadFloat }
pub impl PadMode : Eq {
pure fn eq(&self, other: &PadMode) -> bool {
match ((*self), (*other)) {
(PadSigned, PadSigned) => true,
(PadUnsigned, PadUnsigned) => true,
(PadNozero, PadNozero) => true,
(PadFloat, PadFloat) => true,
(PadSigned, _) => false,
(PadUnsigned, _) => false,
(PadNozero, _) => false,
(PadFloat, _) => false
}
}
pure fn ne(&self, other: &PadMode) -> bool { !(*self).eq(other) }
}
pub fn pad(cv: Conv, s: ~str, mode: PadMode) -> ~str {
let mut s = move s; // sadtimes
let uwidth : uint = match cv.width {

View File

@ -523,18 +523,9 @@ fn tell() -> uint { self.pos }
pub enum FileFlag { Append, Create, Truncate, NoFlag, }
// What type of writer are we?
#[deriving_eq]
pub enum WriterType { Screen, File }
pub impl WriterType : Eq {
pure fn eq(&self, other: &WriterType) -> bool {
match ((*self), (*other)) {
(Screen, Screen) | (File, File) => true,
(Screen, _) | (File, _) => false
}
}
pure fn ne(&self, other: &WriterType) -> bool { !(*self).eq(other) }
}
// FIXME (#2004): Seekable really should be orthogonal.
// FIXME (#2004): eventually u64
/// The raw underlying writer trait. All writers must implement this.

View File

@ -47,6 +47,7 @@
use cmp::Eq;
/// The option type
#[deriving_eq]
pub enum Option<T> {
None,
Some(T),
@ -310,27 +311,6 @@ impl<T: Copy> Option<T> {
pure fn while_some(blk: fn(v: T) -> Option<T>) { while_some(self, blk) }
}
impl<T: Eq> Option<T> : Eq {
pure fn eq(&self, other: &Option<T>) -> bool {
match (*self) {
None => {
match (*other) {
None => true,
Some(_) => false
}
}
Some(ref self_contents) => {
match (*other) {
None => false,
Some(ref other_contents) =>
(*self_contents).eq(other_contents)
}
}
}
}
pure fn ne(&self, other: &Option<T>) -> bool { !(*self).eq(other) }
}
#[test]
fn test_unwrap_ptr() {
let x = ~0;

View File

@ -20,6 +20,7 @@
use cmp::Eq;
#[deriving_eq]
pub struct WindowsPath {
host: Option<~str>,
device: Option<~str>,
@ -31,6 +32,7 @@ pub struct WindowsPath {
from_str(s)
}
#[deriving_eq]
pub struct PosixPath {
is_absolute: bool,
components: ~[~str],
@ -356,24 +358,6 @@ impl PosixPath : ToStr {
}
}
impl PosixPath : Eq {
pure fn eq(&self, other: &PosixPath) -> bool {
return (*self).is_absolute == (*other).is_absolute &&
(*self).components == (*other).components;
}
pure fn ne(&self, other: &PosixPath) -> bool { !(*self).eq(other) }
}
impl WindowsPath : Eq {
pure fn eq(&self, other: &WindowsPath) -> bool {
return (*self).host == (*other).host &&
(*self).device == (*other).device &&
(*self).is_absolute == (*other).is_absolute &&
(*self).components == (*other).components;
}
pure fn ne(&self, other: &WindowsPath) -> bool { !(*self).eq(other) }
}
// FIXME (#3227): when default methods in traits are working, de-duplicate
// PosixPath and WindowsPath, most of their methods are common.
impl PosixPath : GenericPath {

View File

@ -518,6 +518,7 @@ fn test_repr2() {
// Old non-factored implementation, transitional...
#[deriving_eq]
enum EnumVisitState {
PreVariant, // We're before the variant we're interested in.
InVariant, // We're inside the variant we're interested in.
@ -525,13 +526,6 @@ enum EnumVisitState {
Degenerate // This is a degenerate enum (exactly 1 variant)
}
impl EnumVisitState : cmp::Eq {
pure fn eq(&self, other: &EnumVisitState) -> bool {
((*self) as uint) == ((*other) as uint)
}
pure fn ne(&self, other: &EnumVisitState) -> bool { !(*self).eq(other) }
}
struct EnumState {
end_ptr: *c_void,
state: EnumVisitState

View File

@ -19,6 +19,7 @@
use either::Either;
/// The result type
#[deriving_eq]
pub enum Result<T, U> {
/// Contains the successful result value
Ok(T),
@ -374,26 +375,6 @@ pub fn unwrap_err<T, U>(res: Result<T, U>) -> U {
}
}
impl<T:Eq,U:Eq> Result<T,U> : Eq {
pure fn eq(&self, other: &Result<T,U>) -> bool {
match (*self) {
Ok(ref e0a) => {
match (*other) {
Ok(ref e0b) => *e0a == *e0b,
_ => false
}
}
Err(ref e0a) => {
match (*other) {
Err(ref e0b) => *e0a == *e0b,
_ => false
}
}
}
}
pure fn ne(&self, other: &Result<T,U>) -> bool { !(*self).eq(other) }
}
#[cfg(test)]
#[allow(non_implicitly_copyable_typarams)]
mod tests {

View File

@ -55,6 +55,7 @@ pub enum Task {
TaskHandle(task_id)
}
// XXX: deriving
impl Task : cmp::Eq {
pure fn eq(&self, other: &Task) -> bool { *(*self) == *(*other) }
pure fn ne(&self, other: &Task) -> bool { !(*self).eq(other) }

View File

@ -79,16 +79,20 @@
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 struct OptGroup {
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 {

View File

@ -16,6 +16,7 @@
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];

View File

@ -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>,

View File

@ -79,6 +79,7 @@
*
*/
#[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 {