pub trait Invariant: Sized {
type Error: Display;
// Required method
fn check(slice: &str) -> Result<(), Self::Error>;
}
Expand description
The Ck
and Check
types are checked strings types that make guarantees
about the contents of the string. These guarantees are determined by this
trait, Invariant
which distinguishes whether or not a string upholds some
arbitrary invariants via the Invariant::check
function. If the Err
is
returned, then the invariant is broken, and the Ck
or Check
generic over
the invariant cannot be constructed.
§Examples
Declaring an invariant that the string contains no whitespace:
struct NoWhitespace;
impl Invariant for NoWhitespace {
type Error = char;
fn check(slice: &str) -> Result<(), Self::Error> {
match slice.chars().find(|ch| ch.is_whitespace()) {
Some(ch) => Err(ch),
None => Ok(()),
}
}
}
Required Associated Types§
Required Methods§
sourcefn check(slice: &str) -> Result<(), Self::Error>
fn check(slice: &str) -> Result<(), Self::Error>
Returns Ok
if the string upholds the invariant, otherwise Err
.
This function is used internally in Check::from_buf
and Ck::from_slice
.
Object Safety§
This trait is not object safe.