From 5a553a1ff65f5c215b95d6a0fea38d9f57a8e3dd Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Tue, 16 Apr 2024 16:25:32 +0200 Subject: [PATCH] value: Provide MulDiv extensions for fractions --- gstreamer/src/lib.rs | 2 +- gstreamer/src/value.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/gstreamer/src/lib.rs b/gstreamer/src/lib.rs index 2d65fa6ad..a683bc621 100644 --- a/gstreamer/src/lib.rs +++ b/gstreamer/src/lib.rs @@ -353,7 +353,7 @@ pub mod prelude { task_pool::{TaskHandle, TaskPoolExtManual}, typefind::TypeFindImpl, utils::Displayable, - value::GstValueExt, + value::{GstValueExt, MulDivExtFraction}, }; } diff --git a/gstreamer/src/value.rs b/gstreamer/src/value.rs index a30109c9b..ff738d286 100644 --- a/gstreamer/src/value.rs +++ b/gstreamer/src/value.rs @@ -3,6 +3,7 @@ use std::{cmp, fmt, ops, slice}; use glib::{prelude::*, translate::*}; +use muldiv::MulDiv; use num_rational::Rational32; #[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)] @@ -307,6 +308,33 @@ impl From for Rational32 { } } +pub trait MulDivExtFraction { + type Output; + + fn mul_frac_floor(self, frac: Fraction) -> Option; + fn mul_frac_round(self, frac: Fraction) -> Option; + fn mul_frac_ceil(self, frac: Fraction) -> Option; +} + +impl MulDivExtFraction for T +where + T: MulDiv, +{ + type Output = >::Output; + + fn mul_frac_floor(self, frac: Fraction) -> Option { + self.mul_div_floor(frac.numer(), frac.denom()) + } + + fn mul_frac_round(self, frac: Fraction) -> Option { + self.mul_div_round(frac.numer(), frac.denom()) + } + + fn mul_frac_ceil(self, frac: Fraction) -> Option { + self.mul_div_ceil(frac.numer(), frac.denom()) + } +} + impl glib::types::StaticType for Fraction { #[inline] fn static_type() -> glib::types::Type {