From b5cb4ae831a2bf7a23a5bc14a36fe4f9dba61e44 Mon Sep 17 00:00:00 2001 From: Dave Patrick Caberto Date: Fri, 1 Mar 2024 11:57:58 +0800 Subject: [PATCH] fraction: add const new_raw and from_integer methods These are direct counterparts to their respective Rational32 constructors. Part-of: --- gstreamer/src/value.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/gstreamer/src/value.rs b/gstreamer/src/value.rs index 18ead030a..3722e1f65 100644 --- a/gstreamer/src/value.rs +++ b/gstreamer/src/value.rs @@ -9,12 +9,37 @@ use num_rational::Rational32; pub struct Fraction(pub Rational32); impl Fraction { + // rustdoc-stripper-ignore-next + /// Creates a new `Ratio`. + /// + /// # Panics + /// + /// Panics if `denom` is zero. #[inline] pub fn new(num: i32, den: i32) -> Self { skip_assert_initialized!(); (num, den).into() } + // rustdoc-stripper-ignore-next + /// Creates a `Fraction` without checking for `denom == 0` or reducing. + /// + /// While this does not panic, there are several methods that will panic + /// if used on a `Fraction` with `denom == 0`. + #[inline] + pub const fn new_raw(num: i32, den: i32) -> Self { + skip_assert_initialized!(); + Self(Rational32::new_raw(num, den)) + } + + // rustdoc-stripper-ignore-next + /// Creates a `Fraction` representing the integer `t`. + #[inline] + pub const fn from_integer(t: i32) -> Self { + skip_assert_initialized!(); + Self::new_raw(t, 1) + } + pub fn approximate_f32(x: f32) -> Option { skip_assert_initialized!(); Rational32::approximate_float(x).map(|r| r.into())