ApproxEq

Trait ApproxEq 

Source
pub trait ApproxEq<Rhs: ?Sized = Self> {
    // Required method
    fn aeq(&self, other: &Rhs) -> bool;

    // Provided method
    fn nae(&self, other: &Rhs) -> bool { ... }
}
Expand description

Trait for equality comparisons that are approximately equal

This trait allows for approximate equality, for results that just have to be “good enough”.

Herein a ~= b implies that a.aeq(b) and a !~= implies a.nae(b).

The approximate equality, however, must be (for all a, b and c) symmetric, in that: a ~= b implies b ~= a.

§How can I implement ApproxEq?

ApproxEq only requires the [aeq] method be implemented; [nae] is defined in terms of it by default. Any implementation of [ane] must respect the rule that [aeq] is a strict inverse of [ane]; that is, !(a ~= b) if and only if a !~= b.

An example implementation for a domain in which two books are considered the same book if their ISBNs have the same parity, even if the formats differ:

use approxeq::ApproxEq;

enum BookFormat {
    Paperback,
    Hardback,
    Ebook,
}

struct Book {
    isbn: i32,
    format: BookFormat,
}

impl ApproxEq for Book {
    fn aeq(&self, other: &Self) -> bool {
      self.isbn % 2 == other.isbn % 2
    }
}

let b1 = Book { isbn: 3, format: BookFormat::Paperback };
let b2 = Book { isbn: 3, format: BookFormat::Ebook };
let b3 = Book { isbn: 10, format: BookFormat::Paperback };

assert!(b1.aeq(&b2));
assert!(b1.nae(&b3));

§How can I compare two different types?

The type you can compare with is controlled by ApproxEq’s type parameter. For example, let’s tweak our previous code a bit:

use approxeq::ApproxEq;

#[derive(PartialEq)]
enum BookFormat {
    Paperback,
    Hardback,
    Ebook,
}

struct Book {
    isbn: i32,
    format: BookFormat,
}

// Implement <Book> ~= <BookFormat> comparisons
impl ApproxEq<BookFormat> for Book {
    fn aeq(&self, other: &BookFormat) -> bool {
        match self.format {
            BookFormat::Ebook => self.format == *other,
            _ => other != &BookFormat::Ebook,
        }
    }
}

// Implement <BookFormat> ~= <Book> comparisons
impl ApproxEq<Book> for BookFormat {
    fn aeq(&self, other: &Book) -> bool {
        *self == BookFormat::Ebook && other.format == BookFormat::Ebook ||
            *self != BookFormat::Ebook && other.format != BookFormat::Ebook
    }
}

let b1 = Book { isbn: 3, format: BookFormat::Paperback };

assert!(b1.aeq(&BookFormat::Paperback));
assert!(BookFormat::Ebook.nae(&b1));

By changing impl ApproxEq for Book to impl ApproxEq<BookFormat> for Book, we allow BookFormats to be compared with Books.

Required Methods§

Source

fn aeq(&self, other: &Rhs) -> bool

Provided Methods§

Source

fn nae(&self, other: &Rhs) -> bool

Implementations on Foreign Types§

Source§

impl ApproxEq for f32

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq for f64

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq for i8

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq for i16

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq for i32

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq for i64

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq for i128

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq for isize

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq for u8

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq for u16

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq for u32

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq for u64

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq for u128

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq for usize

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<f32> for f64

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for i8

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for i16

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for i32

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for i64

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for i128

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for isize

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for u8

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for u16

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for u32

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for u64

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for u128

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f32> for usize

Source§

fn aeq(&self, other: &f32) -> bool

Source§

impl ApproxEq<f64> for f32

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for i8

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for i16

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for i32

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for i64

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for i128

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for isize

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for u8

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for u16

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for u32

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for u64

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for u128

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<f64> for usize

Source§

fn aeq(&self, other: &f64) -> bool

Source§

impl ApproxEq<i8> for f32

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for f64

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for i16

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for i32

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for i64

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for i128

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for isize

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for u8

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for u16

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for u32

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for u64

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for u128

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i8> for usize

Source§

fn aeq(&self, other: &i8) -> bool

Source§

impl ApproxEq<i16> for f32

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for f64

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for i8

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for i32

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for i64

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for i128

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for isize

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for u8

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for u16

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for u32

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for u64

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for u128

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i16> for usize

Source§

fn aeq(&self, other: &i16) -> bool

Source§

impl ApproxEq<i32> for f32

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for f64

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for i8

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for i16

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for i64

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for i128

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for isize

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for u8

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for u16

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for u32

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for u64

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for u128

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i32> for usize

Source§

fn aeq(&self, other: &i32) -> bool

Source§

impl ApproxEq<i64> for f32

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for f64

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for i8

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for i16

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for i32

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for i128

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for isize

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for u8

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for u16

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for u32

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for u64

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for u128

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i64> for usize

Source§

fn aeq(&self, other: &i64) -> bool

Source§

impl ApproxEq<i128> for f32

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for f64

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for i8

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for i16

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for i32

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for i64

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for isize

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for u8

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for u16

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for u32

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for u64

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for u128

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<i128> for usize

Source§

fn aeq(&self, other: &i128) -> bool

Source§

impl ApproxEq<isize> for f32

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for f64

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for i8

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for i16

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for i32

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for i64

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for i128

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for u8

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for u16

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for u32

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for u64

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for u128

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<isize> for usize

Source§

fn aeq(&self, other: &isize) -> bool

Source§

impl ApproxEq<u8> for f32

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for f64

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for i8

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for i16

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for i32

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for i64

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for i128

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for isize

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for u16

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for u32

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for u64

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for u128

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u8> for usize

Source§

fn aeq(&self, other: &u8) -> bool

Source§

impl ApproxEq<u16> for f32

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for f64

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for i8

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for i16

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for i32

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for i64

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for i128

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for isize

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for u8

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for u32

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for u64

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for u128

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u16> for usize

Source§

fn aeq(&self, other: &u16) -> bool

Source§

impl ApproxEq<u32> for f32

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for f64

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for i8

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for i16

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for i32

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for i64

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for i128

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for isize

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for u8

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for u16

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for u64

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for u128

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u32> for usize

Source§

fn aeq(&self, other: &u32) -> bool

Source§

impl ApproxEq<u64> for f32

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for f64

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for i8

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for i16

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for i32

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for i64

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for i128

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for isize

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for u8

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for u16

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for u32

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for u128

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u64> for usize

Source§

fn aeq(&self, other: &u64) -> bool

Source§

impl ApproxEq<u128> for f32

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for f64

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for i8

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for i16

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for i32

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for i64

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for i128

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for isize

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for u8

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for u16

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for u32

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for u64

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<u128> for usize

Source§

fn aeq(&self, other: &u128) -> bool

Source§

impl ApproxEq<usize> for f32

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for f64

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for i8

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for i16

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for i32

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for i64

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for i128

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for isize

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for u8

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for u16

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for u32

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for u64

Source§

fn aeq(&self, other: &usize) -> bool

Source§

impl ApproxEq<usize> for u128

Source§

fn aeq(&self, other: &usize) -> bool

Implementors§