From 34fee6b6916db8c26898875e3677b4c75a541624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 10 Dec 2023 20:49:17 +0200 Subject: [PATCH] gstreamer: formatted values: Implement `ClockTime::absdiff()` and on related types Fixes https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/issues/494 Part-of: --- gstreamer/src/format/clock_time.rs | 11 +++++++++++ gstreamer/src/format/macros.rs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/gstreamer/src/format/clock_time.rs b/gstreamer/src/format/clock_time.rs index 0c9208d70..7e984d46a 100644 --- a/gstreamer/src/format/clock_time.rs +++ b/gstreamer/src/format/clock_time.rs @@ -1801,4 +1801,15 @@ mod tests { let neg = Signed::::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); + } } diff --git a/gstreamer/src/format/macros.rs b/gstreamer/src/format/macros.rs index ec3c5a515..51402d903 100644 --- a/gstreamer/src/format/macros.rs +++ b/gstreamer/src/format/macros.rs @@ -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 + } + } + } }; );