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