pub struct OnceList<T: ?Sized, A: Allocator = Global> { /* private fields */ }Expand description
A single linked list which behaves like std::cell::OnceCell, but for multiple values.
§Usage
A simple example:
use once_list2::OnceList;
// Create a new empty list. Note that the variable is immutable.
let list = OnceList::<i32>::new();
// You can push values to the list without the need for mutability.
list.push(1);
list.push(2);
// Or you can push multiple values at once.
list.extend([3, 4, 5]);
// You can iterate over the list.
assert_eq!(list.iter().copied().collect::<Vec<_>>(), vec![1, 2, 3, 4, 5]);
// Some methods are mutable only.
let mut list_mut = list;
// You can remove (take) a value from the list.
let removed = list_mut.remove(|&x| x % 2 == 0);
assert_eq!(removed, Some(2));
assert_eq!(list_mut.iter().copied().collect::<Vec<_>>(), vec![1, 3, 4, 5]);§(Almost nightly rustc only) Unsized types support
You can use the unsized types like str, [u8] or dyn Display as the value type of the OnceList.
Though you can use this feature without the nightly rustc compiler, you can not push or extend the values to the list without the nightly compiler and the nightly feature enabled. i.e. You can only create an empty list, that’s all.
In the nightly compiler and with the nightly feature enabled, the additional methods like push_unsized, remove_unsized_as become available:
// This code only works with the nightly compiler and the `nightly` feature enabled.
use once_list2::OnceList;
// Creating a `OnceList` for `[i32]`, the unsized type.
let list = OnceList::<[i32]>::new();
list.push_unsized([1] /* A sized array type, `[i32; 1]`, can be coerced into [i32].*/);
list.push_unsized([2, 3] /* Same for `[i32; 2] type. */);
// The normal methods like `iter` are available because it returns a reference to the value.
assert_eq!(list.iter().nth(0).unwrap(), &[1]);
assert_eq!(list.iter().nth(1).unwrap(), &[2, 3]);
let mut list_mut = list;
// `remove_unsized_as` method allows you to check the unsized value type and remove it.
let removed: Option<[i32; 2]> = unsafe {
list_mut.remove_unsized_as(|x| if x.len() == 2 {
Some(x.try_into().unwrap())
} else {
None
})
};
// The removed value is an array, not a slice!
assert_eq!(removed, Some([2, 3]));Implementations§
Source§impl<T: ?Sized, A: Allocator> OnceList<T, A>
impl<T: ?Sized, A: Allocator> OnceList<T, A>
Sourcepub fn new_in(alloc: A) -> Self
pub fn new_in(alloc: A) -> Self
Creates a new empty OnceList with the given allocator. This method does not allocate.
Sourcepub fn contains(&self, val: &T) -> boolwhere
T: PartialEq,
pub fn contains(&self, val: &T) -> boolwhere
T: PartialEq,
Returns true if the list contains the value.
Sourcepub fn first_mut(&mut self) -> Option<&mut T>
pub fn first_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the first value, if it exists.
Sourcepub fn last_mut(&mut self) -> Option<&mut T>
pub fn last_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the last value, if it exists. This method is O(n).
Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Returns an iterator over the &T references in the list.
Source§impl<T, A: Allocator + Clone> OnceList<T, A>
impl<T, A: Allocator + Clone> OnceList<T, A>
Sourcepub fn push(&self, val: T) -> &T
pub fn push(&self, val: T) -> &T
Appends a value to the list, and returns the reference to that value.
Note that this method takes &self, not &mut self.
Sourcepub fn extend<U: IntoIterator<Item = T>>(&self, iter: U)
pub fn extend<U: IntoIterator<Item = T>>(&self, iter: U)
An almost same method with the std::iter::Extend::extend,
though this method takes &self instead of &mut self.
Trait Implementations§
Source§impl<T, A: Allocator + Clone> Extend<T> for OnceList<T, A>
impl<T, A: Allocator + Clone> Extend<T> for OnceList<T, A>
Source§fn extend<U: IntoIterator<Item = T>>(&mut self, iter: U)
fn extend<U: IntoIterator<Item = T>>(&mut self, iter: U)
Due to the definition of the Extend trait, this method takes &mut self.
Use the OnceList::extend method instead if you want to use &self.
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)