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 ProtectScopeImplementations§
Source§impl<'a> ListBuilder<'a>
impl<'a> ListBuilder<'a>
Sourcepub unsafe fn new(scope: &'a ProtectScope, len: usize) -> Self
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.
Sourcepub unsafe fn new_unchecked(scope: &'a ProtectScope, len: usize) -> Self
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”).
Sourcepub unsafe fn from_protected(scope: &'a ProtectScope, list: SEXP) -> Self
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
listmust be a valid, protected VECSXP
Sourcepub unsafe fn set(&self, idx: isize, child: SEXP)
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
childmust be a valid SEXPchildshould be protected (typically via the same scope)
Sourcepub unsafe fn set_unchecked(&self, idx: isize, child: SEXP)
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
childmust 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
Sourcepub unsafe fn set_protected(&self, idx: isize, child: SEXP)
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
childmust be a valid SEXP
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> 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> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§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 moreLayout§
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