gstreamer: format: Percent: add getters

We had constructor from the percent/ppm/ratio values but not getters
to get those values.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1488>
This commit is contained in:
Guillaume Desmottes 2024-07-26 09:27:43 +02:00
parent 658b8c2231
commit 2ae1e4a511
2 changed files with 27 additions and 0 deletions

View file

@ -115,6 +115,9 @@
//! //!
//! // `Percent` can be built from a percent integer value: //! // `Percent` can be built from a percent integer value:
//! let a_quarter = 25.percent(); //! let a_quarter = 25.percent();
//! assert_eq!(a_quarter.percent(), 25);
//! assert_eq!(a_quarter.ppm(), 250000);
//! assert_eq!(a_quarter.ratio(), 0.25);
//! // ... from a floating point ratio: //! // ... from a floating point ratio:
//! let a_quarter_from_ratio = 0.25.percent_ratio(); //! let a_quarter_from_ratio = 0.25.percent_ratio();
//! assert_eq!(a_quarter, a_quarter_from_ratio); //! assert_eq!(a_quarter, a_quarter_from_ratio);

View file

@ -336,6 +336,30 @@ impl Percent {
// FIXME floating point arithmetic is not allowed in constant functions (rustc 1.64.0) // FIXME floating point arithmetic is not allowed in constant functions (rustc 1.64.0)
Percent::try_from(ratio).expect("`Percent` ratio out of range") Percent::try_from(ratio).expect("`Percent` ratio out of range")
} }
// rustdoc-stripper-ignore-next
/// The percent value in the range [0, 100].
#[track_caller]
#[inline]
pub fn percent(&self) -> u32 {
self.0 / ffi::GST_FORMAT_PERCENT_SCALE as u32
}
// rustdoc-stripper-ignore-next
/// The per million value in the range [0, 100000].
#[track_caller]
#[inline]
pub fn ppm(&self) -> u32 {
self.0
}
// rustdoc-stripper-ignore-next
/// The ratio value in the range [0.0, 1.0].
#[track_caller]
#[inline]
pub fn ratio(&self) -> f32 {
self.0 as f32 / ffi::GST_FORMAT_PERCENT_MAX as f32
}
} }
impl_common_ops_for_newtype_uint!(Percent, u32, one: ffi::GST_FORMAT_PERCENT_SCALE as u32); impl_common_ops_for_newtype_uint!(Percent, u32, one: ffi::GST_FORMAT_PERCENT_SCALE as u32);