Skip to content

Commit

Permalink
Implement *Eq traits for slices of different types
Browse files Browse the repository at this point in the history
  • Loading branch information
jturner314 committed Jan 11, 2019
1 parent 5d12593 commit 545db1d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
15 changes: 8 additions & 7 deletions src/abs_diff_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,22 @@ impl<T: AbsDiffEq + ?Sized> AbsDiffEq for cell::RefCell<T> {
}
}

impl<T: AbsDiffEq> AbsDiffEq for [T]
impl<A, B> AbsDiffEq<[B]> for [A]
where
T::Epsilon: Clone,
A: AbsDiffEq<B>,
A::Epsilon: Clone,
{
type Epsilon = T::Epsilon;
type Epsilon = A::Epsilon;

#[inline]
fn default_epsilon() -> T::Epsilon {
T::default_epsilon()
fn default_epsilon() -> A::Epsilon {
A::default_epsilon()
}

#[inline]
fn abs_diff_eq(&self, other: &[T], epsilon: T::Epsilon) -> bool {
fn abs_diff_eq(&self, other: &[B], epsilon: A::Epsilon) -> bool {
self.len() == other.len()
&& Iterator::zip(self.iter(), other).all(|(x, y)| T::abs_diff_eq(x, y, epsilon.clone()))
&& Iterator::zip(self.iter(), other).all(|(x, y)| A::abs_diff_eq(x, y, epsilon.clone()))
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/relative_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,21 @@ impl<T: RelativeEq + ?Sized> RelativeEq for cell::RefCell<T> {
}
}

impl<T: RelativeEq> RelativeEq for [T]
impl<A, B> RelativeEq<[B]> for [A]
where
T::Epsilon: Clone,
A: RelativeEq<B>,
A::Epsilon: Clone,
{
#[inline]
fn default_max_relative() -> T::Epsilon {
T::default_max_relative()
fn default_max_relative() -> A::Epsilon {
A::default_max_relative()
}

#[inline]
fn relative_eq(&self, other: &[T], epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
fn relative_eq(&self, other: &[B], epsilon: A::Epsilon, max_relative: A::Epsilon) -> bool {
self.len() == other.len()
&& Iterator::zip(self.iter(), other)
.all(|(x, y)| T::relative_eq(x, y, epsilon.clone(), max_relative.clone()))
.all(|(x, y)| A::relative_eq(x, y, epsilon.clone(), max_relative.clone()))
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/ulps_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,21 @@ impl<T: UlpsEq + ?Sized> UlpsEq for cell::RefCell<T> {
}
}

impl<T: UlpsEq> UlpsEq for [T]
impl<A, B> UlpsEq<[B]> for [A]
where
T::Epsilon: Clone,
A: UlpsEq<B>,
A::Epsilon: Clone,
{
#[inline]
fn default_max_ulps() -> u32 {
T::default_max_ulps()
A::default_max_ulps()
}

#[inline]
fn ulps_eq(&self, other: &[T], epsilon: T::Epsilon, max_ulps: u32) -> bool {
fn ulps_eq(&self, other: &[B], epsilon: A::Epsilon, max_ulps: u32) -> bool {
self.len() == other.len()
&& Iterator::zip(self.iter(), other)
.all(|(x, y)| T::ulps_eq(x, y, epsilon.clone(), max_ulps.clone()))
.all(|(x, y)| A::ulps_eq(x, y, epsilon.clone(), max_ulps.clone()))
}
}

Expand Down

0 comments on commit 545db1d

Please sign in to comment.