Skip to main content

TrySparseIndex

Trait TrySparseIndex 

Source
pub trait TrySparseIndex: Sized {
    type Error: Error;

    // Required methods
    fn index(&self) -> usize;
    fn try_from_index(index: usize) -> Result<Self, Self::Error>;

    // Provided methods
    fn from_index(index: usize) -> Self { ... }
    fn validate_sorted(
        indices: impl DoubleEndedIterator<Item = usize>,
    ) -> Result<(), Self::Error> { ... }
}
Expand description

A trait for types that can be safely converted to and from indices.

This trait extends SparseIndex with fallible conversion methods, allowing for validation of index values during conversion. It’s particularly useful for bounded types like small integers or enums with gaps in their value range.

§Examples

use omp_core::sparse_index::TrySparseIndex;

// u8 implements TrySparseIndex with bounds checking
assert!(u8::try_from_index(255).is_ok());
assert!(u8::try_from_index(256).is_err());

Required Associated Types§

Source

type Error: Error

The error type returned when index conversion fails.

Required Methods§

Source

fn index(&self) -> usize

Returns the index for this value.

Source

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Attempts to convert an index to this type.

§Errors

Returns an error if the index is not valid for this type.

Provided Methods§

Source

fn from_index(index: usize) -> Self

Converts an index to this type, assuming the index is valid.

§Safety

This method should only be called with indices that are known to be valid for the type. For fallible conversion, use Self::try_from_index.

Source

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Validates that every index in a sorted iterator is valid for this type.

The default checks each index individually: validity for an arbitrary type (e.g. an enum with gaps) is not an interval, so probing only the extremes is insufficient. Types whose valid indices form a contiguous range (like the integer implementations) override this with an O(1) min/max check.

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 TrySparseIndex for NonZero<i8>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for NonZero<i16>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for NonZero<i32>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for NonZero<i64>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for NonZero<isize>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for NonZero<u8>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for NonZero<u16>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for NonZero<u32>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for NonZero<u64>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for NonZero<usize>

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for i8

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for i16

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for i32

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for i64

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for isize

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for u8

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for u16

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for u32

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for u64

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Source§

impl TrySparseIndex for usize

Source§

type Error = NumericIndexError

Source§

fn index(&self) -> usize

Source§

fn from_index(index: usize) -> Self

Source§

fn try_from_index(index: usize) -> Result<Self, Self::Error>

Source§

fn validate_sorted( indices: impl DoubleEndedIterator<Item = usize>, ) -> Result<(), Self::Error>

Implementors§

Source§

impl<T: SparseIndex> TrySparseIndex for T

Blanket implementation of TrySparseIndex for all SparseIndex types.

This allows any infallible sparse index type to be used in contexts requiring TrySparseIndex without additional boilerplate.