pub struct ProtectedStrVec {
inner: StrVec,
len: isize,
_protect: Option<OwnedProtect>,
}Expand description
GC-protected view over an R character vector (STRSXP).
Unlike StrVec (which is Copy and trusts the caller for GC protection),
ProtectedStrVec owns an OwnedProtect guard that keeps the STRSXP alive.
All borrowed data (&str, iterators) has its lifetime tied to &self,
not 'static — preventing use-after-GC bugs at compile time.
§When to use
StrVec: for SEXP arguments to.Call(R protects them), or when you manage protection yourself. Lightweight,Copy.ProtectedStrVec: when you allocate or receive an STRSXP and need to keep it alive beyond the immediate scope. NotCopy.
§Example
#[miniextendr]
pub fn count_unique(strings: ProtectedStrVec) -> i32 {
let unique: HashSet<&str> = strings.iter()
.filter_map(|s| s)
.collect();
unique.len() as i32
}Fields§
§inner: StrVec§len: isize§_protect: Option<OwnedProtect>Implementations§
Source§impl ProtectedStrVec
impl ProtectedStrVec
Sourcepub unsafe fn new(sexp: SEXP) -> Self
pub unsafe fn new(sexp: SEXP) -> Self
Create a protected view over an STRSXP.
Calls Rf_protect on the SEXP. Use from_sexp_trusted
when the SEXP is already protected (e.g., .Call arguments) to avoid
double-protecting.
§Safety
sexpmust be a valid STRSXP.- Must be called from the R main thread.
Sourcepub unsafe fn from_sexp_trusted(sexp: SEXP) -> Self
pub unsafe fn from_sexp_trusted(sexp: SEXP) -> Self
Create a view without adding GC protection.
Use this when the SEXP is already protected by R (e.g., a .Call
argument, or in a ProtectScope). Avoids the redundant
Rf_protect/Rf_unprotect pair.
The lifetime-bound &str borrows are still enforced — this only
skips the protect stack push, not the safety guarantees.
§Safety
sexpmust be a valid STRSXP.sexpmust remain GC-protected for the lifetime of this struct.- Must be called from the R main thread.
Sourcepub fn get_str(&self, idx: isize) -> Option<&str>
pub fn get_str(&self, idx: isize) -> Option<&str>
Get the string at index (zero-copy, lifetime tied to &self).
Returns None for out-of-bounds or NA_character_.
Sourcepub fn get_cow(&self, idx: isize) -> Option<Cow<'_, str>>
pub fn get_cow(&self, idx: isize) -> Option<Cow<'_, str>>
Get the string at index as Cow<str> (encoding-safe, lifetime tied to &self).
Sourcepub fn iter(&self) -> ProtectedStrVecIter<'_> ⓘ
pub fn iter(&self) -> ProtectedStrVecIter<'_> ⓘ
Iterate over elements as Option<&str> (lifetime tied to &self).
Sourcepub fn iter_cow(&self) -> ProtectedStrVecCowIter<'_> ⓘ
pub fn iter_cow(&self) -> ProtectedStrVecCowIter<'_> ⓘ
Iterate over elements as Option<Cow<str>> (encoding-safe).