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§
Required Methods§
Provided Methods§
Sourcefn from_index(index: usize) -> Self
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.
Sourcefn validate_sorted(
indices: impl DoubleEndedIterator<Item = usize>,
) -> Result<(), Self::Error>
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>
impl TrySparseIndex for NonZero<i8>
Source§impl TrySparseIndex for NonZero<i16>
impl TrySparseIndex for NonZero<i16>
Source§impl TrySparseIndex for NonZero<i32>
impl TrySparseIndex for NonZero<i32>
Source§impl TrySparseIndex for NonZero<i64>
impl TrySparseIndex for NonZero<i64>
Source§impl TrySparseIndex for NonZero<isize>
impl TrySparseIndex for NonZero<isize>
Source§impl TrySparseIndex for NonZero<u8>
impl TrySparseIndex for NonZero<u8>
Source§impl TrySparseIndex for NonZero<u16>
impl TrySparseIndex for NonZero<u16>
Source§impl TrySparseIndex for NonZero<u32>
impl TrySparseIndex for NonZero<u32>
Source§impl TrySparseIndex for NonZero<u64>
impl TrySparseIndex for NonZero<u64>
Source§impl TrySparseIndex for NonZero<usize>
impl TrySparseIndex for NonZero<usize>
Source§impl TrySparseIndex for i8
impl TrySparseIndex for i8
Source§impl TrySparseIndex for i16
impl TrySparseIndex for i16
Source§impl TrySparseIndex for i32
impl TrySparseIndex for i32
Source§impl TrySparseIndex for i64
impl TrySparseIndex for i64
Source§impl TrySparseIndex for isize
impl TrySparseIndex for isize
Source§impl TrySparseIndex for u8
impl TrySparseIndex for u8
Source§impl TrySparseIndex for u16
impl TrySparseIndex for u16
Source§impl TrySparseIndex for u32
impl TrySparseIndex for u32
Source§impl TrySparseIndex for u64
impl TrySparseIndex for u64
Source§impl TrySparseIndex for usize
impl TrySparseIndex for usize
Implementors§
Source§impl<T: SparseIndex> TrySparseIndex for T
Blanket implementation of TrySparseIndex for all SparseIndex types.
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.