Skip to main content

Registry

Struct Registry 

Source
pub struct Registry<Id: Identifier, T> { /* private fields */ }
Expand description

A strongly typed vector

§Example

use jstd::Identifier;
use jstd::registry::Registry;

#[derive(Identifier)]
struct ItemId(usize);

let mut reg = Registry::<ItemId, i32>::default();
let id = reg.push(10);
reg[id] += 5;

assert_eq!(reg[id], 15);
assert_eq!(reg.len(), 1);
assert!(!reg.is_empty());

Segmented, append-only-stable backing store: element n lives in chunk k = floor(log2(n + 1)) at offset n + 1 - 2^k, so chunk k holds exactly 2^k elements. Each chunk is allocated once at its full capacity and never reallocated, so an element’s address is stable for the life of the registry even as later pushes grow the store (growing appends new chunks; it never moves existing elements). Growing the outer Vec<Vec<T>> moves the chunk headers, not their heap buffers. This stability is what lets the literal/type interners hand out &T references that outlive a mint (see qcode’s RwLock-wrapped interners). Chunk sizes double, so a small registry stays cheap (chunks 1, 2, 4, …) and a large one needs few chunks.

The public API is identical to a flat Vec-backed registry: ids are dense 0..len and index in insertion order.

Implementations§

Source§

impl<Id: Identifier, T> Registry<Id, T>

Source

pub fn push(&mut self, e: T) -> Id

Pushes a value and returns its typed identifier.

Source

pub fn len(&self) -> usize

Returns the number of elements.

Source

pub fn truncate(&mut self, len: usize)

Drops every element with an id of len or above, keeping the chunks already allocated so the next pushes reuse them.

The ids below len and their elements’ addresses are unchanged. This is the one way a registry shrinks: for a construction that appended elements it then has to take back, and for a scratch store that starts its ids over.

Source

pub fn is_empty(&self) -> bool

Returns true if the registry contains no elements.

Source

pub fn replace(&mut self, id: Id, value: T) -> T

Replaces the element at id, returning the previous value. The id (and every element’s stable address) is unchanged. Used to check out an element — swap in a sentinel, own the original, swap it back later — without disturbing any other id.

§Panics

Panics if id is out of bounds.

Source

pub fn get(&self, id: Id) -> Identified<Id, &T>

Returns an immutable identified view of an element.

§Panics

Panics if id is out of bounds.

Source

pub fn get_mut(&mut self, id: Id) -> Identified<Id, &mut T>

Returns a mutable identified view of an element.

§Panics

Panics if id is out of bounds.

Source

pub fn iter(&self) -> Iter<'_, Id, T>

Iterates immutably over (id, value) as Identified items.

§Example
use jstd::Identifier;
use jstd::registry::Registry;

#[derive(Identifier)]
struct Id(usize);

let mut reg = Registry::<Id, &str>::default();
reg.push("x");
reg.push("y");

let ids: Vec<usize> = reg.iter().map(|item| usize::from(item.id)).collect();
let vals: Vec<&str> = reg.iter().map(|item| **item).collect();

assert_eq!(ids, vec![0, 1]);
assert_eq!(vals, vec!["x", "y"]);
Source

pub fn iter_mut(&mut self) -> IterMut<'_, Id, T>

Iterates mutably over (id, value) as Identified items.

§Example
use jstd::Identifier;
use jstd::registry::Registry;

#[derive(Identifier)]
struct Id(usize);

let mut reg = Registry::<Id, i32>::default();
reg.push(1);
reg.push(2);

for mut item in reg.iter_mut() {
    **item += 10;
}

assert_eq!(reg[Id::from(0)], 11);
assert_eq!(reg[Id::from(1)], 12);
Source

pub fn select_mut(&mut self, ids: &[Id]) -> Vec<&mut T>

Borrows the elements at ids mutably and disjointly, returned in the same order as ids.

This is the disjoint-&mut-slice primitive the parallel function-pass driver uses to hand each worker its own body straight out of the registry, without the checkout/checkin swap (context-split stage 5b-ii, see docs/plans/context-split/05b-plan.md §2.2). Because every returned reference comes from a distinct iter_mut slot, the borrows are provably non-overlapping and no unsafe is required.

ids must be distinct and in bounds; the returned vector has one reference per requested id, positionally aligned with ids.

§Panics

Panics if ids contains a duplicate id or an out-of-bounds id.

Trait Implementations§

Source§

impl<Id: Identifier, T: Clone> Clone for Registry<Id, T>

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<Id: Identifier, T: Debug> Debug for Registry<Id, T>

Source§

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

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

impl<Id: Identifier, T> Default for Registry<Id, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, Id: Identifier, T: Deserialize<'de>> Deserialize<'de> for Registry<Id, T>

Source§

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

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

impl<Id: Identifier, T: Eq> Eq for Registry<Id, T>

Source§

impl<Id: Identifier, T> FromIterator<T> for Registry<Id, T>

Source§

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

Creates a value from an iterator. Read more
Source§

impl<Id: Identifier, T> Index<Id> for Registry<Id, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: Id) -> &Self::Output

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

impl<Id: Identifier, T> IndexMut<Id> for Registry<Id, T>

Source§

fn index_mut(&mut self, index: Id) -> &mut Self::Output

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

impl<Id: Identifier, T: Intern> Intern for Registry<Id, T>

Source§

type Static = Registry<Id, <T as Intern>::Static>

Source§

fn intern(self, pool: &mut StringPool) -> Self::Static

Source§

impl<'a, Id: Identifier, T> IntoIterator for &'a Registry<Id, T>

Source§

type Item = Identified<Id, &'a T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Id, 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<'a, Id: Identifier, T> IntoIterator for &'a mut Registry<Id, T>

Source§

type Item = Identified<Id, &'a mut T>

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, Id, 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<Id: Identifier, T> IntoIterator for Registry<Id, T>

Source§

type Item = Identified<Id, T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<Id, 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<Id: Identifier, T: PartialEq> PartialEq for Registry<Id, T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<Id: Identifier, T: Serialize> Serialize for Registry<Id, T>

Source§

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

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<Id, T> Freeze for Registry<Id, T>
where Vec<Vec<T>>: Freeze, PhantomData<Id>: Freeze,

§

impl<Id, T> RefUnwindSafe for Registry<Id, T>

§

impl<Id, T> Send for Registry<Id, T>
where Vec<Vec<T>>: Send, PhantomData<Id>: Send,

§

impl<Id, T> Sync for Registry<Id, T>
where Vec<Vec<T>>: Sync, PhantomData<Id>: Sync,

§

impl<Id, T> Unpin for Registry<Id, T>
where Vec<Vec<T>>: Unpin, PhantomData<Id>: Unpin,

§

impl<Id, T> UnsafeUnpin for Registry<Id, T>

§

impl<Id, T> UnwindSafe for Registry<Id, T>

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> 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.