Skip to main content

HasLength

Trait HasLength 

Source
pub trait HasLength {
    // Required method
    fn length(&self) -> usize;
}
Expand description

A value with a measurable length, used by the length rules.

Implement this for your own container types to make them eligible for NonEmpty, MinLen, MaxLen, and LenRange.

§Examples

use type_lib::rules::HasLength;

assert_eq!("héllo".length(), 5); // chars, not bytes
assert_eq!([1, 2, 3][..].length(), 3);

Required Methods§

Source

fn length(&self) -> usize

Returns the length of the value: chars for strings, elements otherwise.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl HasLength for str

Source§

fn length(&self) -> usize

Source§

impl HasLength for String

Available on crate feature alloc only.
Source§

fn length(&self) -> usize

Source§

impl<T> HasLength for [T]

Source§

fn length(&self) -> usize

Source§

impl<T> HasLength for Vec<T>

Available on crate feature alloc only.
Source§

fn length(&self) -> usize

Source§

impl<U: HasLength + ?Sized> HasLength for &U

Source§

fn length(&self) -> usize

Implementors§