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.

§Examples

use qubit_argument::CollectionArgument;

let values = vec![1, 2, 3].require_len("items", 3).unwrap();
assert_eq!(values, [1, 2, 3]);

Required Methods§

Source

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

Requires this collection to contain at least one element.

§Parameters
  • path: identifies the collection in a validation error.
§Returns

The original collection on success, without cloning its elements.

§Errors

Returns ArgumentErrorKind::Empty at path when the collection is empty.

Source

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

Requires this collection to contain exactly expected elements.

§Parameters
  • path: identifies the collection in a validation error.
  • expected: the required number of elements.
§Returns

The original collection on success, without cloning its elements.

§Errors

Returns ArgumentErrorKind::Length at path when the length differs from expected.

Source

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

Requires this collection to contain at least min elements.

§Parameters
  • path: identifies the collection in a validation error.
  • min: the minimum number of elements.
§Returns

The original collection on success, without cloning its elements.

§Errors

Returns ArgumentErrorKind::Length at path when the length is below min.

Source

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

Requires this collection to contain at most max elements.

§Parameters
  • path: identifies the collection in a validation error.
  • max: the maximum number of elements.
§Returns

The original collection on success, without cloning its elements.

§Errors

Returns ArgumentErrorKind::Length at path when the length is above max.

Source

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

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

§Parameters
  • path: identifies the collection in a validation error.
  • min: the inclusive lower bound.
  • max: the inclusive upper bound.
§Returns

The original collection on success, without cloning its elements.

§Errors

The range is validated before the observed length. If min > max, returns ArgumentErrorKind::InvalidLengthConstraint at path; otherwise, an out-of-range length returns ArgumentErrorKind::Length.

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§