mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2024-10-31 22:59:14 +00:00
fraction: add const new_raw and from_integer methods
These are direct counterparts to their respective Rational32 constructors. Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1404>
This commit is contained in:
parent
14576fdf26
commit
b5cb4ae831
1 changed files with 25 additions and 0 deletions
|
@ -9,12 +9,37 @@ use num_rational::Rational32;
|
||||||
pub struct Fraction(pub Rational32);
|
pub struct Fraction(pub Rational32);
|
||||||
|
|
||||||
impl Fraction {
|
impl Fraction {
|
||||||
|
// rustdoc-stripper-ignore-next
|
||||||
|
/// Creates a new `Ratio`.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if `denom` is zero.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(num: i32, den: i32) -> Self {
|
pub fn new(num: i32, den: i32) -> Self {
|
||||||
skip_assert_initialized!();
|
skip_assert_initialized!();
|
||||||
(num, den).into()
|
(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<Self> {
|
pub fn approximate_f32(x: f32) -> Option<Self> {
|
||||||
skip_assert_initialized!();
|
skip_assert_initialized!();
|
||||||
Rational32::approximate_float(x).map(|r| r.into())
|
Rational32::approximate_float(x).map(|r| r.into())
|
||||||
|
|
Loading…
Reference in a new issue