Skip to main content

PieViewMut

Struct PieViewMut 

Source
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>

Source

pub fn new(list: &'a mut PieList<T>, pool: &'a mut ElemPool<T>) -> Self

Creates a new mutable view.

Source

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);
Source

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());
Source

pub fn push_back(&mut self, elt: T)

Appends an element to the back of the list.

§Panics

Panics if the pool is out of memory (capacity overflow).

§Example
let mut view = list.view_mut(&mut pool);
view.push_back(10);
assert_eq!(view.back(), Some(&10));
Source

pub fn push_front(&mut self, elt: T)

Appends an element to the front of the list.

§Panics

Panics if the pool is out of memory (capacity overflow).

§Example
let mut view = list.view_mut(&mut pool);
view.push_front(10);
assert_eq!(view.front(), Some(&10));
Source

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);
Source

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);
Source

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));
Source

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));
Source

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));
Source

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));
Source

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());
Source

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]);
Source

pub fn remove(&mut self, index: usize) -> Result<T, IndexError>

Removes the element at the given logical index and returns it.

§Errors

Returns IndexError if index >= len.

§Example
let mut view = list.view_mut(&mut pool);
view.push_back(10);
view.push_back(20);
view.push_back(30);

assert_eq!(view.remove(1).unwrap(), 20);
assert_eq!(view.len(), 2);
Source

pub fn iter(&self) -> Iter<'_, T>

Creates an immutable iterator over the list’s elements.

§Example
let mut view = list.view_mut(&mut pool);
view.extend([1, 2, 3]);

let vec: Vec<_> = view.iter().copied().collect();
assert_eq!(vec, vec![1, 2, 3]);
Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Creates a mutable iterator over the list’s elements.

§Example
let mut view = list.view_mut(&mut pool);
view.push_back(1);

for item in view.iter_mut() {
    *item += 10;
}
assert_eq!(view.front(), Some(&11));

Trait Implementations§

Source§

impl<'a, T> Extend<T> for PieViewMut<'a, T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, 'b, T> IntoIterator for &'b mut PieViewMut<'a, T>

Source§

type Item = &'b mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'b, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for PieViewMut<'a, T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for PieViewMut<'a, T>

§

impl<'a, T> RefUnwindSafe for PieViewMut<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for PieViewMut<'a, T>
where T: Send,

§

impl<'a, T> Sync for PieViewMut<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for PieViewMut<'a, T>

§

impl<'a, T> UnsafeUnpin for PieViewMut<'a, T>

§

impl<'a, T> !UnwindSafe for PieViewMut<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.