pub struct PieView<'a, T> { /* private fields */ }Expand description
A lightweight, temporary view into a PieList that borrows the backing ElemPool.
PieView bundles the list handle and the data pool together into a single, lightweight
struct. This enables standard Rust traits like IntoIterator, Debug, and PartialEq,
which require access to the data without passing a secondary pool argument.
Since PieView only holds shared references, it implements Copy and Clone
regardless of whether the underlying type T implements them.
§Examples
§Printing (Debug)
use pie_core::{ElemPool, PieList};
let mut pool = ElemPool::new();
let mut list = PieList::new(&mut pool);
list.push_back(10, &mut pool).unwrap();
list.push_back(20, &mut pool).unwrap();
let view = list.view(&pool);
assert_eq!(format!("{:?}", view), "[10, 20]");
list.clear(&mut pool);§Iterating
let view = list.view(&pool);
let mut sum = 0;
for &item in view {
sum += item;
}
assert_eq!(sum, 3);§Equality
You can compare two lists for equality, even if they live in different pools.
let mut pool1 = ElemPool::new();
let mut list1 = PieList::new(&mut pool1);
list1.push_back("apple", &mut pool1).unwrap();
let mut pool2 = ElemPool::new();
let mut list2 = PieList::new(&mut pool2);
list2.push_back("apple", &mut pool2).unwrap();
// Compare views
assert_eq!(list1.view(&pool1), list2.view(&pool2));Implementations§
Source§impl<'a, T> PieView<'a, T>
impl<'a, T> PieView<'a, T>
Sourcepub fn new(list: &'a PieList<T>, pool: &'a ElemPool<T>) -> Self
pub fn new(list: &'a PieList<T>, pool: &'a ElemPool<T>) -> Self
Creates a new view for the given list using the data in pool.
It is often more ergonomic to use PieList::view instead.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the list.
§Example
let view = list.view(&pool);
assert_eq!(view.len(), 0);Sourcepub fn front(&self) -> Option<&T>
pub fn front(&self) -> Option<&T>
Returns a reference to the front element.
§Example
let view = list.view(&pool);
assert_eq!(view.front(), Some(&10));Trait Implementations§
Source§impl<'a, T> IntoIterator for PieView<'a, T>
impl<'a, T> IntoIterator for PieView<'a, T>
Source§impl<'a, T: Ord> Ord for PieView<'a, T>
impl<'a, T: Ord> Ord for PieView<'a, T>
Source§impl<'a, T: PartialOrd> PartialOrd for PieView<'a, T>
impl<'a, T: PartialOrd> PartialOrd for PieView<'a, T>
impl<'a, T> Copy for PieView<'a, T>
impl<'a, T: Eq> Eq for PieView<'a, T>
Auto Trait Implementations§
impl<'a, T> Freeze for PieView<'a, T>
impl<'a, T> RefUnwindSafe for PieView<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for PieView<'a, T>where
T: Sync,
impl<'a, T> Sync for PieView<'a, T>where
T: Sync,
impl<'a, T> Unpin for PieView<'a, T>
impl<'a, T> UnsafeUnpin for PieView<'a, T>
impl<'a, T> UnwindSafe for PieView<'a, T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more