From f6336b1be3c9e01690bcc751db3f16feebdcc87e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Laignel?= Date: Sun, 9 Oct 2022 23:47:51 +0200 Subject: [PATCH] gst/format: fix `Percent` `Display` impl - The `GST_FORMAT_PERCENT_SCALE` was not used to compute the value to display. - Added `Display` examples in the format module documentation. - Simplified `glib_newtype_display` macro. --- gstreamer/src/format/generic.rs | 2 +- gstreamer/src/format/macros.rs | 31 +++++++--------- gstreamer/src/format/mod.rs | 10 +++++- gstreamer/src/format/specific.rs | 60 ++++++++++++++++++------------- gstreamer/src/format/undefined.rs | 2 +- 5 files changed, 60 insertions(+), 45 deletions(-) diff --git a/gstreamer/src/format/generic.rs b/gstreamer/src/format/generic.rs index f05095be8..e69174fa8 100644 --- a/gstreamer/src/format/generic.rs +++ b/gstreamer/src/format/generic.rs @@ -22,7 +22,7 @@ impl Other { impl_common_ops_for_newtype_uint!(Other, u64); impl_signed_div_mul!(Other, u64); option_glib_newtype_from_to!(Other, u64::MAX); -glib_newtype_display!(Other, DisplayableOther, DisplayableOptionOther); +glib_newtype_display!(Other, DisplayableOptionOther); impl TryFrom for Other { type Error = GlibNoneError; diff --git a/gstreamer/src/format/macros.rs b/gstreamer/src/format/macros.rs index ea3378538..fb0bcf109 100644 --- a/gstreamer/src/format/macros.rs +++ b/gstreamer/src/format/macros.rs @@ -1531,13 +1531,11 @@ macro_rules! option_glib_newtype_from_to { }; } -// FIXME we could automatically build `$displayable_name` and -// `$displayable_option_name` if `concat_idents!` was stable. +// FIXME we could automatically build `$displayable_option_name` +// if `concat_idents!` was stable. // See: https://doc.rust-lang.org/std/macro.concat_idents.html macro_rules! glib_newtype_display { - ($typ:ty, $displayable_name:ident) => { - pub struct $displayable_name($typ); - + ($typ:ty) => { impl std::fmt::Display for $typ { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { std::fmt::Display::fmt(&self.0, f) @@ -1545,16 +1543,14 @@ macro_rules! glib_newtype_display { } impl crate::utils::Displayable for $typ { - type DisplayImpl = $typ; - fn display(self) -> $typ { + type DisplayImpl = Self; + fn display(self) -> Self { self } } }; - ($typ:ty, $displayable_name:ident, Format::$format:ident$(,)?) => { - pub struct $displayable_name($typ); - + ($typ:ty, Format::$format:ident) => { impl std::fmt::Display for $typ { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { std::fmt::Display::fmt(&self.0, f)?; @@ -1564,15 +1560,15 @@ macro_rules! glib_newtype_display { } impl crate::utils::Displayable for $typ { - type DisplayImpl = $typ; - fn display(self) -> $typ { + type DisplayImpl = Self; + fn display(self) -> Self { self } } }; - ($typ:ty, $displayable_name:ident, $displayable_option_name:ident) => { - glib_newtype_display!($typ, $displayable_name); + ($typ:ty, $displayable_option_name:ident) => { + glib_newtype_display!($typ); pub struct $displayable_option_name(Option<$typ>); @@ -1594,8 +1590,8 @@ macro_rules! glib_newtype_display { } }; - ($typ:ty, $displayable_name:ident, $displayable_option_name:ident, Format::$format:ident$(,)?) => { - glib_newtype_display!($typ, $displayable_name, Format::$format); + ($typ:ty, $displayable_option_name:ident, Format::$format:ident$(,)?) => { + glib_newtype_display!($typ, Format::$format); pub struct $displayable_option_name(Option<$typ>); @@ -1604,8 +1600,7 @@ macro_rules! glib_newtype_display { if let Some(val) = self.0.as_ref() { std::fmt::Display::fmt(val, f) } else { - f.write_str("undef. ")?; - std::fmt::Display::fmt(&Format::$format, f) + write!(f, "undef. {}", Format::$format) } } } diff --git a/gstreamer/src/format/mod.rs b/gstreamer/src/format/mod.rs index cf92c9f9f..cd5489d7c 100644 --- a/gstreamer/src/format/mod.rs +++ b/gstreamer/src/format/mod.rs @@ -72,6 +72,10 @@ //! //! assert_eq!(format!("{}", time), "12:43:54.908569837"); //! assert_eq!(format!("{:.0}", time), "12:43:54"); +//! +//! let percent = gst::format::Percent::try_from(0.1234).unwrap(); +//! assert_eq!(format!("{}", percent), "12.34 %"); +//! assert_eq!(format!("{:5.1}", percent), " 12.3 %"); //! ``` //! //! ## Some operations available on specific formatted values @@ -858,13 +862,17 @@ mod tests { assert_eq!(&format!("{}", Some(bytes).display()), "42 bytes"); assert_eq!(&format!("{}", Bytes::NONE.display()), "undef. bytes"); - let gv_1 = GenericFormattedValue::Percent(Some(Percent(42))); + let gv_1 = GenericFormattedValue::Percent(Percent::try_from(0.42).ok()); assert_eq!(&format!("{gv_1}"), "42 %"); assert_eq!( &format!("{}", GenericFormattedValue::Percent(None)), "undef. %" ); + let percent = Percent::try_from(0.1234).unwrap(); + assert_eq!(&format!("{percent}"), "12.34 %"); + assert_eq!(&format!("{percent:5.1}"), " 12.3 %"); + let other: Other = 42.try_into().unwrap(); assert_eq!(&format!("{other}"), "42"); diff --git a/gstreamer/src/format/specific.rs b/gstreamer/src/format/specific.rs index ce77cb643..91f193e7e 100644 --- a/gstreamer/src/format/specific.rs +++ b/gstreamer/src/format/specific.rs @@ -32,12 +32,7 @@ impl_common_ops_for_newtype_uint!(Buffers, u64); impl_signed_div_mul!(Buffers, u64); impl_format_value_traits!(Buffers, Buffers, Buffers, u64); option_glib_newtype_from_to!(Buffers, Buffers::OFFSET_NONE); -glib_newtype_display!( - Buffers, - DisplayableBuffers, - DisplayableOptionBuffers, - Format::Buffers, -); +glib_newtype_display!(Buffers, DisplayableOptionBuffers, Format::Buffers); #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)] pub struct Bytes(pub u64); @@ -49,12 +44,7 @@ impl_common_ops_for_newtype_uint!(Bytes, u64); impl_signed_div_mul!(Bytes, u64); impl_format_value_traits!(Bytes, Bytes, Bytes, u64); option_glib_newtype_from_to!(Bytes, u64::MAX); -glib_newtype_display!( - Bytes, - DisplayableBytes, - DisplayableOptionBytes, - Format::Bytes, -); +glib_newtype_display!(Bytes, DisplayableOptionBytes, Format::Bytes); #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)] pub struct Default(pub u64); @@ -66,12 +56,7 @@ impl_common_ops_for_newtype_uint!(Default, u64); impl_signed_div_mul!(Default, u64); impl_format_value_traits!(Default, Default, Default, u64); option_glib_newtype_from_to!(Default, u64::MAX); -glib_newtype_display!( - Default, - DisplayableDefault, - DisplayableOptionDefault, - Format::Default, -); +glib_newtype_display!(Default, DisplayableOptionDefault, Format::Default); pub type Time = super::ClockTime; @@ -86,12 +71,6 @@ impl Percent { impl_common_ops_for_newtype_uint!(Percent, u32); impl_signed_div_mul!(Percent, u32); -glib_newtype_display!( - Percent, - DisplayablePercent, - DisplayableOptionPercent, - Format::Percent, -); impl FormattedValue for Option { type FullRange = Option; @@ -245,3 +224,36 @@ impl TryFrom for Percent { } } } + +impl std::fmt::Display for Percent { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + const ONE_PERCENT: f32 = Percent::SCALE as f32 / 100.0; + std::fmt::Display::fmt(&(self.0 as f32 / ONE_PERCENT), f)?; + f.write_str(" %") + } +} + +impl crate::utils::Displayable for Percent { + type DisplayImpl = Self; + fn display(self) -> Self { + self + } +} +pub struct DisplayableOptionPercent(Option); + +impl std::fmt::Display for DisplayableOptionPercent { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(val) = self.0.as_ref() { + std::fmt::Display::fmt(val, f) + } else { + f.write_str("undef. %") + } + } +} + +impl crate::utils::Displayable for Option { + type DisplayImpl = DisplayableOptionPercent; + fn display(self) -> Self::DisplayImpl { + DisplayableOptionPercent(self) + } +} diff --git a/gstreamer/src/format/undefined.rs b/gstreamer/src/format/undefined.rs index 164909672..8ee1350f1 100644 --- a/gstreamer/src/format/undefined.rs +++ b/gstreamer/src/format/undefined.rs @@ -108,4 +108,4 @@ impl From for Signed { } } -glib_newtype_display!(Undefined, DisplayableUndefined, Format::Undefined); +glib_newtype_display!(Undefined, Format::Undefined);