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 {:?}-side of balance immutably", index);
        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 {:?}-side of balance mutably", index);
        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.

Implementations on Foreign Types

Implementors

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

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

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

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

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

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

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

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

impl<O, T, Idx> IndexMut<Idx> for BitVec<O, T> where
    O: BitOrder,
    T: BitStore,
    BitSlice<O, T>: 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: ?Sized, S> IndexMut<&'_ Q> for IndexMap<K, V, S> where
    Q: Hash + Equivalent<K>,
    K: Hash + Eq,
    S: BuildHasher

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

impl<'a, K, V, S, Q: ?Sized> IndexMut<&'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: 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<I> IndexMut<I> for H128 where
    I: SliceIndex<[u8], Output = [u8]>, 

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

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

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

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

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

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

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

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

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

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

impl<V> IndexMut<usize> for VecMap<V>

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