value: Provide MulDiv extensions for fractions

This commit is contained in:
Jan Alexander Steffens (heftig) 2024-04-16 16:25:32 +02:00
parent 0d872ae6f8
commit 5a553a1ff6
No known key found for this signature in database
GPG key ID: 3B94A80E50A477C7
2 changed files with 29 additions and 1 deletions

View file

@ -353,7 +353,7 @@ pub mod prelude {
task_pool::{TaskHandle, TaskPoolExtManual},
typefind::TypeFindImpl,
utils::Displayable,
value::GstValueExt,
value::{GstValueExt, MulDivExtFraction},
};
}

View file

@ -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<Fraction> for Rational32 {
}
}
pub trait MulDivExtFraction {
type Output;
fn mul_frac_floor(self, frac: Fraction) -> Option<Self::Output>;
fn mul_frac_round(self, frac: Fraction) -> Option<Self::Output>;
fn mul_frac_ceil(self, frac: Fraction) -> Option<Self::Output>;
}
impl<T> MulDivExtFraction for T
where
T: MulDiv<i32>,
{
type Output = <Self as MulDiv<i32>>::Output;
fn mul_frac_floor(self, frac: Fraction) -> Option<Self::Output> {
self.mul_div_floor(frac.numer(), frac.denom())
}
fn mul_frac_round(self, frac: Fraction) -> Option<Self::Output> {
self.mul_div_round(frac.numer(), frac.denom())
}
fn mul_frac_ceil(self, frac: Fraction) -> Option<Self::Output> {
self.mul_div_ceil(frac.numer(), frac.denom())
}
}
impl glib::types::StaticType for Fraction {
#[inline]
fn static_type() -> glib::types::Type {