pub struct ConcurrentAppendHive<T, const PAGE: usize = DEFAULT_PAGE> { /* private fields */ }concurrent only.Expand description
A paged container with stable addresses and shared insertion.
get returns an ordinary &T after one Acquire slot-state
load, with no guard or reference count. Published values can be changed,
removed or moved only under &mut self, which excludes every reference
borrowed from shared access. Prefer ConcurrentHive
when removal must also be shared.
Pages stay allocated until shrink_to_fit frees
the empty ones; shared inserts reuse freed pages later. Shared
iteration is weakly consistent and may miss insertions in flight. len
is approximate during insertion and exact when insertions have finished.
Exclusive scans and conversions take O(index_bound)
time. Conversions to and from Hive preserve live ids and stale-id
history, but may change allocation and slot reuse order.
As with Hive, ids carry no container identity, generations never wrap,
and stale ids stay dead within one allocation history. A panicking
insert_with spends the id shown to its factory.
PAGE must be non-zero, fit in a u32, and fit a page in isize::MAX
bytes. Uses only core and alloc.
use paged_collections::concurrent::ConcurrentAppendHive;
let mut hive = ConcurrentAppendHive::<&str, 4>::new();
let first = hive.insert("first");
let value = hive.get(first).unwrap();
let second = hive.insert("second");
assert_eq!(*value, "first");
assert_eq!(hive[second], "second");
assert_eq!(hive.remove(first), Some("first"));
assert!(hive.get(first).is_none());Sharing requires both Send and Sync elements:
use paged_collections::concurrent::ConcurrentAppendHive;
use std::rc::Rc;
fn assert_sync<T: Sync>() {}
assert_sync::<ConcurrentAppendHive<Rc<()>>>();Implementations§
Source§impl<T, const PAGE: usize> ConcurrentAppendHive<T, PAGE>
impl<T, const PAGE: usize> ConcurrentAppendHive<T, PAGE>
Sourcepub fn new() -> Self
pub fn new() -> Self
An empty hive, without allocating.
§Panics
Panics if the page capacity is invalid; see the type’s documentation.
Sourcepub fn insert(&self, value: T) -> Id
pub fn insert(&self, value: T) -> Id
Inserts value and returns its id.
§Panics
Panics if all MAX_INDICES indices are occupied or retired, or a
page cannot be allocated. A panicking concurrent factory may make room.
Sourcepub fn try_insert(&self, value: T) -> Result<Id, T>
pub fn try_insert(&self, value: T) -> Result<Id, T>
Like insert, returning the value on index exhaustion.
Allocation failures may still panic or abort.
Sourcepub fn insert_with(&self, f: impl FnOnce(Id) -> T) -> Id
pub fn insert_with(&self, f: impl FnOnce(Id) -> T) -> Id
Inserts the value f makes from its id.
If f panics, its id’s generation is spent and that id stays dead.
The slot becomes reusable unless its last generation was spent.
§Panics
Panics on index exhaustion or if f panics; allocation may also fail.
Sourcepub fn try_insert_with(&self, f: impl FnOnce(Id) -> T) -> Option<Id>
pub fn try_insert_with(&self, f: impl FnOnce(Id) -> T) -> Option<Id>
Like insert_with, returning None without
calling f when no index is available.
Sourcepub fn get(&self, id: Id) -> Option<&T>
pub fn get(&self, id: Id) -> Option<&T>
The value of a matching live id, without a guard or reference count.
Returns None for stale, invalid or unpublished ids.
Sourcepub fn id_of(&self, value: &T) -> Option<Id>
pub fn id_of(&self, value: &T) -> Option<Id>
The id of the element at value’s address, if value is a live
element of this hive: the inverse of get.
Takes time linear in the directory’s page cells up to the highest
installed page (with Flat, O(highest page number)): it compares the
address with each installed page’s slot range, then checks that the slot found is published live
and holds exactly that element. Anything else gives None. Uses
address comparison only; no pointer is dereferenced.
use paged_collections::concurrent::ConcurrentAppendHive;
let hive = ConcurrentAppendHive::<u32, 4>::new();
let id = hive.insert(7);
let element = hive.get(id).unwrap();
assert_eq!(hive.id_of(element), Some(id));
assert_eq!(hive.id_of(&7), None);Sourcepub fn contains(&self, id: Id) -> bool
pub fn contains(&self, id: Id) -> bool
Whether this id is live at the instant of one state load.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of live values; approximate during shared insertion. Exact when no insertion is in flight; this is no publication barrier.
It sums per-thread counters (per page group without std), which only
grow while shared, so it lies between the counts at the start and the
end of the call.
Sourcepub fn index_bound(&self) -> u32
pub fn index_bound(&self) -> u32
One past the highest reserved index, including insertions in flight. A reserved index’s page may not have been installed yet.
Threads that contend for fresh indices reserve them in blocks of about one page, so the bound may exceed the indices handed out by up to one block per thread.
Sourcepub fn iter(&self) -> AppendIter<'_, T, PAGE> ⓘ
pub fn iter(&self) -> AppendIter<'_, T, PAGE> ⓘ
Iterates over (id, &value) in slot order, weakly consistently.
Scans only indices below the reservation bound at iterator creation;
may miss insertions in flight, but never yields an unpublished value.
Sourcepub fn get_mut(&mut self, id: Id) -> Option<&mut T>
pub fn get_mut(&mut self, id: Id) -> Option<&mut T>
The matching live value, mutably, under exclusive access.
Sourcepub fn remove(&mut self, id: Id) -> Option<T>
pub fn remove(&mut self, id: Id) -> Option<T>
Removes and returns a matching live value under exclusive access. The id stays dead; its slot is reused with a new generation or retired.
Sourcepub fn iter_mut(&mut self) -> AppendIterMut<'_, T, PAGE> ⓘ
pub fn iter_mut(&mut self) -> AppendIterMut<'_, T, PAGE> ⓘ
Iterates over live (id, &mut value) pairs in slot order.
Sourcepub fn drain(&mut self) -> AppendDrain<'_, T, PAGE> ⓘ
pub fn drain(&mut self) -> AppendDrain<'_, T, PAGE> ⓘ
Eagerly removes every live value, yielding owned (id, value) pairs
in slot order. Dropping or forgetting the iterator leaves the hive
empty. Pages and generation history stay allocated.
Sourcepub fn retain(&mut self, keep: impl FnMut(Id, &mut T) -> bool)
pub fn retain(&mut self, keep: impl FnMut(Id, &mut T) -> bool)
Keeps values for which keep(id, value) returns true, in slot order.
If the predicate or a destructor panics, prior changes remain and
len stays exact.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Frees every page that holds no value, under exclusive access. Takes
O(index_bound) time.
A page is freed if it lies wholly below the reservation bound and all its slots are vacant and reusable (a page with a retired slot is kept). It remembers its highest version, so ids it issued stay dead: shared inserts reuse freed pages, lowest first, after the free slots of the remaining pages, with generations above every earlier one. That order is exact only for one inserter that has not contended with others: a thread first uses up a block of fresh indices it reserved under contention, and while one insert is relinking a reused page, a concurrent insert can take a fresh index instead (and fail at the index limit even though slots are about to be freed). The free slots are relinked in index order. The page directory’s own small arrays stay allocated.
use paged_collections::concurrent::ConcurrentAppendHive;
let mut hive = ConcurrentAppendHive::<u32, 4>::new();
let ids: Vec<_> = (0..12).map(|v| hive.insert(v)).collect();
for &id in &ids[..8] {
hive.remove(id);
}
hive.shrink_to_fit(); // frees the first two pages
assert_eq!(hive[ids[8]], 8);
let new = hive.insert(99); // reuses a freed page
assert!(new.index() < 8 && !ids.contains(&new));
assert!(hive.get(ids[0]).is_none());Sourcepub fn into_hive(self) -> Hive<T, PAGE>
pub fn into_hive(self) -> Hive<T, PAGE>
Moves into a plain Hive, preserving live ids and stale-id history.
Takes O(index_bound) time.
Every slot below the bound is represented explicitly: zero states
become retired vacancies, even versions become reusable vacancies,
and odd versions keep their values. Indices reserved for a thread’s
block but never handed out become reusable vacancies at version 2. A page freed by
shrink_to_fit becomes an empty raw page with
its floor; other floors are zero. Offsets beyond the bound have never
issued ids and need no history.
Raw-page validation cannot fail by construction: all versions have
the required parity, all pages fit PAGE, and indices are below
MAX_INDICES. Allocation may still panic or abort. Values already
moved are owned by the raw pages; this hive’s empty cells cannot drop
them again, including if allocation unwinds.
Sourcepub fn snapshot(&self) -> Hive<T, PAGE>where
T: Clone,
pub fn snapshot(&self) -> Hive<T, PAGE>where
T: Clone,
Clones a weakly consistent snapshot into a plain Hive.
Insertions that happen-before the call are included. Ids whose removal
happens-before the call stay dead. The raw pages always validate. Scans
up to index_bound with a floor lookup per page.
Trait Implementations§
Source§impl<T: Clone, const PAGE: usize> Clone for ConcurrentAppendHive<T, PAGE>
Clones via ConcurrentAppendHive::snapshot, preserving generations.
impl<T: Clone, const PAGE: usize> Clone for ConcurrentAppendHive<T, PAGE>
Clones via ConcurrentAppendHive::snapshot, preserving generations.
Source§impl<T, const PAGE: usize> Debug for ConcurrentAppendHive<T, PAGE>
impl<T, const PAGE: usize> Debug for ConcurrentAppendHive<T, PAGE>
Source§impl<T, const PAGE: usize> Default for ConcurrentAppendHive<T, PAGE>
impl<T, const PAGE: usize> Default for ConcurrentAppendHive<T, PAGE>
Source§impl<'de, T: Deserialize<'de>, const PAGE: usize> Deserialize<'de> for ConcurrentAppendHive<T, PAGE>
Validates the plain Hive format and converts in O(index bound).
impl<'de, T: Deserialize<'de>, const PAGE: usize> Deserialize<'de> for ConcurrentAppendHive<T, PAGE>
Validates the plain Hive format and converts in O(index bound).
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> Drop for ConcurrentAppendHive<T, PAGE>
impl<T, const PAGE: usize> Drop for ConcurrentAppendHive<T, PAGE>
Source§impl<T, const PAGE: usize> From<Hive<T, PAGE>> for ConcurrentAppendHive<T, PAGE>
Moves a plain hive into shared append storage without changing live ids.
Takes O(the source hive’s index bound) time; pages and reuse order may change.
impl<T, const PAGE: usize> From<Hive<T, PAGE>> for ConcurrentAppendHive<T, PAGE>
Moves a plain hive into shared append storage without changing live ids. Takes O(the source hive’s index bound) time; pages and reuse order may change.
The new reservation bound ends after the last explicit raw slot or the
last page with a nonzero floor, whichever is later. The untouched suffix
of floor-zero pages is left to fresh reservation. Omitted offsets below
this bound become reusable vacancies at their page floor. For floor-zero
holes only, generation 1 is spent and the vacancy starts at version 2:
a popped zero state must always mean retired. Nonzero floors retain their
exact next generation, floor / 2 + 1. Explicit retired zeros are never
pushed. Thus no previously issued dead id can revive.
Versions are copied before draining values, since draining advances the source versions. Occupied states are published only after their values have moved; unwinding during allocation cannot expose uninitialized cells to this hive’s destructor.
Source§impl<'a, T, const PAGE: usize> IntoIterator for &'a ConcurrentAppendHive<T, PAGE>
impl<'a, T, const PAGE: usize> IntoIterator for &'a ConcurrentAppendHive<T, PAGE>
Source§impl<'a, T, const PAGE: usize> IntoIterator for &'a mut ConcurrentAppendHive<T, PAGE>
impl<'a, T, const PAGE: usize> IntoIterator for &'a mut ConcurrentAppendHive<T, PAGE>
impl<T: Send, const PAGE: usize> Send for ConcurrentAppendHive<T, PAGE>
Source§impl<T: Serialize, const PAGE: usize> Serialize for ConcurrentAppendHive<T, PAGE>
Serializes a weakly consistent snapshot in the plain Hive format.
Uses the same consistency as ConcurrentAppendHive::snapshot and
scans to the initial index bound with one floor lookup per page.
The resulting pages always pass Hive’s validation.
impl<T: Serialize, const PAGE: usize> Serialize for ConcurrentAppendHive<T, PAGE>
Serializes a weakly consistent snapshot in the plain Hive format.
Uses the same consistency as ConcurrentAppendHive::snapshot and
scans to the initial index bound with one floor lookup per page.
The resulting pages always pass Hive’s validation.
impl<T: Send + Sync, const PAGE: usize> Sync for ConcurrentAppendHive<T, PAGE>
Auto Trait Implementations§
impl<T, const PAGE: usize = DEFAULT_PAGE> !Freeze for ConcurrentAppendHive<T, PAGE>
impl<T, const PAGE: usize = DEFAULT_PAGE> !RefUnwindSafe for ConcurrentAppendHive<T, PAGE>
impl<T, const PAGE: usize> Forget for ConcurrentAppendHive<T, PAGE>
impl<T, const PAGE: usize> Move for ConcurrentAppendHive<T, PAGE>
impl<T, const PAGE: usize> Unpin for ConcurrentAppendHive<T, PAGE>where
Spine<Page<T>, CHUNKS>: Unpin,
impl<T, const PAGE: usize> UnsafeUnpin for ConcurrentAppendHive<T, PAGE>where
Spine<Page<T>, CHUNKS>: UnsafeUnpin,
impl<T, const PAGE: usize> UnwindSafe for ConcurrentAppendHive<T, PAGE>where
Spine<Page<T>, CHUNKS>: 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