Skip to main content

Len

Struct Len 

Source
pub struct Len<const MIN: usize, const MAX: usize = { usize::MAX }>;
Expand description

Requires the length of a string or collection to be in a range.

Both ends are inclusive. Strings count characters, collections items (see Length). The code of the violation is the name of the validator: len, min_len (MinLen) or max_len (MaxLen). It has the parameters min and max (if they are set).

use deser_validate::{Len, MaxLen, Validator};

assert!(Len::<1, 3>::validate("abc").is_ok());
assert_eq!(
    Len::<1, 3>::validate("abcd").unwrap_err().message(),
    "length must be between 1 and 3"
);
assert!(MaxLen::<2>::validate(&vec![1, 2, 3]).is_err());

Trait Implementations§

Source§

impl<T: Length + ?Sized, const MIN: usize, const MAX: usize> Validator<T> for Len<MIN, MAX>

Source§

fn validate(value: &T) -> Result<(), Violation>

Validates the value.

Auto Trait Implementations§

§

impl<const MIN: usize, const MAX: usize> Freeze for Len<MIN, MAX>

§

impl<const MIN: usize, const MAX: usize> RefUnwindSafe for Len<MIN, MAX>

§

impl<const MIN: usize, const MAX: usize> Send for Len<MIN, MAX>

§

impl<const MIN: usize, const MAX: usize> Sync for Len<MIN, MAX>

§

impl<const MIN: usize, const MAX: usize> Unpin for Len<MIN, MAX>

§

impl<const MIN: usize, const MAX: usize> UnsafeUnpin for Len<MIN, MAX>

§

impl<const MIN: usize, const MAX: usize> UnwindSafe for Len<MIN, MAX>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.