From e0d9f886e330b2a0075996735614c170730b6263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Laignel?= Date: Thu, 15 Sep 2022 20:06:43 +0200 Subject: [PATCH] gst/format: add Signed::{positive, negative} It's more convenient to use than: ```rust signed_val.positive_or(()).ok() ``` --- gstreamer/src/format.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/gstreamer/src/format.rs b/gstreamer/src/format.rs index a01c878cd..8836924a7 100644 --- a/gstreamer/src/format.rs +++ b/gstreamer/src/format.rs @@ -28,6 +28,16 @@ impl Signed { matches!(self, Signed::Positive(_)) } + // rustdoc-stripper-ignore-next + /// Returns `Some(value)`, where `value` is the inner value, + /// if `self` is positive. + pub fn positive(self) -> Option { + match self { + Signed::Positive(val) => Some(val), + Signed::Negative(_) => None, + } + } + // rustdoc-stripper-ignore-next /// Transforms the `Signed` into a `Result`, /// mapping `Positive(v)` to `Ok(v)` and `Negative(_)` to `Err(err)`. @@ -52,6 +62,16 @@ impl Signed { matches!(self, Signed::Negative(_)) } + // rustdoc-stripper-ignore-next + /// Returns `Some(value)`, where `value` is the inner value, + /// if `self` is negative. + pub fn negative(self) -> Option { + match self { + Signed::Negative(val) => Some(val), + Signed::Positive(_) => None, + } + } + // rustdoc-stripper-ignore-next /// Transforms the `Signed` into a `Result`, /// mapping `Negative(v)` to `Ok(v)` and `Positive(_)` to `Err(err)`. @@ -1261,20 +1281,32 @@ mod tests { let signed = ct_1.into_positive(); assert_eq!(signed, Signed::Positive(ct_1)); assert!(signed.is_positive()); + assert_eq!(signed.positive(), Some(ct_1)); + assert!(!signed.is_negative()); + assert!(signed.negative().is_none()); let signed = ct_1.into_negative(); assert_eq!(signed, Signed::Negative(ct_1)); assert!(signed.is_negative()); + assert_eq!(signed.negative(), Some(ct_1)); + assert!(!signed.is_positive()); + assert!(signed.positive().is_none()); let und = Undefined(1); let signed = und.into_positive(); assert_eq!(signed, Signed::Positive(und)); assert!(signed.is_positive()); + assert_eq!(signed.positive(), Some(und)); + assert!(!signed.is_negative()); + assert!(signed.negative().is_none()); let signed = und.into_negative(); assert_eq!(signed, Signed::Negative(und)); assert!(signed.is_negative()); + assert_eq!(signed.negative(), Some(und)); + assert!(!signed.is_positive()); + assert!(signed.positive().is_none()); } #[test] @@ -1285,10 +1317,12 @@ mod tests { let signed = ct_1.into_positive(); assert_eq!(signed, Signed::Positive(ct_1)); assert!(signed.is_positive()); + assert_eq!(signed.positive(), Some(ct_1)); let signed = ct_1.into_negative(); assert_eq!(signed, Signed::Negative(ct_1)); assert!(signed.is_negative()); + assert_eq!(signed.negative(), Some(ct_1)); let ct_none = GenericFormattedValue::Time(ClockTime::NONE); assert!(ct_none.is_none());