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 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_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> Freeze for ListBuilder<'a>

§

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

§

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

§

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

§

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

§

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

§

impl<'a> !UnwindSafe 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, 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.