SlotsMutTrait

Trait SlotsMutTrait 

Source
pub trait SlotsMutTrait<T>: SlotsTrait<T> + AsMut<Slots<T>> {
    // Provided methods
    fn front_value_mut(&mut self) -> Option<&mut T> { ... }
    fn front_entry_mut(&mut self) -> Option<(usize, &mut T)> { ... }
    fn back_value_mut(&mut self) -> Option<&mut T> { ... }
    fn back_entry_mut(&mut self) -> Option<(usize, &mut T)> { ... }
    fn get_value_mut(&mut self, direction: Direction) -> Option<&mut T> { ... }
    fn get_entry_mut(&mut self, direction: Direction) -> Option<(usize, &mut T)> { ... }
    fn collapse_front(&mut self) { ... }
    fn collapse_back(&mut self) { ... }
    fn collapse(&mut self, direction: Direction) { ... }
    fn replace(&mut self, mapping: impl IntoIterator<Item = (usize, T)>) { ... }
    fn try_replace<M: IntoIterator<Item = (usize, T)>>(
        &mut self,
        mapping: M,
    ) -> Result<(), ((usize, T), M::IntoIter)> { ... }
}
Expand description

Provides mutable access and manipulation methods for a collection of slots.

This trait extends the SlotsTrait<T> trait and adds additional methods for mutating the slots. It is implemented for types that can be converted to a mutable reference of Slots<T>.

Provided Methods§

Source

fn front_value_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the value of the first occupied slot.

§Examples
use slots_slice::SlotsMutTrait;

let mut slots = [Some('a'), None, Some('b')];
if let Some(value) = slots.front_value_mut() {
    *value = 'c';
}
assert_eq!(slots[0], Some('c'));
Source

fn front_entry_mut(&mut self) -> Option<(usize, &mut T)>

Returns the index and a mutable reference to the value of the first occupied slot.

§Examples
use slots_slice::SlotsMutTrait;

let mut slots = [Some('a'), None, Some('b')];
if let Some((index, value)) = slots.front_entry_mut() {
    *value = 'c';
}
assert_eq!(slots[0], Some('c'));
Source

fn back_value_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the value of the last occupied slot.

§Examples
use slots_slice::SlotsMutTrait;

let mut slots = [Some('a'), None, Some('b')];
if let Some(value) = slots.back_value_mut() {
    *value = 'c';
}
assert_eq!(slots[2], Some('c'));
Source

fn back_entry_mut(&mut self) -> Option<(usize, &mut T)>

Returns the index and a mutable reference to the value of the last occupied slot.

§Examples
use slots_slice::SlotsMutTrait;

let mut slots = [Some('a'), None, Some('b')];
if let Some((index, value)) = slots.back_entry_mut() {
    *value = 'c';
}
assert_eq!(slots[2], Some('c'));
Source

fn get_value_mut(&mut self, direction: Direction) -> Option<&mut T>

Returns a mutable reference to the value of the slot in the specified direction.

§Examples
use slots_slice::{SlotsMutTrait, Direction};

let mut slots = [Some('a'), None, Some('b')];
if let Some(value) = slots.get_value_mut(Direction::Front) {
    *value = 'c';
}
assert_eq!(slots[0], Some('c'));

if let Some(value) = slots.get_value_mut(Direction::Back) {
    *value = 'd';
}
assert_eq!(slots[2], Some('d'));
Source

fn get_entry_mut(&mut self, direction: Direction) -> Option<(usize, &mut T)>

Returns the index and a mutable reference to the value of the slot in the specified direction.

§Examples
use slots_slice::{SlotsMutTrait, Direction};

let mut slots = [Some('a'), None, Some('b')];
if let Some((index, value)) = slots.get_entry_mut(Direction::Front) {
    *value = 'c';
}
assert_eq!(slots[0], Some('c'));

if let Some((index, value)) = slots.get_entry_mut(Direction::Back) {
    *value = 'd';
}
assert_eq!(slots[2], Some('d'));
Source

fn collapse_front(&mut self)

Collapses the slots towards the front by removing unoccupied slots and shifting occupied slots to the left.

§Examples
use slots_slice::SlotsMutTrait;

let mut slots = [None, Some('a'), None, Some('b')];
slots.collapse_front();
assert_eq!(slots, [Some('a'), Some('b'), None, None]);
Source

fn collapse_back(&mut self)

Collapses the slots towards the back by removing unoccupied slots and shifting occupied slots to the right.

§Examples
use slots_slice::SlotsMutTrait;

let mut slots = [None, Some('a'), None, Some('b')];
slots.collapse_back();
assert_eq!(slots, [None, None, Some('a'), Some('b')]);
Source

fn collapse(&mut self, direction: Direction)

Collapses the slots in the specified direction by removing unoccupied slots and shifting occupied slots accordingly.

§Examples
use slots_slice::{SlotsMutTrait, Direction};

let mut slots = [None, Some('a'), None, Some('b')];
slots.collapse(Direction::Front);
assert_eq!(slots, [Some('a'), Some('b'), None, None]);

let mut slots = [None, Some('a'), None, Some('b')];
slots.collapse(Direction::Back);
assert_eq!(slots, [None, None, Some('a'), Some('b')]);
Source

fn replace(&mut self, mapping: impl IntoIterator<Item = (usize, T)>)

Replaces the values of the slots based on the provided index-value pairs.

The method takes an iterator of (index, value) pairs and replaces the values of the slots at the specified indices with the provided values.

§Panics

This method panics if an index is out of bounds.

§Examples
use slots_slice::SlotsMutTrait;

let mut slots = [None, None, None];
slots.replace([(0, 'a'), (2, 'b')]);
assert_eq!(slots, [Some('a'), None, Some('b')]);
Source

fn try_replace<M: IntoIterator<Item = (usize, T)>>( &mut self, mapping: M, ) -> Result<(), ((usize, T), M::IntoIter)>

Tries to replace the values of the slots based on the provided index-value pairs, returning an error if any index is out of bounds.

The method takes an iterator of (index, value) pairs and tries to replace the values of the slots at the specified indices with the provided values. If any index is out of bounds, it returns an Err variant with the (index, value) pair that caused the error and the remaining iterator. If all replacements are successful, it returns an Ok variant.

§Examples
use slots_slice::SlotsMutTrait;

let mut slots = [None, None, None];

let result = slots.try_replace([(0, 'a'), (2, 'b')]);
assert!(result.is_ok());
assert_eq!(slots, [Some('a'), None, Some('b')]);

let result = slots.try_replace([(1, 'c'), (3, 'd')]);
assert!(result.is_err());
let ((index, value), remaining) = result.unwrap_err();
assert_eq!(index, 3);
assert_eq!(value, 'd');
assert_eq!(remaining.collect::<Vec<_>>(), vec![]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, A> SlotsMutTrait<T> for A
where A: SlotsTrait<T> + AsMut<Slots<T>>,