Skip to main content

Arena

Struct Arena 

Source
pub struct Arena<M: MapStorage> {
    state: RefCell<ArenaState<M>>,
    _nosend: PhantomData<Rc<()>>,
}
Expand description

A reference-counted arena for GC protection, generic over map type.

This provides an alternative to R’s PROTECT stack that:

  • Uses reference counting for each SEXP
  • Allows releasing protections in any order
  • Has no stack size limit (uses heap allocation)

§Type Aliases

  • RefCountedArena = Arena<BTreeMap<...>> (ordered, good for ref counting)
  • HashMapArena = Arena<HashMap<...>> (faster for large collections)

Fields§

§state: RefCell<ArenaState<M>>§_nosend: PhantomData<Rc<()>>

Implementations§

Source§

impl<M: MapStorage> Arena<M>

Source

pub unsafe fn new() -> Self

Create a new arena with default capacity (16 slots).

For workloads protecting many distinct SEXPs (e.g., ppsize-scale loops), prefer with_capacity to avoid backing VECSXP growth and map rehashing during operation.

§Safety

Must be called from the R main thread.

Source

pub unsafe fn with_capacity(capacity: usize) -> Self

Create a new arena with specific initial capacity.

Pre-sizing the arena avoids growth of the backing VECSXP and rehashing of the internal map. Use this when the expected number of distinct protected values is known or can be estimated.

§Safety

Must be called from the R main thread.

Source

pub unsafe fn protect(&self, x: SEXP) -> SEXP

Protect a SEXP, incrementing its reference count.

§Safety

Must be called from the R main thread.

Source

pub unsafe fn unprotect(&self, x: SEXP)

Unprotect a SEXP, decrementing its reference count.

§Safety

Must be called from the R main thread.

§Panics

Panics if x was not protected by this arena.

Source

pub unsafe fn try_unprotect(&self, x: SEXP) -> bool

Try to unprotect a SEXP, returning true if it was protected.

§Safety

Must be called from the R main thread.

Source

pub fn is_protected(&self, x: SEXP) -> bool

Check if a SEXP is currently protected by this arena.

Source

pub fn ref_count(&self, x: SEXP) -> usize

Get the reference count for a SEXP (0 if not protected).

Source

pub fn len(&self) -> usize

Get the number of distinct SEXPs currently protected.

Source

pub fn is_empty(&self) -> bool

Check if the arena is empty.

Source

pub fn capacity(&self) -> usize

Get the current capacity.

Source

pub unsafe fn clear(&self)

Clear all protections.

§Safety

Must be called from the R main thread.

Source

pub unsafe fn guard(&self, x: SEXP) -> ArenaGuard<'_, M>

Protect a SEXP and return an RAII guard.

§Safety

Must be called from the R main thread.

Trait Implementations§

Source§

impl<M: MapStorage> Default for Arena<M>

Source§

fn default() -> Self

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

impl<M: MapStorage> Drop for Arena<M>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<M> !Freeze for Arena<M>

§

impl<M> !RefUnwindSafe for Arena<M>

§

impl<M> !Send for Arena<M>

§

impl<M> !Sync for Arena<M>

§

impl<M> Unpin for Arena<M>
where M: Unpin,

§

impl<M> UnsafeUnpin for Arena<M>
where M: UnsafeUnpin,

§

impl<M> UnwindSafe for Arena<M>
where M: UnwindSafe,

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> 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> RDefault for T
where T: Default,

Source§

fn default() -> T

Create a new instance with default values.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.