Skip to main content

ListBuilder

Struct ListBuilder 

Source
pub struct ListBuilder<'a> {
    list: SEXP,
    _scope: &'a ProtectScope,
}
Expand description

Builder for constructing lists with efficient protection management.

ListBuilder holds a reference to a ProtectScope, allowing multiple elements to be inserted without repeatedly protecting/unprotecting each one. This is more efficient than using List::set_elt in a loop.

§Example

unsafe fn build_list(n: isize) -> SEXP {
    let scope = ProtectScope::new();
    let builder = ListBuilder::new(&scope, n);

    for i in 0..n {
        // Allocations inside the loop are protected by the scope
        let child = scope.alloc_real(10).into_raw();
        builder.set(i, child);
    }

    builder.into_sexp()
}

Fields§

§list: SEXP§_scope: &'a ProtectScope

Implementations§

Source§

impl<'a> ListBuilder<'a>

Source

pub unsafe fn new(scope: &'a ProtectScope, len: usize) -> Self

Create a new list builder with the given length.

The list is allocated and protected using the provided scope.

§Safety

Must be called from the R main thread.

Source

pub unsafe fn new_unchecked(scope: &'a ProtectScope, len: usize) -> Self

Create a new list builder via the unchecked FFI allocation path.

_unchecked twin of new: the VECSXP is allocated with Rf_allocVector_unchecked (see ProtectScope::alloc_vecsxp_unchecked), bypassing the main-thread assertion. Use inside ALTREP callbacks, with_r_unwind_protect, or with_r_thread bodies, and pair element insertion with set_unchecked.

§Safety

Must be called from the R main thread, in a context where the checked-FFI assertion is intentionally bypassed (see CLAUDE.md “FFI thread checking”).

Source

pub unsafe fn from_protected(scope: &'a ProtectScope, list: SEXP) -> Self

Create a builder wrapping an existing protected list.

§Safety
  • Must be called from the R main thread
  • list must be a valid, protected VECSXP
Source

pub unsafe fn set(&self, idx: isize, child: SEXP)

Set an element at the given index.

The child should be protected by the same scope (or a parent scope). Use scope.protect_raw(...) before calling this method.

§Safety
  • child must be a valid SEXP
  • child should be protected (typically via the same scope)
Source

pub unsafe fn set_unchecked(&self, idx: isize, child: SEXP)

Set an element via the unchecked FFI path.

_unchecked twin of set: inserts with set_vector_elt_unchecked, bypassing the main-thread assertion. Pair with new_unchecked inside ALTREP callbacks, with_r_unwind_protect, or with_r_thread bodies.

§Safety
  • child must be a valid SEXP, already protected (typically by the same scope, or by being inserted into this protected parent immediately after allocation with no intervening allocation)
  • Must be called from the R main thread, in a checked-bypass context
Source

pub unsafe fn set_protected(&self, idx: isize, child: SEXP)

Set an element, protecting the child within the builder’s scope.

This is a convenience method that protects the child and then inserts it.

§Safety
  • child must be a valid SEXP
Source

pub fn as_sexp(&self) -> SEXP

Get the underlying list SEXP.

Source

pub fn into_list(self) -> List

Convert to a List wrapper.

Source

pub fn into_sexp(self) -> SEXP

Convert to the underlying SEXP.

Source

pub fn len(&self) -> isize

Get the length of the list.

Source

pub fn is_empty(&self) -> bool

Check if the list is empty.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for ListBuilder<'a>

§

impl<'a> !Send for ListBuilder<'a>

§

impl<'a> !Sync for ListBuilder<'a>

§

impl<'a> !UnwindSafe for ListBuilder<'a>

§

impl<'a> Freeze for ListBuilder<'a>

§

impl<'a> Unpin for ListBuilder<'a>

§

impl<'a> UnsafeUnpin for ListBuilder<'a>

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> SizeHint for T
where T: ?Sized,

Source§

default fn lower_bound(&self) -> usize

🔬This is a nightly-only experimental API. (core_io_internals)
Returns a lower bound on the number of elements this container-like item contains. For example, an array [u8; 12] could return any value between 0 and 12 inclusively as a correct implementation. Read more
Source§

default fn upper_bound(&self) -> Option<usize>

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an upper bound on the number of elements this container-like item contains if it can be determined, otherwise None. Read more
Source§

final fn size_hint(&self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an estimate for the number of elements this container like type contains. Read more
Source§

impl<T> SizedTypeProperties for T

Source§

#[doc(hidden)]
const SIZE: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGNMENT: Alignment = _

🔬This is a nightly-only experimental API. (ptr_alignment_type)
Source§

#[doc(hidden)]
const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
Source§

#[doc(hidden)]
const LAYOUT: Layout = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const MAX_SLICE_LEN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
The largest safe length for a [Self]. Read more
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.

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: 16 bytes