Skip to content
Merged
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
13 changes: 13 additions & 0 deletions approx/src/abs_diff_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use alloc::vec::Vec;
use core::cell;
#[cfg(feature = "indexmap_impl")]
use core::hash::{BuildHasher, Hash};
use core::time::Duration;
#[cfg(feature = "indexmap_impl")]
use indexmap::IndexMap;
#[cfg(feature = "num_complex")]
Expand Down Expand Up @@ -306,6 +307,18 @@ mod abs_diff_eq_tuple_impls {
impl_abs_diff_eq!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
}

impl AbsDiffEq for Duration {
type Epsilon = Self;

fn default_epsilon() -> Self::Epsilon {
Duration::from_nanos(0)
}

fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
self.abs_diff(*other) <= epsilon
}
}

#[cfg(feature = "vec_impl")]
#[cfg_attr(docsrs, doc(cfg(feature = "vec_impl")))]
impl<A, B> AbsDiffEq<Vec<B>> for Vec<A>
Expand Down
12 changes: 12 additions & 0 deletions approx/tests/abs_diff_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,18 @@ mod test_indexmap {
}
}

mod test_duration {
use core::time::Duration;

#[test]
fn test_basic() {
assert_abs_diff_eq!(Duration::from_secs(1), Duration::from_secs(1));
assert_abs_diff_ne!(Duration::from_secs(1), Duration::from_secs(2));
assert_abs_diff_ne!(Duration::from_secs(1), Duration::new(1, 10), epsilon = Duration::from_nanos(1));
assert_abs_diff_eq!(Duration::from_secs(1), Duration::new(1, 10), epsilon = Duration::from_nanos(100));
}
}

#[test]
fn test_debug_abs_diff() {
let x = 1.0;
Expand Down
Loading