Trait scale_info::prelude::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);

Required 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.

Implementors§

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

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, V, S, A> Index<&Q> for HashMap<K, V, S, A>where
    K: Eq + Hash + Borrow<Q>,
    Q: Eq + Hash + ?Sized,
    S: BuildHasher,
    A: Allocator + Clone,

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

impl<K, V, Q, S> Index<&Q> for IndexMap<K, V, S>where
    Q: Hash + Equivalent<K> + ?Sized,
    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> Index<&'a Q> for LinkedHashMap<K, V, S>where
    K: Hash + Eq + Borrow<Q>,
    S: BuildHasher,
    Q: Eq + Hash + ?Sized,

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<N, E, Ty, Ix> Index<Ix> for Csr<N, E, Ty, Ix>where
    Ty: EdgeType,
    Ix: IndexType,

impl<N, E, Ty, Ix> Index<NodeIndex<Ix>> for Graph<N, E, Ty, Ix>where
    Ty: EdgeType,
    Ix: IndexType,

impl<N, E, Ty, Ix> Index<EdgeIndex<Ix>> for Graph<N, E, Ty, Ix>where
    Ty: EdgeType,
    Ix: IndexType,

impl<'a, G, I> Index<I> for Frozen<'a, G>where
    G: Index<I>,

impl<N, E, Ty, Ix> Index<NodeIndex<Ix>> for StableGraph<N, E, Ty, Ix>where
    Ty: EdgeType,
    Ix: IndexType,

impl<N, E, Ty, Ix> Index<EdgeIndex<Ix>> for StableGraph<N, E, Ty, Ix>where
    Ty: EdgeType,
    Ix: IndexType,

impl<N, E, Ty> Index<(N, N)> for GraphMap<N, E, Ty>where
    N: NodeTrait,
    Ty: EdgeType,

impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> Index<NodeIndex<Ix>> for MatrixGraph<N, E, Ty, Null, Ix>

impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> Index<(NodeIndex<Ix>, NodeIndex<Ix>)> for MatrixGraph<N, E, Ty, Null, Ix>

impl<T> Index<PeerSet> for PerPeerSet<T>

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

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

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

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

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

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

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 Index<usize> for Message

impl Index<usize> for KeyPair

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

impl<I> Index<I> for Valuewhere
    I: Index,

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

impl<K: Key, V> Index<K> for SlotMap<K, V>

impl<K: Key, V> Index<K> for DenseSlotMap<K, V>

impl<K: Key, V> Index<K> for HopSlotMap<K, V>

impl<K: Key, V> Index<K> for SecondaryMap<K, V>

impl<K, V, S> Index<K> for SparseSecondaryMap<K, V, S>where
    K: Key,
    S: BuildHasher,

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

impl<T, S, I> Index<I> for BoundedVec<T, S>where
    I: SliceIndex<[T]>,

impl<T, S, I> Index<I> for WeakBoundedVec<T, S>where
    I: SliceIndex<[T]>,

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> Index<&'a Q> for Map<String, Value>where
    String: Borrow<Q>,
    Q: Ord + Eq + Hash + ?Sized,

impl<I> Index<I> for Valuewhere
    I: Index,

impl Index<RangeFull> for Url

impl Index<Range<Position>> for Url

impl<I, T> Index<I> for StoreContext<'_, T>where
    StoreData: Index<I>,

impl<I, T> Index<I> for StoreContextMut<'_, T>where
    StoreData: Index<I>,

impl<T> Index<T> for ModuleTypesBuilderwhere
    ModuleTypes: Index<T>,