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 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_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