pub struct StrVec(SEXP);Expand description
Owned handle to an R character vector (STRSXP).
This wrapper provides safe methods for building character vectors element-by-element with proper GC protection.
Tuple Fields§
§0: SEXPImplementations§
Source§impl StrVec
impl StrVec
Sourcepub const unsafe fn from_raw(sexp: SEXP) -> Self
pub const unsafe fn from_raw(sexp: SEXP) -> Self
Wrap an existing STRSXP without additional checks.
§Safety
Caller must ensure sexp is a valid character vector (STRSXP)
whose lifetime remains managed by R.
Sourcepub fn get_charsxp(self, idx: isize) -> Option<SEXP>
pub fn get_charsxp(self, idx: isize) -> Option<SEXP>
Get the CHARSXP at the given index.
Returns None if out of bounds.
Sourcepub fn get_str(self, idx: isize) -> Option<&'static str>
pub fn get_str(self, idx: isize) -> Option<&'static str>
Get the string at the given index (zero-copy).
Returns None if out of bounds or if the element is NA_character_.
Panics if the CHARSXP is not valid UTF-8 (should not happen in a UTF-8 locale).
Sourcepub fn get_cow(self, idx: isize) -> Option<Cow<'static, str>>
pub fn get_cow(self, idx: isize) -> Option<Cow<'static, str>>
Get the string at the given index as Cow<str> (encoding-safe).
Returns Cow::Borrowed for UTF-8 strings (zero-copy), Cow::Owned for
non-UTF-8 strings (translated via Rf_translateCharUTF8).
Returns None if out of bounds or NA_character_.
Sourcepub fn iter(self) -> StrVecIter ⓘ
pub fn iter(self) -> StrVecIter ⓘ
Iterate over elements as Option<&str>.
NA_character_ elements yield None, valid strings yield Some(&str).
Zero-copy — each &str borrows directly from R’s CHARSXP.
Sourcepub fn iter_cow(self) -> StrVecCowIter ⓘ
pub fn iter_cow(self) -> StrVecCowIter ⓘ
Iterate over elements as Option<Cow<str>> (encoding-safe).
Like iter but handles non-UTF-8 CHARSXPs gracefully.
Sourcepub unsafe fn set_charsxp(self, idx: isize, charsxp: SEXP)
pub unsafe fn set_charsxp(self, idx: isize, charsxp: SEXP)
Set a CHARSXP at the given index, protecting it during insertion.
This is the safe way to insert a freshly allocated CHARSXP into a string vector.
§Safety
- Must be called from the R main thread
charsxpmust be a valid CHARSXP (fromRf_mkChar*orSTRING_ELT)selfmust be a valid, protected STRSXP
§Panics
Panics if idx is out of bounds.
Sourcepub unsafe fn set_charsxp_unchecked(self, idx: isize, charsxp: SEXP)
pub unsafe fn set_charsxp_unchecked(self, idx: isize, charsxp: SEXP)
Set a CHARSXP without protecting it.
§Safety
In addition to the safety requirements of set_charsxp:
- The caller must ensure
charsxpis already protected or from the global CHARSXP cache.