Trait sp_std::ops::IndexMut

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

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

container[index] is actually syntactic sugar for *container.index_mut(index), but only when used as a mutable value. If an immutable value is requested, the Index trait is used instead. This allows nice things such as v[index] = value.

Examples

A very simple implementation of a Balance struct that has two sides, where each can be indexed mutably and immutably.

use std::ops::{Index, IndexMut};

#[derive(Debug)]
enum Side {
    Left,
    Right,
}

#[derive(Debug, PartialEq)]
enum Weight {
    Kilogram(f32),
    Pound(f32),
}

struct Balance {
    pub left: Weight,
    pub right: Weight,
}

impl Index<Side> for Balance {
    type Output = Weight;

    fn index(&self, index: Side) -> &Self::Output {
        println!("Accessing {index:?}-side of balance immutably");
        match index {
            Side::Left => &self.left,
            Side::Right => &self.right,
        }
    }
}

impl IndexMut<Side> for Balance {
    fn index_mut(&mut self, index: Side) -> &mut Self::Output {
        println!("Accessing {index:?}-side of balance mutably");
        match index {
            Side::Left => &mut self.left,
            Side::Right => &mut self.right,
        }
    }
}

let mut balance = Balance {
    right: Weight::Kilogram(2.5),
    left: Weight::Pound(1.5),
};

// In this case, `balance[Side::Right]` is sugar for
// `*balance.index(Side::Right)`, since we are only *reading*
// `balance[Side::Right]`, not writing it.
assert_eq!(balance[Side::Right], Weight::Kilogram(2.5));

// However, in this case `balance[Side::Left]` is sugar for
// `*balance.index_mut(Side::Left)`, since we are writing
// `balance[Side::Left]`.
balance[Side::Left] = Weight::Kilogram(3.0);

Required Methods§

Performs the mutable indexing (container[index]) operation.

Panics

May panic if the index is out of bounds.

Implementors§

impl<A, O, Idx> IndexMut<Idx> for BitArray<A, O>where
    A: BitViewSized,
    O: BitOrder,
    BitSlice<A::Store, O>: IndexMut<Idx>,

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

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

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

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

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

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

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

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

impl IndexMut<usize> for BStr

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

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

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

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

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

impl<'a, K, V, S, Q> IndexMut<&'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: StorageMut<T, R, C>> IndexMut<usize> for Matrix<T, R, C, S>

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

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

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

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

impl<N, E, Ty, Ix> IndexMut<Ix> for Csr<N, E, Ty, Ix>where
    Ty: EdgeType,
    Ix: IndexType,

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

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

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

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

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

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

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

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

impl<T> IndexMut<PeerSet> for PerPeerSet<T>

impl<I> IndexMut<I> for H128where
    I: SliceIndex<[u8], Output = [u8]>,

impl<I> IndexMut<I> for H160where
    I: SliceIndex<[u8], Output = [u8]>,

impl<I> IndexMut<I> for H256where
    I: SliceIndex<[u8], Output = [u8]>,

impl<I> IndexMut<I> for H384where
    I: SliceIndex<[u8], Output = [u8]>,

impl<I> IndexMut<I> for H512where
    I: SliceIndex<[u8], Output = [u8]>,

impl<I> IndexMut<I> for H768where
    I: SliceIndex<[u8], Output = [u8]>,

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

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

impl<T> IndexMut<usize> for Slab<T>

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

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

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

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

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

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

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

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

impl<D: AsMut<[f64]> + AsRef<[f64]>> IndexMut<usize> for Data<D>

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

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

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

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

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

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