gstreamer: formatted values: Implement ClockTime::absdiff() and on related types

Fixes https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/issues/494

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1364>
This commit is contained in:
Sebastian Dröge 2023-12-10 20:49:17 +02:00
parent fddeacc358
commit 34fee6b691
2 changed files with 22 additions and 0 deletions

View file

@ -1801,4 +1801,15 @@ mod tests {
let neg = Signed::<ClockTime>::from_seconds_f64(-5.0);
assert!(neg.is_negative());
}
#[test]
fn absdiff() {
let t1 = ClockTime::from_seconds(10);
let t2 = ClockTime::from_seconds(4);
let d = ClockTime::from_seconds(6);
assert_eq!(t1.absdiff(t2), d);
assert_eq!(t2.absdiff(t1), d);
}
}

View file

@ -93,6 +93,17 @@ macro_rules! impl_non_trait_op_same(
pub const fn wrapping_sub(self, rhs: Self) -> Self {
self.overflowing_sub(rhs).0
}
#[must_use = "this returns the result of the operation, without modifying the original"]
#[inline]
pub fn absdiff(self, rhs: Self) -> Self {
if self > rhs {
self - rhs
} else {
rhs - self
}
}
}
};
);