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).
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§type Error = Infallible
type Error = Infallible
Source§fn try_into_sexp(self) -> Result<SEXP, Self::Error>
fn try_into_sexp(self) -> Result<SEXP, Self::Error>
Source§impl TryFromSexp for ProtectedStrVec
impl TryFromSexp for ProtectedStrVec
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> SizeEq<MaybeUninit<T>> for T
impl<T> SizeEq<MaybeUninit<T>> for T
type CastFrom = CastSizedExact
Source§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 moreimpl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> InvariantsEq<ManuallyDrop<T>> for Twhere
T: ?Sized,
impl<T> InvariantsEq<T> for Twhere
T: ?Sized,
impl<T> InvariantsEq<Unalign<T>> for T
impl<T> InvariantsEq<Wrapping<T>> for T
impl<Src, Dst, A, SV, DV, R> MutationCompatible<Src, A, SV, DV, (BecauseRead, R)> for Dst
impl<Src, Dst, A, SV, DV> MutationCompatible<Src, A, SV, DV, BecauseInvariantsEq> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: TransmuteFrom<Src, SV, DV> + InvariantsEq<Src> + ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Cell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ManuallyDrop<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ReadOnly<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
impl<T> TransmuteFrom<Unalign<T>, Valid, Valid> for T
impl<T> TransmuteFrom<UnsafeCell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Wrapping<T>, Valid, Valid> for T
impl<Src, Dst, A, SV, DV, C, R> TransmuteFromPtr<Src, A, SV, DV, C, R> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
C: CastExact<Src, Dst>,
Dst: TransmuteFrom<Src, SV, DV> + TryTransmuteFromPtr<Src, A, SV, DV, C, R> + ?Sized,
Src: ?Sized,
impl<Src, Dst, SV, DV, A, C, R> TryTransmuteFromPtr<Src, A, SV, DV, C, (BecauseMutationCompatible, R)> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: MutationCompatible<Src, A, SV, DV, R> + ?Sized,
C: CastExact<Src, Dst>,
Layout§
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: 32 bytes