Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array blanket implementations. #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ keywords = [
]

[package.metadata.docs.rs]
features = ["std", "num-complex"]
features = ["std", "num-complex", "array_impl"]

[lib]
name = "approx"

[features]
default = ["std"]
std = []
array_impl = []

[dependencies]
num-traits = { version = "0.2.0", default_features = false }
Expand Down
20 changes: 20 additions & 0 deletions src/abs_diff_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ where
}
}

#[cfg(feature = "array_impl")]
impl<A, B, const N: usize> AbsDiffEq<[B; N]> for [A; N]
where
A: AbsDiffEq<B>,
A::Epsilon: Clone,
{
type Epsilon = A::Epsilon;

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

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

#[cfg(feature = "num-complex")]
impl<T: AbsDiffEq> AbsDiffEq for Complex<T>
where
Expand Down
19 changes: 19 additions & 0 deletions src/relative_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ where
}
}

#[cfg(feature = "array_impl")]
impl<A, B, const N: usize> RelativeEq<[B; N]> for [A; N]
where
A: RelativeEq<B>,
A::Epsilon: Clone,
{
#[inline]
fn default_max_relative() -> A::Epsilon {
A::default_max_relative()
}

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

#[cfg(feature = "num-complex")]
impl<T: RelativeEq> RelativeEq for Complex<T>
where
Expand Down
19 changes: 19 additions & 0 deletions src/ulps_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ where
}
}

#[cfg(feature = "array_impl")]
impl<A, B, const N: usize> UlpsEq<[B; N]> for [A; N]
where
A: UlpsEq<B>,
A::Epsilon: Clone,
{
#[inline]
fn default_max_ulps() -> u32 {
A::default_max_ulps()
}

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

#[cfg(feature = "num-complex")]
impl<T: UlpsEq> UlpsEq for Complex<T>
where
Expand Down
19 changes: 19 additions & 0 deletions tests/abs_diff_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,25 @@ mod test_slice {
}
}

#[cfg(feature = "array_impl")]
mod test_array {
mod test_f32 {
#[test]
fn test_basic() {
assert_abs_diff_eq!([1.0f32, 2.0f32], [1.0f32, 2.0f32]);
assert_abs_diff_ne!([1.0f32, 2.0f32], [2.0f32, 1.0f32]);
}
}

mod test_f64 {
#[test]
fn test_basic() {
assert_abs_diff_eq!([1.0f64, 2.0f64], [1.0f64, 2.0f64]);
assert_abs_diff_ne!([1.0f64, 2.0f64], [2.0f64, 1.0f64]);
}
}
}

#[cfg(feature = "num-complex")]
mod test_complex {
extern crate num_complex;
Expand Down
19 changes: 19 additions & 0 deletions tests/relative_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,25 @@ mod test_slice {
}
}

#[cfg(feature = "array_impl")]
mod test_array {
mod test_f32 {
#[test]
fn test_basic() {
assert_relative_eq!([1.0f32, 2.0f32], [1.0f32, 2.0f32]);
assert_relative_ne!([1.0f32, 2.0f32], [2.0f32, 1.0f32]);
}
}

mod test_f64 {
#[test]
fn test_basic() {
assert_relative_eq!([1.0f64, 2.0f64], [1.0f64, 2.0f64]);
assert_relative_ne!([1.0f64, 2.0f64], [2.0f64, 1.0f64]);
}
}
}

#[cfg(feature = "num-complex")]
mod test_complex {
extern crate num_complex;
Expand Down
19 changes: 19 additions & 0 deletions tests/ulps_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,25 @@ mod test_slice {
}
}

#[cfg(feature = "array_impl")]
mod test_array {
mod test_f32 {
#[test]
fn test_basic() {
assert_ulps_eq!([1.0f32, 2.0f32], [1.0f32, 2.0f32]);
assert_ulps_ne!([1.0f32, 2.0f32], [2.0f32, 1.0f32]);
}
}

mod test_f64 {
#[test]
fn test_basic() {
assert_ulps_eq!([1.0f64, 2.0f64], [1.0f64, 2.0f64]);
assert_ulps_ne!([1.0f64, 2.0f64], [2.0f64, 1.0f64]);
}
}
}

#[cfg(feature = "num-complex")]
mod test_complex {
extern crate num_complex;
Expand Down