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);

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

Looks up a single bit by semantic index.

Examples
use bitvec::prelude::*;

let bits = bits![Msb0, u8; 0, 1, 0];
assert!(!bits[0]); // -----^  |  |
assert!( bits[1]); // --------^  |
assert!(!bits[2]); // -----------^

If the index is greater than or equal to the length, indexing will panic.

The below test will panic when accessing index 1, as only index 0 is valid.

use bitvec::prelude::*;

let bits = bits![0,  ];
bits[1]; // --------^

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 Scalar

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<'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<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<'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