Trait sp_std::ops::Index

1.0.0 · source · []
pub trait Index<Idx> where
    Idx: ?Sized
{ type Output: ?Sized; fn index(&self, index: Idx) -> &Self::Output; }
Expand description

Used for indexing operations (container[index]) in immutable contexts.

container[index] is actually syntactic sugar for *container.index(index), but only when used as an immutable value. If a mutable value is requested, IndexMut is used instead. This allows nice things such as let value = v[index] if the type of value implements Copy.

Examples

The following example implements Index on a read-only NucleotideCount container, enabling individual counts to be retrieved with index syntax.

use std::ops::Index;

enum Nucleotide {
    A,
    C,
    G,
    T,
}

struct NucleotideCount {
    a: usize,
    c: usize,
    g: usize,
    t: usize,
}

impl Index<Nucleotide> for NucleotideCount {
    type Output = usize;

    fn index(&self, nucleotide: Nucleotide) -> &Self::Output {
        match nucleotide {
            Nucleotide::A => &self.a,
            Nucleotide::C => &self.c,
            Nucleotide::G => &self.g,
            Nucleotide::T => &self.t,
        }
    }
}

let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
assert_eq!(nucleotide_count[Nucleotide::A], 14);
assert_eq!(nucleotide_count[Nucleotide::C], 9);
assert_eq!(nucleotide_count[Nucleotide::G], 10);
assert_eq!(nucleotide_count[Nucleotide::T], 12);

Associated Types

The returned type after indexing.

Required methods

Performs the indexing (container[index]) operation.

Panics

May panic if the index is out of bounds.

Implementations on Foreign Types

Returns a reference to the value corresponding to the supplied key.

Panics

Panics if the key is not present in the HashMap.

Implementors

impl<O, V, Idx> Index<Idx> for BitArray<O, V> where
    O: BitOrder,
    V: BitView,
    BitSlice<O, V::Store>: Index<Idx>, 

impl<O, T> Index<usize> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<Range<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeFrom<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeFull> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeInclusive<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeTo<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T> Index<RangeToInclusive<usize>> for BitSlice<O, T> where
    O: BitOrder,
    T: BitStore

impl<O, T, Idx> Index<Idx> for BitBox<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: Index<Idx>, 

impl<O, T, Idx> Index<Idx> for BitVec<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: Index<Idx>, 

impl Index<usize> for BStr

impl Index<RangeFull> for BStr

impl Index<Range<usize>> for BStr

impl Index<RangeTo<usize>> for BStr

impl<K, V> Index<K> for BoxedSlice<K, V> where
    K: EntityRef

impl<K, V> Index<K> for SecondaryMap<K, V> where
    K: EntityRef,
    V: Clone

impl<K, V> Index<K> for PrimaryMap<K, V> where
    K: EntityRef

impl Index<usize> for Scalar

impl<'input, Endian> Index<usize> for EndianSlice<'input, Endian> where
    Endian: Endianity

impl<'input, Endian> Index<RangeFrom<usize>> for EndianSlice<'input, Endian> where
    Endian: Endianity

impl<K, Q: ?Sized, V, S, A> Index<&'_ Q> for HashMap<K, V, S, A> where
    K: Eq + Hash + Borrow<Q>,
    Q: Eq + Hash,
    S: BuildHasher,
    A: Allocator + Clone

impl<'a, K, T> Index<K> for HeaderMap<T> where
    K: AsHeaderName

impl<K, V, Q: ?Sized, S> Index<&'_ Q> for IndexMap<K, V, S> where
    Q: Hash + Equivalent<K>,
    K: Hash + Eq,
    S: BuildHasher

impl<K, V, S> Index<usize> for IndexMap<K, V, S>

impl<T, S> Index<usize> for IndexSet<T, S>

impl<'a, K, V, S, Q: ?Sized> Index<&'a Q> for LinkedHashMap<K, V, S> where
    K: Hash + Eq + Borrow<Q>,
    S: BuildHasher,
    Q: Eq + Hash

impl<T: Scalar, R: Dim, C: Dim, S: Storage<T, R, C>> Index<usize> for Matrix<T, R, C, S>

impl<T, R: Dim, C: Dim, S> Index<(usize, usize)> for Matrix<T, R, C, S> where
    T: Scalar,
    S: Storage<T, R, C>, 

impl<T: Scalar, const D: usize> Index<usize> for Point<T, D>

impl<T: Scalar, const D: usize> Index<(usize, usize)> for Rotation<T, D>

impl<T: Scalar> Index<usize> for Quaternion<T>

impl<T: RealField, C: TCategory, const D: usize> Index<(usize, usize)> for Transform<T, C, D> where
    Const<D>: DimNameAdd<U1>,
    DefaultAllocator: Allocator<T, DimNameSum<Const<D>, U1>, DimNameSum<Const<D>, U1>>, 

impl<I> Index<I> for H128 where
    I: SliceIndex<[u8]>, 

impl<I> Index<I> for H160 where
    I: SliceIndex<[u8]>, 

impl<I> Index<I> for H256 where
    I: SliceIndex<[u8]>, 

impl<I> Index<I> for H512 where
    I: SliceIndex<[u8]>, 

impl<TyIx, Ty> Index<TyIx> for TypedIxVec<TyIx, Ty> where
    TyIx: Into<u32>, 

impl<'t> Index<usize> for Captures<'t>

impl<'t, 'i> Index<&'i str> for Captures<'t>

impl<'t> Index<usize> for Captures<'t>

impl<'t, 'i> Index<&'i str> for Captures<'t>

impl<'a, Q> Index<&'a Q> for Map<String, Value> where
    String: Borrow<Q>,
    Q: ?Sized + Ord + Eq + Hash

impl<I> Index<I> for Value where
    I: Index

impl<T> Index<usize> for Slab<T>

impl<A: Array, I: SliceIndex<[A::Item]>> Index<I> for SmallVec<A>

impl<D: AsRef<[f64]>> Index<usize> for Data<D>

impl<T, P> Index<usize> for Punctuated<T, P>

impl<A: Array, I: SliceIndex<[A::Item]>> Index<I> for ArrayVec<A>

impl<'s, T, I> Index<I> for SliceVec<'s, T> where
    I: SliceIndex<[T]>, 

impl<A: Array, I: SliceIndex<[A::Item]>> Index<I> for TinyVec<A>

impl<'a, Q: ?Sized> Index<&'a Q> for Map<String, Value> where
    String: Borrow<Q>,
    Q: Ord + Eq + Hash

impl<I> Index<I> for Value where
    I: Index

impl Index<RangeFull> for Url

impl Index<Range<Position>> for Url

impl<V> Index<usize> for VecMap<V>

impl<'a, V> Index<&'a usize> for VecMap<V>