pub struct StableVec<T> { /* private fields */ }
Expand description
A vector type with indices stable across removes & inserts, and which reuses deleted indices; comparable to Vec<Option
This is very similar to the Slab crate, which we aren’t using because Slab’s Vec entries are at
least size_of<usize>() + 1
bytes – since we’re using this primarily to store things smaller
than usize, it’s much more efficient to use this.
There’s also a stable-vec crate but that was last updated in 2019, so I think it’s unmaintained.
Honestly, at the moment, I’m not really happy with this. It’s all a bit haphazard.
Implementations
sourceimpl<T> StableVec<T>
impl<T> StableVec<T>
sourcepub unsafe fn from_parts(
data: NonNull<MaybeUninit<T>>,
flags: BitVec,
cap: usize,
init_amt: usize,
farthest_init: Option<usize>
) -> Self
pub unsafe fn from_parts(
data: NonNull<MaybeUninit<T>>,
flags: BitVec,
cap: usize,
init_amt: usize,
farthest_init: Option<usize>
) -> Self
Safety
data
must be allocatedflags
must have length = length ofdata
allocationcap
= length ofdata
allocationinit_amt
= number of initialized entries indata
farthest_init
= index of initialized value at highest index, or None if no initialized values
pub fn new() -> Self
pub fn farthest_init_index(&self) -> Option<usize>
pub fn with_capacity(cap: usize) -> Self
sourcepub fn spare_slots(&self) -> usize
pub fn spare_slots(&self) -> usize
Get the number of empty spaces within this StableVec
sourcepub fn spare_capacity(&self) -> usize
pub fn spare_capacity(&self) -> usize
Get the number of elements that can be added to this StableVec without allocation
sourcepub unsafe fn get_raw_unsafe(&self, index: usize) -> &MaybeUninit<T>
pub unsafe fn get_raw_unsafe(&self, index: usize) -> &MaybeUninit<T>
Safety
index
<self.cap
sourcepub unsafe fn get_raw_unsafe_mut(&mut self, index: usize) -> &mut MaybeUninit<T>
pub unsafe fn get_raw_unsafe_mut(&mut self, index: usize) -> &mut MaybeUninit<T>
Safety
index
<self.cap
sourcepub unsafe fn get_unsafe(&self, index: usize) -> &T
pub unsafe fn get_unsafe(&self, index: usize) -> &T
Safety
index
<self.cap
self[index]
must already be initialized
sourcepub unsafe fn get_unsafe_mut(&mut self, index: usize) -> &mut T
pub unsafe fn get_unsafe_mut(&mut self, index: usize) -> &mut T
Safety
index
<self.cap
self[index]
must already be initialized
pub fn get_unchecked(&self, index: usize) -> &T
pub fn get_unchecked_mut(&mut self, index: usize) -> &mut T
pub fn get(&self, index: usize) -> Option<&T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn shrink_to_fit(&mut self)
pub fn from_vec(data: Vec<T>) -> Self
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>]
pub fn next_push_index(&self) -> usize
pub fn push(&mut self, data: T) -> usize
pub fn remove(&mut self, index: usize) -> Option<T>
pub fn replace(&mut self, index: usize, data: T) -> Option<T>
pub fn reserve_exact(&mut self, amt: usize)
pub fn reserve(&mut self, amt: usize)
pub fn push_iter<'s, Idx: PrimInt + 'static>(
&'s mut self,
iter: impl IntoIterator<Item = T> + 's
) -> impl Iterator<Item = Idx> + 'swhere
usize: AsPrimitive<Idx>,
pub fn extend_from_iter(
&mut self,
iter: impl IntoIterator<Item = T>
) -> Vec<usize>
pub fn extend_from_other(&mut self, other: Self) -> HashMap<usize, usize>
sourcepub unsafe fn prepare_raw_array<'s, const LEN: usize>(
&'s mut self
) -> [&'s mut MaybeUninit<T>; LEN]
pub unsafe fn prepare_raw_array<'s, const LEN: usize>(
&'s mut self
) -> [&'s mut MaybeUninit<T>; LEN]
Safety
- Must initialize all returned values
pub fn extend_from_slice_cloned(&mut self, slice: &[T]) -> Vec<usize>where
T: Clone,
pub fn extend_from_slice_copied(&mut self, slice: &[T]) -> Vec<usize>where
T: Copy,
pub fn as_uninit_slice(&self) -> &[MaybeUninit<T>]
pub fn as_uninit_slice_mut(&mut self) -> &mut [MaybeUninit<T>]
pub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn enumerate(&self) -> impl Iterator<Item = (usize, &T)>
pub fn is_init(&self, index: usize) -> bool
pub fn is_fragmented(&self) -> bool
sourcepub unsafe fn swap_unchecked(&mut self, a: usize, b: usize)
pub unsafe fn swap_unchecked(&mut self, a: usize, b: usize)
Safety
a
<self.cap
b
<self.cap
pub fn swap(&mut self, a: usize, b: usize)
sourcepub fn defragment(&mut self) -> Vec<(usize, usize)>
pub fn defragment(&mut self) -> Vec<(usize, usize)>
Partition self such that initialized values are stored contiguously from index 0; return the new locations of
each value in the form (from, to)
.
sourcepub fn compress(&mut self) -> Vec<(usize, usize)>
pub fn compress(&mut self) -> Vec<(usize, usize)>
Defragment & reallocate self such that self.cap
== self.init_amt
.
pub fn as_slice(&self) -> Option<&[T]>
pub fn as_slice_mut(&mut self) -> Option<&mut [T]>
pub fn init_flags(&self) -> &BitVec
Trait Implementations
sourceimpl<T> FromIterator<T> for StableVec<T>
impl<T> FromIterator<T> for StableVec<T>
sourcefn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Auto Trait Implementations
impl<T> RefUnwindSafe for StableVec<T>where
T: RefUnwindSafe,
impl<T> !Send for StableVec<T>
impl<T> !Sync for StableVec<T>
impl<T> Unpin for StableVec<T>
impl<T> UnwindSafe for StableVec<T>where
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> FmtForward for T
impl<T> FmtForward for T
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read morefn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read morefn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_ref()
into the pipe function.fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> Rwhere
Self: Deref<Target = T>,
T: 'a + ?Sized,
R: 'a,
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> Rwhere
Self: Deref<Target = T>,
T: 'a + ?Sized,
R: 'a,
self
, then passes self.deref()
into the pipe function.impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read morefn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.impl<T> Tap for T
impl<T> Tap for T
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Borrow<B>
of a value. Read morefn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
BorrowMut<B>
of a value. Read morefn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
AsRef<R>
view of a value. Read morefn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
AsMut<R>
view of a value. Read morefn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Deref::Target
of a value. Read morefn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Deref::Target
of a value. Read morefn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds. Read morefn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds. Read morefn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds. Read morefn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds. Read morefn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds. Read more