alloc_no_stdlib::allocated_memory

Trait IndexMut

1.0.0 · Source
pub trait IndexMut<Idx>: Index<Idx>
where Idx: ?Sized,
{ // Required method 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§

1.0.0 · Source

fn index_mut(&mut self, index: Idx) -> &mut Self::Output

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

§Panics

May panic if the index is out of bounds.

Implementors§

Source§

impl<'a, T> IndexMut<usize> for AllocatedStackMemory<'a, T>

Source§

impl<'a, T> IndexMut<Range<usize>> for AllocatedStackMemory<'a, T>

1.0.0 · Source§

impl<I> IndexMut<I> for str
where I: SliceIndex<str>,

Source§

impl<I, T, const N: usize> IndexMut<I> for Simd<T, N>

1.0.0 · Source§

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

1.50.0 · Source§

impl<T, I, const N: usize> IndexMut<I> for [T; N]
where [T]: IndexMut<I>,

impl IndexMut<Span> for [u8]

impl<T> IndexMut<StateID> for [T]

impl<T> IndexMut<StateID> for Vec<T>

impl<T> IndexMut<PatternID> for [T]

impl<T> IndexMut<PatternID> for Vec<T>

impl<T> IndexMut<SmallIndex> for [T]

impl<T> IndexMut<SmallIndex> for Vec<T>

impl<'a, T: 'a> IndexMut<usize> for HeapPrealloc<'a, T>

impl<T> IndexMut<usize> for WrapBox<T>

impl<T> IndexMut<Range<usize>> for WrapBox<T>

impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A>

impl<I> IndexMut<I> for Compat16x16
where I: SliceIndex<[i16]>,

impl<I> IndexMut<I> for Compat32x8
where I: SliceIndex<[i32]>,

impl<I> IndexMut<I> for CompatF8
where I: SliceIndex<[f32]>,

impl<'a, T: 'a> IndexMut<usize> for MemPool<'a, T>

impl<T> IndexMut<usize> for WrapBox<T>

impl IndexMut<usize> for Rgba

impl IndexMut<usize> for Pos2

impl IndexMut<usize> for Vec2

impl IndexMut<usize> for Vec2b

impl<T> IndexMut<(usize, usize)> for Grid<T>

impl IndexMut<Key> for Store

impl<T> IndexMut<usize> for RawLinks<T>

impl<Buffer> IndexMut<(u8, u32, u32)> for FlatSamples<Buffer>
where Buffer: IndexMut<usize>,

impl<P, Container> IndexMut<(u32, u32)> for ImageBuffer<P, Container>
where P: Pixel, Container: Deref<Target = [P::Subpixel]> + DerefMut,

impl<T> IndexMut<usize> for Luma<T>

impl<T> IndexMut<usize> for LumaA<T>

impl<T> IndexMut<usize> for Rgb<T>

impl<T> IndexMut<usize> for Rgba<T>

impl<K, V> IndexMut<(Bound<usize>, Bound<usize>)> for Slice<K, V>

impl<K, V> IndexMut<usize> for Slice<K, V>

impl<K, V> IndexMut<Range<usize>> for Slice<K, V>

impl<K, V> IndexMut<RangeFrom<usize>> for Slice<K, V>

impl<K, V> IndexMut<RangeFull> for Slice<K, V>

impl<K, V> IndexMut<RangeInclusive<usize>> for Slice<K, V>

impl<K, V> IndexMut<RangeTo<usize>> for Slice<K, V>

impl<K, V> IndexMut<RangeToInclusive<usize>> for Slice<K, V>

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

impl<K, V, S> IndexMut<(Bound<usize>, Bound<usize>)> for IndexMap<K, V, S>

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

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

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

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

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

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

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

impl<K, V, S> IndexMut<&K> for LiteMap<K, V, S>
where K: Ord, S: StoreMut<K, V>,

impl<T> IndexMut<Handle<T>> for Arena<T>

impl<T, U> IndexMut<Handle<T>> for HandleVec<T, U>

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

impl IndexMut<usize> for F32x2

impl IndexMut<usize> for F32x4

impl IndexMut<usize> for I32x2

impl IndexMut<usize> for I32x4

impl IndexMut<usize> for F32x2

impl IndexMut<usize> for F32x4

impl IndexMut<usize> for I32x2

impl IndexMut<usize> for I32x4

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

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

impl<N, E, Ty, Ix> IndexMut<EdgeIndex<Ix>> for Graph<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, Ix> IndexMut<NodeIndex<Ix>> for Graph<N, E, Ty, Ix>
where Ty: EdgeType, Ix: IndexType,

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

impl IndexMut<Span> for [u8]

impl<T> IndexMut<PatternID> for [T]

impl<T> IndexMut<PatternID> for Vec<T>

impl<T> IndexMut<SmallIndex> for [T]

impl<T> IndexMut<SmallIndex> for Vec<T>

impl<T> IndexMut<StateID> for [T]

impl<T> IndexMut<StateID> for Vec<T>

impl IndexMut<&Value> for Map

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

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

impl<T: PortKind> IndexMut<PortIndex<T>> for Chunk

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

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

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

impl<'s> IndexMut<&'s str> for DocumentMut

impl<'s> IndexMut<&'s str> for InlineTable

impl<'s> IndexMut<&'s str> for Table

impl<I> IndexMut<I> for Item
where I: Index,

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

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

impl<I: Internable> IndexMut<Handle<I>> for DataStore<I>

impl<T> IndexMut<usize> for Store<T>

impl<T> IndexMut<Index<T>> for Storage<T>

impl<T> IndexMut<Range<T>> for Storage<T>