Add method to transpose valid options
This commit is contained in:
parent
713e8f4d96
commit
ff56bbf7be
2 changed files with 19 additions and 1 deletions
11
src/lib.rs
11
src/lib.rs
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue