feat(util/rational): add Rational::non_zero for guarding against zero-valued Rationals

This commit is contained in:
tilpner
2023-08-29 15:53:09 +02:00
parent 56c3dd0f4a
commit 23dde387b7
+13
View File
@@ -14,6 +14,8 @@ use crate::ffi::*;
pub struct Rational(pub i32, pub i32);
impl Rational {
pub const ZERO: Rational = Rational(0, 1);
#[inline]
pub fn new(numerator: i32, denominator: i32) -> Self {
Rational(numerator, denominator)
@@ -69,6 +71,17 @@ impl Rational {
pub fn approx(&self) -> f64 {
self.0 as f64 / self.1 as f64
}
#[inline]
pub fn non_zero(self) -> Option<Rational> {
(self.numerator() != 0).then_some(self)
}
}
impl Default for Rational {
fn default() -> Self {
Rational::ZERO
}
}
impl From<AVRational> for Rational {