pub struct ProtectPool {
backing: SEXP,
capacity: usize,
generations: Vec<u32>,
free_slots: Vec<usize>,
next_slot: usize,
len: usize,
_nosend: PhantomData<Rc<()>>,
}Expand description
A VECSXP-backed pool for GC protection with generational keys.
§Example
let mut pool = unsafe { ProtectPool::new(16) };
let key = unsafe { pool.insert(some_sexp) };
// SEXP is now protected from GC
let sexp = pool.get(key).unwrap();
// Use the SEXP...
unsafe { pool.release(key) };
// SEXP is no longer protected (eligible for GC)Fields§
§backing: SEXPThe VECSXP that holds protected SEXPs. Anchored by R_PreserveObject.
capacity: usizeCurrent capacity of the backing VECSXP.
generations: Vec<u32>Generation counter per VECSXP slot. Incremented on each release.
A key is valid iff generations[key.slot] == key.generation.
free_slots: Vec<usize>Free VECSXP slot indices for reuse.
next_slot: usizeNext fresh VECSXP slot index (for when free_slots is empty).
len: usizeNumber of currently protected objects.
_nosend: PhantomData<Rc<()>>Implementations§
Source§impl ProtectPool
impl ProtectPool
Sourcepub const DEFAULT_CAPACITY: usize = 16
pub const DEFAULT_CAPACITY: usize = 16
Initial default capacity.
Sourcepub unsafe fn with_capacity(capacity: usize) -> Self
pub unsafe fn with_capacity(capacity: usize) -> Self
Sourcepub unsafe fn insert(&mut self, sexp: SEXP) -> ProtectKey
pub unsafe fn insert(&mut self, sexp: SEXP) -> ProtectKey
Protect a SEXP, returning a generational key.
The SEXP will be protected from GC until release is called
with the returned key. If the key is dropped without calling release, the
SEXP remains protected (leak, not crash).
§Safety
Must be called from the R main thread. sexp must be a valid SEXP.
§Panics
Panics if the pool has grown beyond u32::MAX slots.
Sourcepub unsafe fn release(&mut self, key: ProtectKey)
pub unsafe fn release(&mut self, key: ProtectKey)
Release a previously protected SEXP.
If the key is stale (already released, or from a different pool), this is a no-op.
§Safety
Must be called from the R main thread.
Sourcepub fn get(&self, key: ProtectKey) -> Option<SEXP>
pub fn get(&self, key: ProtectKey) -> Option<SEXP>
Get the SEXP for a key, or None if the key is stale.
Sourcepub unsafe fn replace(&mut self, key: ProtectKey, sexp: SEXP) -> bool
pub unsafe fn replace(&mut self, key: ProtectKey, sexp: SEXP) -> bool
Overwrite the SEXP at an existing key without releasing/reinserting.
Returns true if the key was valid and the value was replaced.
Returns false if the key was stale (no-op).
This is the pool equivalent of R_Reprotect — O(1), no allocation.
§Safety
Must be called from the R main thread. sexp must be a valid SEXP.
Sourcepub fn contains_key(&self, key: ProtectKey) -> bool
pub fn contains_key(&self, key: ProtectKey) -> bool
Check if a key is currently valid (not stale).
fn alloc_slot(&mut self) -> usize
unsafe fn grow(&mut self)
Trait Implementations§
Source§impl Drop for ProtectPool
impl Drop for ProtectPool
Auto Trait Implementations§
impl Freeze for ProtectPool
impl RefUnwindSafe for ProtectPool
impl !Send for ProtectPool
impl !Sync for ProtectPool
impl Unpin for ProtectPool
impl UnsafeUnpin for ProtectPool
impl UnwindSafe for ProtectPool
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> SizeEq<MaybeUninit<T>> for T
impl<T> SizeEq<MaybeUninit<T>> for T
type CastFrom = CastSizedExact
Source§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read moreimpl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> InvariantsEq<ManuallyDrop<T>> for Twhere
T: ?Sized,
impl<T> InvariantsEq<T> for Twhere
T: ?Sized,
impl<T> InvariantsEq<Unalign<T>> for T
impl<T> InvariantsEq<Wrapping<T>> for T
impl<Src, Dst, A, SV, DV, R> MutationCompatible<Src, A, SV, DV, (BecauseRead, R)> for Dst
impl<Src, Dst, A, SV, DV> MutationCompatible<Src, A, SV, DV, BecauseInvariantsEq> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: TransmuteFrom<Src, SV, DV> + InvariantsEq<Src> + ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Cell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ManuallyDrop<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ReadOnly<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
impl<T> TransmuteFrom<Unalign<T>, Valid, Valid> for T
impl<T> TransmuteFrom<UnsafeCell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Wrapping<T>, Valid, Valid> for T
impl<Src, Dst, A, SV, DV, C, R> TransmuteFromPtr<Src, A, SV, DV, C, R> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
C: CastExact<Src, Dst>,
Dst: TransmuteFrom<Src, SV, DV> + TryTransmuteFromPtr<Src, A, SV, DV, C, R> + ?Sized,
Src: ?Sized,
impl<Src, Dst, SV, DV, A, C, R> TryTransmuteFromPtr<Src, A, SV, DV, C, (BecauseMutationCompatible, R)> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: MutationCompatible<Src, A, SV, DV, R> + ?Sized,
C: CastExact<Src, Dst>,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 80 bytes