forked from mirrors/gstreamer-rs
gst/format: add Signed::{positive, negative}
It's more convenient to use than: ```rust signed_val.positive_or(()).ok() ```
This commit is contained in:
parent
55ef309b16
commit
e0d9f886e3
1 changed files with 34 additions and 0 deletions
|
@ -28,6 +28,16 @@ impl<T> Signed<T> {
|
||||||
matches!(self, Signed::Positive(_))
|
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<T> {
|
||||||
|
match self {
|
||||||
|
Signed::Positive(val) => Some(val),
|
||||||
|
Signed::Negative(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// rustdoc-stripper-ignore-next
|
// rustdoc-stripper-ignore-next
|
||||||
/// Transforms the `Signed<T>` into a `Result<T, E>`,
|
/// Transforms the `Signed<T>` into a `Result<T, E>`,
|
||||||
/// mapping `Positive(v)` to `Ok(v)` and `Negative(_)` to `Err(err)`.
|
/// mapping `Positive(v)` to `Ok(v)` and `Negative(_)` to `Err(err)`.
|
||||||
|
@ -52,6 +62,16 @@ impl<T> Signed<T> {
|
||||||
matches!(self, Signed::Negative(_))
|
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<T> {
|
||||||
|
match self {
|
||||||
|
Signed::Negative(val) => Some(val),
|
||||||
|
Signed::Positive(_) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// rustdoc-stripper-ignore-next
|
// rustdoc-stripper-ignore-next
|
||||||
/// Transforms the `Signed<T>` into a `Result<T, E>`,
|
/// Transforms the `Signed<T>` into a `Result<T, E>`,
|
||||||
/// mapping `Negative(v)` to `Ok(v)` and `Positive(_)` to `Err(err)`.
|
/// mapping `Negative(v)` to `Ok(v)` and `Positive(_)` to `Err(err)`.
|
||||||
|
@ -1261,20 +1281,32 @@ mod tests {
|
||||||
let signed = ct_1.into_positive();
|
let signed = ct_1.into_positive();
|
||||||
assert_eq!(signed, Signed::Positive(ct_1));
|
assert_eq!(signed, Signed::Positive(ct_1));
|
||||||
assert!(signed.is_positive());
|
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();
|
let signed = ct_1.into_negative();
|
||||||
assert_eq!(signed, Signed::Negative(ct_1));
|
assert_eq!(signed, Signed::Negative(ct_1));
|
||||||
assert!(signed.is_negative());
|
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 und = Undefined(1);
|
||||||
|
|
||||||
let signed = und.into_positive();
|
let signed = und.into_positive();
|
||||||
assert_eq!(signed, Signed::Positive(und));
|
assert_eq!(signed, Signed::Positive(und));
|
||||||
assert!(signed.is_positive());
|
assert!(signed.is_positive());
|
||||||
|
assert_eq!(signed.positive(), Some(und));
|
||||||
|
assert!(!signed.is_negative());
|
||||||
|
assert!(signed.negative().is_none());
|
||||||
|
|
||||||
let signed = und.into_negative();
|
let signed = und.into_negative();
|
||||||
assert_eq!(signed, Signed::Negative(und));
|
assert_eq!(signed, Signed::Negative(und));
|
||||||
assert!(signed.is_negative());
|
assert!(signed.is_negative());
|
||||||
|
assert_eq!(signed.negative(), Some(und));
|
||||||
|
assert!(!signed.is_positive());
|
||||||
|
assert!(signed.positive().is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1285,10 +1317,12 @@ mod tests {
|
||||||
let signed = ct_1.into_positive();
|
let signed = ct_1.into_positive();
|
||||||
assert_eq!(signed, Signed::Positive(ct_1));
|
assert_eq!(signed, Signed::Positive(ct_1));
|
||||||
assert!(signed.is_positive());
|
assert!(signed.is_positive());
|
||||||
|
assert_eq!(signed.positive(), Some(ct_1));
|
||||||
|
|
||||||
let signed = ct_1.into_negative();
|
let signed = ct_1.into_negative();
|
||||||
assert_eq!(signed, Signed::Negative(ct_1));
|
assert_eq!(signed, Signed::Negative(ct_1));
|
||||||
assert!(signed.is_negative());
|
assert!(signed.is_negative());
|
||||||
|
assert_eq!(signed.negative(), Some(ct_1));
|
||||||
|
|
||||||
let ct_none = GenericFormattedValue::Time(ClockTime::NONE);
|
let ct_none = GenericFormattedValue::Time(ClockTime::NONE);
|
||||||
assert!(ct_none.is_none());
|
assert!(ct_none.is_none());
|
||||||
|
|
Loading…
Reference in a new issue