mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2024-12-22 16:16:37 +00:00
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:
parent
658b8c2231
commit
2ae1e4a511
2 changed files with 27 additions and 0 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue