pub struct ProtectedStrVec {
sexp: SEXP,
_root: Option<Preserved>,
children: ChildRoots,
len: isize,
}Expand description
GC-protected view over an R character vector (STRSXP).
Unlike StrVec (which is Copy and trusts the caller for GC protection),
ProtectedStrVec holds a Preserved root on the STRSXP for its Rust
lifetime. It can outlive a .Call frame and be dropped in any order
relative to other handles.
Borrowed strings and iterators are tied to &self. It is !Send + !Sync:
reading and releasing the R object must happen on R’s main thread.
§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§
§sexp: SEXP§_root: Option<Preserved>None for from_sexp_trusted, whose caller
roots sexp. The field also makes the handle !Send + !Sync.
children: ChildRoots§len: isizeImplementations§
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.
Roots the SEXP with a Preserved handle, released on drop,
independently of R’s protection stack. Use
from_sexp_trusted only when another owner
guarantees reachability for this handle’s entire lifetime.
§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) for the handle’s entire lifetime.
Avoids an additional preserved root.
The lifetime-bound &str borrows are still enforced — this only
skips acquiring a root; the unsafe caller must supply it.
§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).
Trait Implementations§
Source§impl Debug for ProtectedStrVec
impl Debug for ProtectedStrVec
Source§impl<'a> IntoIterator for &'a ProtectedStrVec
impl<'a> IntoIterator for &'a ProtectedStrVec
Source§impl IntoR for ProtectedStrVec
impl IntoR for ProtectedStrVec
Source§impl TryFromSexp for ProtectedStrVec
impl TryFromSexp for ProtectedStrVec
Source§const CHARACTER_ONLY: bool = true
const CHARACTER_ONLY: bool = true
NULL, is a character vector or a factor. Read moreSource§fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error>
fn try_from_sexp(sexp: SEXP) -> Result<Self, Self::Error>
Source§const NATIVE_BORROW: Option<NativeBorrow> = None
const NATIVE_BORROW: Option<NativeBorrow> = None
Source§#[doc(hidden)]const __MX_EXPECTATION: Option<&'static str> = None
#[doc(hidden)]const __MX_EXPECTATION: Option<&'static str> = None
#[miniextendr] cannot tell from the type as written: the
<expected> of a failed conversion’s '<p>' must be <expected>: .... Read moreSource§unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error>
unsafe fn try_from_sexp_unchecked(sexp: SEXP) -> Result<Self, Self::Error>
Source§#[doc(hidden)]unsafe fn __mx_try_from_rooted(root: Root<'_>) -> Result<Self, Self::Error>
#[doc(hidden)]unsafe fn __mx_try_from_rooted(root: Root<'_>) -> Result<Self, Self::Error>
Source§#[doc(hidden)]fn __mx_has_na(&self) -> bool
#[doc(hidden)]fn __mx_has_na(&self) -> bool
#[miniextendr(no_na)]
refuses after the conversion. Read moreSource§#[doc(hidden)]fn __mx_input_has_na(&self, input: SEXP) -> bool
#[doc(hidden)]fn __mx_input_has_na(&self, input: SEXP) -> bool
#[miniextendr(no_na)] refuses this converted argument, read
from input (the R value it converted from), when no R guard ran. Read moreSource§#[doc(hidden)]fn __mx_try_from_sexp_with_check(
sexp: SEXP,
check: &ElementCheck<'_>,
) -> Result<Self, SexpError>
#[doc(hidden)]fn __mx_try_from_sexp_with_check(
sexp: SEXP,
check: &ElementCheck<'_>,
) -> Result<Self, SexpError>
check, an element type’s
TryFromSexpElement::check_sexp,
on the R value that carries an element’s class (#1837). Read moreSource§#[doc(hidden)]unsafe fn __mx_try_from_sexp_unchecked_with_check(
sexp: SEXP,
check: &ElementCheck<'_>,
) -> Result<Self, SexpError>
#[doc(hidden)]unsafe fn __mx_try_from_sexp_unchecked_with_check(
sexp: SEXP,
check: &ElementCheck<'_>,
) -> Result<Self, SexpError>
Auto Trait Implementations§
impl !Freeze for ProtectedStrVec
impl !RefUnwindSafe for ProtectedStrVec
impl !Send for ProtectedStrVec
impl !Sync for ProtectedStrVec
impl Unpin for ProtectedStrVec
impl UnsafeUnpin for ProtectedStrVec
impl UnwindSafe for ProtectedStrVec
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> RDebug for Twhere
T: Debug,
impl<T> RDebug for Twhere
T: Debug,
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: 48 bytes