From 2ae1e4a511c42ebf9b75ed2a2f2a8539efd44023 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Fri, 26 Jul 2024 09:27:43 +0200 Subject: [PATCH] gstreamer: format: Percent: add getters We had constructor from the percent/ppm/ratio values but not getters to get those values. Part-of: --- gstreamer/src/format/mod.rs | 3 +++ gstreamer/src/format/specific.rs | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/gstreamer/src/format/mod.rs b/gstreamer/src/format/mod.rs index d555c8793..ce9c21177 100644 --- a/gstreamer/src/format/mod.rs +++ b/gstreamer/src/format/mod.rs @@ -115,6 +115,9 @@ //! //! // `Percent` can be built from a percent integer value: //! 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: //! let a_quarter_from_ratio = 0.25.percent_ratio(); //! assert_eq!(a_quarter, a_quarter_from_ratio); diff --git a/gstreamer/src/format/specific.rs b/gstreamer/src/format/specific.rs index 182c50ce5..e055acac6 100644 --- a/gstreamer/src/format/specific.rs +++ b/gstreamer/src/format/specific.rs @@ -336,6 +336,30 @@ impl Percent { // FIXME floating point arithmetic is not allowed in constant functions (rustc 1.64.0) 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);