pub struct PieViewMut<'a, T> { /* private fields */ }Expand description
A mutable view that bundles a PieList and its ElemPool.
This struct allows you to perform mutable operations (push, pop, clear, modify elements) without repeatedly passing the pool as an argument. It holds exclusive mutable access to both the list and the pool for its lifetime.
§Example
use pie_core::{ElemPool, PieList};
let mut pool = ElemPool::new();
let mut list = PieList::new(&mut pool);
{
let mut view = list.view_mut(&mut pool);
view.push_back(10);
view.push_back(20);
// Standard mutable iteration
for item in &mut view {
*item *= 2;
}
view.clear();
}
assert!(list.is_empty());Implementations§
Source§impl<'a, T> PieViewMut<'a, T>
impl<'a, T> PieViewMut<'a, T>
Sourcepub fn new(list: &'a mut PieList<T>, pool: &'a mut ElemPool<T>) -> Self
pub fn new(list: &'a mut PieList<T>, pool: &'a mut ElemPool<T>) -> Self
Creates a new mutable view.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the list.
§Example
let mut view = list.view_mut(&mut pool);
view.push_back(1);
assert_eq!(view.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the list is empty.
§Example
let mut view = list.view_mut(&mut pool);
assert!(view.is_empty());
view.push_back(1);
assert!(!view.is_empty());Sourcepub fn push_front(&mut self, elt: T)
pub fn push_front(&mut self, elt: T)
Sourcepub fn pop_back(&mut self) -> Option<T>
pub fn pop_back(&mut self) -> Option<T>
Removes the last element from the list and returns it, or None if empty.
§Example
let mut view = list.view_mut(&mut pool);
view.push_back(10);
assert_eq!(view.pop_back(), Some(10));
assert_eq!(view.pop_back(), None);Sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Removes the first element from the list and returns it, or None if empty.
§Example
let mut view = list.view_mut(&mut pool);
view.push_back(10);
assert_eq!(view.pop_front(), Some(10));
assert_eq!(view.pop_front(), None);Sourcepub fn front(&self) -> Option<&T>
pub fn front(&self) -> Option<&T>
Returns a reference to the front element.
§Example
let mut view = list.view_mut(&mut pool);
view.push_back(42);
assert_eq!(view.front(), Some(&42));Sourcepub fn front_mut(&mut self) -> Option<&mut T>
pub fn front_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the front element.
§Example
let mut view = list.view_mut(&mut pool);
view.push_back(42);
if let Some(x) = view.front_mut() {
*x = 100;
}
assert_eq!(view.front(), Some(&100));Sourcepub fn back(&self) -> Option<&T>
pub fn back(&self) -> Option<&T>
Returns a reference to the back element.
§Example
let mut view = list.view_mut(&mut pool);
view.push_back(99);
assert_eq!(view.back(), Some(&99));Sourcepub fn back_mut(&mut self) -> Option<&mut T>
pub fn back_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the back element.
§Example
let mut view = list.view_mut(&mut pool);
view.push_back(99);
*view.back_mut().unwrap() = 0;
assert_eq!(view.back(), Some(&0));Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the list, returning all elements to the pool’s free list.
§Example
let mut view = list.view_mut(&mut pool);
view.push_back(1);
view.clear();
assert!(view.is_empty());Sourcepub fn insert(&mut self, index: usize, element: T) -> Result<(), IndexError>
pub fn insert(&mut self, index: usize, element: T) -> Result<(), IndexError>
Inserts a new element at the given logical index.
§Errors
Returns IndexError if index > len.
§Example
let mut view = list.view_mut(&mut pool);
view.push_back(10);
view.push_back(30);
view.insert(1, 20).unwrap();
let items: Vec<_> = view.iter().copied().collect();
assert_eq!(items, vec![10, 20, 30]);Sourcepub fn remove(&mut self, index: usize) -> Result<T, IndexError>
pub fn remove(&mut self, index: usize) -> Result<T, IndexError>
Trait Implementations§
Source§impl<'a, T> Extend<T> for PieViewMut<'a, T>
impl<'a, T> Extend<T> for PieViewMut<'a, T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)