Skip to main content

Valid

Trait Valid 

Source
pub trait Valid { }
Expand description

Marker trait for types that are valid by construction.

Every Valid type trivially validates as itself via Validate and ValidateFrom.

§Examples

use peer_pressure::{Valid, Validate};

#[derive(Clone, Debug, PartialEq)]
pub struct NonEmptyVec<T>(Vec<T>);

impl<T> NonEmptyVec<T> {
    pub fn new(inner: Vec<T>) -> Option<Self> {
        (inner.len() > 0).then_some(Self(inner))
    }
}

// `NonEmptyVec` can only be constructed via `NonEmptyVec::new` which ensures the non-emptiness
// invariant holds for all `NonEmptyVec` values.
impl<T> Valid for NonEmptyVec<T> {}

let p = NonEmptyVec::new(vec![1,2,3]).unwrap();
assert_eq!(p.clone().validate(), Ok(p));

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§