From 23b414b01e449aa084cd33e31ebeea535334b222 Mon Sep 17 00:00:00 2001 From: meh Date: Wed, 24 Jun 2015 22:54:52 +0200 Subject: [PATCH] util/rational: some minor improvements --- src/util/rational.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/util/rational.rs b/src/util/rational.rs index 546f3d0..db11f99 100644 --- a/src/util/rational.rs +++ b/src/util/rational.rs @@ -21,7 +21,14 @@ impl Rational { self.0.den as i32 } - pub fn reduce(&self, max: i32) -> (Rational, bool) { + pub fn reduce(&self) -> Rational { + match self.reduce_with_limit(i32::max_value()) { + Ok(r) => r, + Err(r) => r, + } + } + + pub fn reduce_with_limit(&self, max: i32) -> Result { unsafe { let mut dst_num: c_int = 0; let mut dst_den: c_int = 0; @@ -30,7 +37,12 @@ impl Rational { self.numerator() as int64_t, self.denominator() as int64_t, max as int64_t); - (Rational::new(dst_num, dst_den), exact == 1) + if exact == 1 { + Ok(Rational::new(dst_num, dst_den)) + } + else { + Err(Rational::new(dst_num, dst_den)) + } } } @@ -61,10 +73,18 @@ impl From for Rational { } } -impl Into for Rational { - fn into(self) -> f64 { +impl From for f64 { + fn from(value: Rational) -> f64 { unsafe { - av_q2d(self.0) + av_q2d(value.0) + } + } +} + +impl From for u32 { + fn from(value: Rational) -> u32 { + unsafe { + av_q2intfloat(value.0) } } }