Skip to main content

ConcurrentPagedVec

Struct ConcurrentPagedVec 

Source
pub struct ConcurrentPagedVec<T, const PAGE: usize = DEFAULT_PAGE> { /* private fields */ }
Available on crate feature 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>

Source

pub const PAGE: usize = PAGE

Number of elements per page.

Source

pub fn new() -> Self

Creates an empty vector without allocating.

§Panics

Panics if PAGE is zero or a page allocation would exceed isize::MAX.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn is_empty(&self) -> bool

Whether the hole-free prefix is empty; subject to the lag of len.

Source

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.

Source

pub fn iter(&self) -> DenseIter<'_, T, PAGE> ⓘ

Iterates over a snapshot of the committed prefix in index order.

Source

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.

Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Mutable access to an element under exclusive access.

Source

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

Iterates mutably over all elements, in index order.

Source

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

pub fn pop(&mut self) -> Option<T>

Removes and returns the last element. Keeps allocated pages.

Source

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.

Source

pub fn clear(&mut self)

Drops every element and clears readiness for subsequent reuse. Keeps allocated pages; destructor panics behave as in truncate.

Source

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.

Source

pub fn snapshot(&self) -> PagedVec<T, PAGE>
where T: Clone,

Clones the committed prefix into a plain PagedVec in O(prefix length). Like len and iter, this can omit completed pushes beyond an earlier unpublished reservation. The prefix forms a valid plain vector.

Source

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.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, const PAGE: usize> Debug for ConcurrentPagedVec<T, PAGE>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, const PAGE: usize> Default for ConcurrentPagedVec<T, PAGE>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
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).

Source§

fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T, const PAGE: usize> Extend<T> for ConcurrentPagedVec<T, PAGE>

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: T)

🔬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<T, const PAGE: usize> From<PagedVec<T, PAGE>> for ConcurrentPagedVec<T, PAGE>

Source§

fn from(inner: PagedVec<T, PAGE>) -> Self

Converts to this type from the input type.
Source§

impl<T, const PAGE: usize> FromIterator<T> for ConcurrentPagedVec<T, PAGE>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T, const PAGE: usize> Index<usize> for ConcurrentPagedVec<T, PAGE>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, const PAGE: usize> IndexMut<usize> for ConcurrentPagedVec<T, PAGE>

Source§

fn index_mut(&mut self, index: usize) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T, const PAGE: usize> IntoIterator for &'a ConcurrentPagedVec<T, PAGE>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = DenseIter<'a, T, PAGE>

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, const PAGE: usize> IntoIterator for &'a mut ConcurrentPagedVec<T, PAGE>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = VecIterMut<'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
Source§

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.

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.