Skip to main content

CollectionArgument

Trait CollectionArgument 

Source
pub trait CollectionArgument: Sealed + Sized {
    // Required methods
    fn require_non_empty(self, path: &str) -> ArgumentResult<Self>;
    fn require_len(self, path: &str, expected: usize) -> ArgumentResult<Self>;
    fn require_len_at_least(
        self,
        path: &str,
        min: usize,
    ) -> ArgumentResult<Self>;
    fn require_len_at_most(self, path: &str, max: usize) -> ArgumentResult<Self>;
    fn require_len_in(
        self,
        path: &str,
        min: usize,
        max: usize,
    ) -> ArgumentResult<Self>;
}
Expand description

Validates collection lengths while preserving the original collection.

Every successful method consumes and returns the same collection value without cloning its elements. Implementations are provided for owned vectors, borrowed slices, and arrays. Length failures carry LengthMetric::Elements.

The trait is sealed to those library-supported collection forms.

Required Methods§

Source

fn require_non_empty(self, path: &str) -> ArgumentResult<Self>

Requires this collection to contain at least one element.

Success returns the original collection without cloning its elements. An empty collection returns ArgumentErrorKind::Empty at path.

Source

fn require_len(self, path: &str, expected: usize) -> ArgumentResult<Self>

Requires this collection to contain exactly expected elements.

Success returns the original collection without cloning its elements. A different length returns ArgumentErrorKind::Length at path with an exact constraint.

Source

fn require_len_at_least(self, path: &str, min: usize) -> ArgumentResult<Self>

Requires this collection to contain at least min elements.

Success returns the original collection without cloning its elements. A shorter collection returns ArgumentErrorKind::Length at path with a minimum constraint.

Source

fn require_len_at_most(self, path: &str, max: usize) -> ArgumentResult<Self>

Requires this collection to contain at most max elements.

Success returns the original collection without cloning its elements. A longer collection returns ArgumentErrorKind::Length at path with a maximum constraint.

Source

fn require_len_in( self, path: &str, min: usize, max: usize, ) -> ArgumentResult<Self>

Requires this collection’s length to lie in min..=max.

The range is validated before the observed length. If min > max, this returns ArgumentErrorKind::InvalidLengthConstraint at path; otherwise, an out-of-range length returns ArgumentErrorKind::Length. Success returns the original collection without cloning its elements.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl<T, const N: usize> CollectionArgument for [T; N]

Source§

fn require_non_empty(self, path: &str) -> ArgumentResult<Self>

Validates non-emptiness and returns the original array.

A zero-length array returns ArgumentErrorKind::Empty at path.

Source§

fn require_len(self, path: &str, expected: usize) -> ArgumentResult<Self>

Validates the exact length and returns the original array.

A mismatch returns ArgumentErrorKind::Length at path.

Source§

fn require_len_at_least(self, path: &str, min: usize) -> ArgumentResult<Self>

Validates the minimum length and returns the original array.

A length below min returns ArgumentErrorKind::Length at path.

Source§

fn require_len_at_most(self, path: &str, max: usize) -> ArgumentResult<Self>

Validates the maximum length and returns the original array.

A length above max returns ArgumentErrorKind::Length at path.

Source§

fn require_len_in( self, path: &str, min: usize, max: usize, ) -> ArgumentResult<Self>

Validates an inclusive length range and returns the original array.

A reversed range returns ArgumentErrorKind::InvalidLengthConstraint at path; an out-of-range length returns ArgumentErrorKind::Length.

Source§

impl<T> CollectionArgument for &[T]

Source§

fn require_non_empty(self, path: &str) -> ArgumentResult<Self>

Validates non-emptiness and returns the original borrowed slice.

An empty slice returns ArgumentErrorKind::Empty at path.

Source§

fn require_len(self, path: &str, expected: usize) -> ArgumentResult<Self>

Validates the exact length and returns the original borrowed slice.

A mismatch returns ArgumentErrorKind::Length at path.

Source§

fn require_len_at_least(self, path: &str, min: usize) -> ArgumentResult<Self>

Validates the minimum length and returns the original borrowed slice.

A length below min returns ArgumentErrorKind::Length at path.

Source§

fn require_len_at_most(self, path: &str, max: usize) -> ArgumentResult<Self>

Validates the maximum length and returns the original borrowed slice.

A length above max returns ArgumentErrorKind::Length at path.

Source§

fn require_len_in( self, path: &str, min: usize, max: usize, ) -> ArgumentResult<Self>

Validates an inclusive length range and returns the borrowed slice.

A reversed range returns ArgumentErrorKind::InvalidLengthConstraint at path; an out-of-range length returns ArgumentErrorKind::Length.

Source§

impl<T> CollectionArgument for Vec<T>

Source§

fn require_non_empty(self, path: &str) -> ArgumentResult<Self>

Validates non-emptiness and returns the original vector.

An empty vector returns ArgumentErrorKind::Empty at path.

Source§

fn require_len(self, path: &str, expected: usize) -> ArgumentResult<Self>

Validates the exact length and returns the original vector.

A mismatch returns ArgumentErrorKind::Length at path.

Source§

fn require_len_at_least(self, path: &str, min: usize) -> ArgumentResult<Self>

Validates the minimum length and returns the original vector.

A length below min returns ArgumentErrorKind::Length at path.

Source§

fn require_len_at_most(self, path: &str, max: usize) -> ArgumentResult<Self>

Validates the maximum length and returns the original vector.

A length above max returns ArgumentErrorKind::Length at path.

Source§

fn require_len_in( self, path: &str, min: usize, max: usize, ) -> ArgumentResult<Self>

Validates an inclusive length range and returns the original vector.

A reversed range returns ArgumentErrorKind::InvalidLengthConstraint at path; an out-of-range length returns ArgumentErrorKind::Length.

Implementors§