Skip to content

Commit cba3ea0

Browse files
committed
time: add MicroSeconds::diff()
1 parent 313bc79 commit cba3ea0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pulse-binding/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
* Made some improvements to `MicroSeconds`:
3434
- Added `MicroSeconds::{MIN, ZERO, SECOND, MILLISECOND}` associated constants.
3535
- Added `MicroSeconds::is_zero()`, `MicroSeconds::from_secs()`, `MicroSeconds::from_millis()`,
36-
`MicroSeconds::inner()`, `MicroSeconds::as_secs()`, `MicroSeconds::as_millis()`.
36+
`MicroSeconds::inner()`, `MicroSeconds::as_secs()`, `MicroSeconds::as_millis()`,
37+
and `MicroSeconds::diff()`.
3738
- Additionally added `MicroSeconds::from_secs_f64()`, `MicroSeconds::from_secs_f32()`,
3839
`MicroSeconds::as_secs_f64()` and `MicroSeconds::as_secs_f32()` for converting to/from
3940
floating point form, and `MicroSeconds::mul_f32()`, `MicroSeconds::mul_f64()`,

pulse-binding/src/time/microseconds.rs

+19
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,25 @@ impl MicroSeconds {
162162
millis.checked_mul(super::MICROS_PER_MILLI).and_then(|i| Some(Self(i)))
163163
}
164164

165+
/// Returns the absolute difference with `other`.
166+
///
167+
/// # Examples
168+
///
169+
/// ```rust
170+
/// # use libpulse_binding::time::MicroSeconds;
171+
/// assert_eq!(MicroSeconds(0).diff(MicroSeconds(0)), MicroSeconds(0));
172+
/// assert_eq!(MicroSeconds(100).diff(MicroSeconds(100)), MicroSeconds(0));
173+
/// assert_eq!(MicroSeconds(200).diff(MicroSeconds(150)), MicroSeconds(50));
174+
/// assert_eq!(MicroSeconds(150).diff(MicroSeconds(200)), MicroSeconds(50));
175+
/// ```
176+
#[inline]
177+
pub fn diff(self, other: Self) -> Self {
178+
match self >= other {
179+
true => Self(self.0 - other.0),
180+
false => Self(other.0 - self.0),
181+
}
182+
}
183+
165184
/// Returns the total number of whole seconds.
166185
///
167186
/// # Examples

0 commit comments

Comments
 (0)