pub struct ConcurrentPagedVec<T, const PAGE: usize = DEFAULT_PAGE> { /* private fields */ }concurrent only.Expand description
A paged vector that supports appending through shared references.
Each push claims a distinct index and publishes its value without waiting
for earlier writers. Existing elements keep their addresses while shared.
get can see every completed push, including values beyond
the hole-free prefix reported by len. Iteration and page
slices snapshot that prefix when called. When no push is in flight,
len() == reserved_len().
Removing, moving or mutating elements requires exclusive access. Pages
stay allocated for reuse until shrink_to_fit or
destruction. Uses core and alloc only.
Send requires T: Send; Sync requires T: Send + Sync.
Implementations§
Source§impl<T, const PAGE: usize> ConcurrentPagedVec<T, PAGE>
impl<T, const PAGE: usize> ConcurrentPagedVec<T, PAGE>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty vector without allocating.
§Panics
Panics if PAGE is zero or a page allocation would exceed isize::MAX.
Sourcepub fn push(&self, value: T) -> usize
pub fn push(&self, value: T) -> usize
Appends value and returns its distinct index.
Does not wait for an earlier writer. No user code runs between claim and publication, so a panic cannot leave a claimed, unwritten slot.
§Panics
Panics on length or allocation capacity overflow; allocation can fail.
Sourcepub fn push_all(&self, iter: impl IntoIterator<Item = T>)
pub fn push_all(&self, iter: impl IntoIterator<Item = T>)
Appends every item with an individual push.
Other writers may interleave their values. If iteration panics, values
already pushed remain visible and no reservation is left unwritten.
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
The element at index, once its push has published it.
This can return a value beyond len.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Length of the hole-free, written prefix.
Can briefly lag behind completed pushes while an earlier push is in
flight. Exact, and equal to reserved_len,
when no push is in flight.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the hole-free prefix is empty; subject to the lag of len.
Sourcepub fn reserved_len(&self) -> usize
pub fn reserved_len(&self) -> usize
Number of claimed positions, including writes still in flight. This count does not prove that any particular element is readable.
Sourcepub fn iter(&self) -> DenseIter<'_, T, PAGE> ⓘ
pub fn iter(&self) -> DenseIter<'_, T, PAGE> ⓘ
Iterates over a snapshot of the committed prefix in index order.
Sourcepub fn pages(
&self,
) -> impl DoubleEndedIterator<Item = &[T]> + ExactSizeIterator + FusedIterator
pub fn pages( &self, ) -> impl DoubleEndedIterator<Item = &[T]> + ExactSizeIterator + FusedIterator
Nonempty slices of a snapshot of the committed prefix, in page order.
Every slice except the last contains PAGE elements.
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Mutable access to an element under exclusive access.
Sourcepub fn iter_mut(&mut self) -> VecIterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> VecIterMut<'_, T> ⓘ
Iterates mutably over all elements, in index order.
Sourcepub fn pages_mut(&mut self) -> impl Iterator<Item = &mut [T]>
pub fn pages_mut(&mut self) -> impl Iterator<Item = &mut [T]>
Nonempty mutable page slices, in index order.
The iterator is Send only if T is:
use paged_collections::concurrent::ConcurrentPagedVec;
let mut vector = ConcurrentPagedVec::<u32, 2>::new();
vector.push(0);
std::thread::scope(|scope| {
let mut pages = vector.pages_mut();
scope.spawn(move || pages.next().unwrap()[0] = 1);
});
assert_eq!(vector[0], 1);use paged_collections::concurrent::ConcurrentPagedVec;
use std::{cell::Cell, rc::Rc};
let mut vector = ConcurrentPagedVec::<Rc<Cell<u32>>, 2>::new();
vector.push(Rc::new(Cell::new(0)));
std::thread::scope(|scope| {
let mut pages = vector.pages_mut();
scope.spawn(move || pages.next().unwrap()[0].set(1));
});Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes and returns the last element. Keeps allocated pages.
Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Drops elements from the back until at most len remain. Keeps pages.
If a destructor panics, that element is gone and earlier ones remain.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Drops every element and clears readiness for subsequent reuse.
Keeps allocated pages; destructor panics behave as in truncate.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Frees pages wholly beyond the end of the vector, keeping directory chunks. Existing elements keep their addresses. Shared appends can allocate the released pages again.
Sourcepub fn into_inner(self) -> PagedVec<T, PAGE>
pub fn into_inner(self) -> PagedVec<T, PAGE>
Moves the elements into a plain vector in index order, in O(len). Element addresses can change; no cloning is required.
Trait Implementations§
Source§impl<T: Clone, const PAGE: usize> Clone for ConcurrentPagedVec<T, PAGE>
Clones the committed prefix via ConcurrentPagedVec::snapshot,
which can omit completed pushes beyond an unpublished reservation.
impl<T: Clone, const PAGE: usize> Clone for ConcurrentPagedVec<T, PAGE>
Clones the committed prefix via ConcurrentPagedVec::snapshot,
which can omit completed pushes beyond an unpublished reservation.
Source§impl<T, const PAGE: usize> Default for ConcurrentPagedVec<T, PAGE>
impl<T, const PAGE: usize> Default for ConcurrentPagedVec<T, PAGE>
Source§impl<'de, T: Deserialize<'de>, const PAGE: usize> Deserialize<'de> for ConcurrentPagedVec<T, PAGE>
Reads the plain PagedVec format and converts in O(len).
impl<'de, T: Deserialize<'de>, const PAGE: usize> Deserialize<'de> for ConcurrentPagedVec<T, PAGE>
Reads the plain PagedVec format and converts in O(len).
Source§fn deserialize<De: Deserializer<'de>>(
deserializer: De,
) -> Result<Self, De::Error>
fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>
Source§impl<T, const PAGE: usize> Extend<T> for ConcurrentPagedVec<T, PAGE>
impl<T, const PAGE: usize> Extend<T> for ConcurrentPagedVec<T, PAGE>
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: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T, const PAGE: usize> FromIterator<T> for ConcurrentPagedVec<T, PAGE>
impl<T, const PAGE: usize> FromIterator<T> for ConcurrentPagedVec<T, PAGE>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<'a, T, const PAGE: usize> IntoIterator for &'a ConcurrentPagedVec<T, PAGE>
impl<'a, T, const PAGE: usize> IntoIterator for &'a ConcurrentPagedVec<T, PAGE>
Source§impl<'a, T, const PAGE: usize> IntoIterator for &'a mut ConcurrentPagedVec<T, PAGE>
impl<'a, T, const PAGE: usize> IntoIterator for &'a mut ConcurrentPagedVec<T, PAGE>
impl<T: Send, const PAGE: usize> Send for ConcurrentPagedVec<T, PAGE>
Source§impl<T: Serialize, const PAGE: usize> Serialize for ConcurrentPagedVec<T, PAGE>
Serializes the committed prefix as a valid plain vector sequence
in O(prefix length). The prefix can omit completed pushes beyond an
earlier unpublished reservation, as in ConcurrentPagedVec::iter.
impl<T: Serialize, const PAGE: usize> Serialize for ConcurrentPagedVec<T, PAGE>
Serializes the committed prefix as a valid plain vector sequence
in O(prefix length). The prefix can omit completed pushes beyond an
earlier unpublished reservation, as in ConcurrentPagedVec::iter.
impl<T: Send + Sync, const PAGE: usize> Sync for ConcurrentPagedVec<T, PAGE>
Auto Trait Implementations§
impl<T, const PAGE: usize = DEFAULT_PAGE> !Freeze for ConcurrentPagedVec<T, PAGE>
impl<T, const PAGE: usize = DEFAULT_PAGE> !RefUnwindSafe for ConcurrentPagedVec<T, PAGE>
impl<T, const PAGE: usize> Forget for ConcurrentPagedVec<T, PAGE>
impl<T, const PAGE: usize> Move for ConcurrentPagedVec<T, PAGE>
impl<T, const PAGE: usize> Unpin for ConcurrentPagedVec<T, PAGE>where
AppendLog<T, PAGE>: Unpin,
impl<T, const PAGE: usize> UnsafeUnpin for ConcurrentPagedVec<T, PAGE>where
AppendLog<T, PAGE>: UnsafeUnpin,
impl<T, const PAGE: usize> UnwindSafe for ConcurrentPagedVec<T, PAGE>where
AppendLog<T, PAGE>: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more