Add method to transpose valid options

This commit is contained in:
Nettika 2022-07-09 15:13:26 -07:00
parent 59964356cb
commit a0b77c19c0
2 changed files with 19 additions and 1 deletions

View file

@ -115,12 +115,21 @@ impl<T: Vet> Vet for Option<T> {
fn is_valid(&self) -> Result<(), Self::VetError> {
match self {
Some(t) => t.is_valid(),
Some(o) => o.is_valid(),
None => Ok(()),
}
}
}
impl<T: Vet> Valid<Option<T>> {
pub fn transpose(self) -> Option<Valid<T>> {
match self {
Valid(Some(o)) => Some(Valid(o)),
Valid(None) => None,
}
}
}
#[cfg(feature = "alloc")]
impl<T: Vet> Vet for alloc::vec::Vec<T> {
type VetError = T::VetError;

View file

@ -50,6 +50,15 @@ fn vet_option() {
assert!(foo.is_valid().is_err());
}
#[test]
fn transpose_valid_option() {
let foo = None::<EvenUsize>.vet().unwrap();
assert_eq!(foo.transpose(), None);
let foo = Some(EvenUsize(84)).vet().unwrap();
assert_eq!(foo.transpose(), Some(Valid(EvenUsize(84))));
}
#[test]
#[cfg(feature = "alloc")]
fn vet_vec() {