Skip to main content

TryFromRoot

Trait TryFromRoot 

Source
pub trait TryFromRoot<'a>: Sized {
    type Error;

#[doc(hidden)]
const NATIVE_BORROW: Option<NativeBorrow> = None; // Required method fn try_from_root(root: Root<'a>) -> Result<Self, Self::Error>; }
Expand description

Convert an R value while retaining its root’s lifetime in the result.

This conversion carries the root lifetime that raw TryFromSexp cannot express. Generated wrappers use the rooted .Call argument for StrVec, RCow, and Cow inputs (including the character-vector Cow containers). Main-thread wrappers and trait shims also use it for explicit shared native references and optional shared references. Worker shared references copy to Rust-owned scalars, vectors or strings before dispatch, then borrow inside the worker. Nested shared-reference Vec inputs retain every list element in the root’s protection scope, even when an ALTREP accessor does not cache that element. A type alias can use this trait explicitly in the function body.

A root proves liveness, not exclusive access: this trait does not provide mutable references to R storage. Shared native borrows still participate in a wrapper’s alias checks against its other, mutable arguments.

ⓘ
use miniextendr_api::{Root, TryFromRoot};
use std::borrow::Cow;
fn escape<'a>(root: Root<'a>) -> Cow<'static, [i32]> {
    Cow::try_from_root(root).unwrap()
}
ⓘ
use miniextendr_api::{Root, TryFromRoot};
use std::borrow::Cow;
fn escape<'a>(root: Root<'a>) -> Vec<Option<Cow<'static, str>>> {
    Vec::try_from_root(root).unwrap()
}
ⓘ
use miniextendr_api::{Root, TryFromRoot};
fn escape<'a>(root: Root<'a>) -> &'static [i32] {
    <&[i32]>::try_from_root(root).unwrap()
}
ⓘ
use miniextendr_api::{Root, TryFromRoot};
fn escape<'a>(root: Root<'a>) -> Option<&'static str> {
    <Option<&str>>::try_from_root(root).unwrap()
}
ⓘ
use miniextendr_api::{Root, TryFromRoot};
fn escape<'a>(root: Root<'a>) -> Vec<Vec<&'static [i32]>> {
    Vec::try_from_root(root).unwrap()
}

Provided Associated Constants§

Source

#[doc(hidden)]
const NATIVE_BORROW: Option<NativeBorrow> = None

Native storage borrowed by this conversion, for wrapper alias checks.

Required Associated Types§

Source

type Error

The error reported when the R representation is not accepted.

Required Methods§

Source

fn try_from_root(root: Root<'a>) -> Result<Self, Self::Error>

Convert without allowing an R-backed borrow to outlive root.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'a, E: RootedVecElement<'a>> TryFromRoot<'a> for Option<Vec<E>>

NULL is None; anything else converts as the Vec.

Source§

const NATIVE_BORROW: Option<NativeBorrow> = E::NATIVE_BORROW

Source§

type Error = SexpError

Source§

fn try_from_root(root: Root<'a>) -> Result<Self, Self::Error>

Source§

impl<'a, E: RootedVecElement<'a>> TryFromRoot<'a> for Vec<E>

Source§

const NATIVE_BORROW: Option<NativeBorrow> = E::NATIVE_BORROW

Source§

type Error = SexpError

Source§

fn try_from_root(root: Root<'a>) -> Result<Self, Self::Error>

Source§

impl<'a, T: ?Sized + 'static> TryFromRoot<'a> for &'a T

A shared reference converts as its raw 'static form does, with the same checks, errors and alias metadata, and so does a custom TryFromSexp impl for a reference to the user’s own type. Only the lifetime changes: the result borrows from the root, not from 'static.

Source§

const NATIVE_BORROW: Option<NativeBorrow> = <&'static T as TryFromSexp>::NATIVE_BORROW

Source§

type Error = <&'static T as TryFromSexp>::Error

Source§

fn try_from_root(root: Root<'a>) -> Result<Self, Self::Error>

Source§

impl<'a, T: ?Sized + 'static> TryFromRoot<'a> for Option<&'a T>

NULL (and NA for strings) is None, as in the raw conversion.

Source§

const NATIVE_BORROW: Option<NativeBorrow> = <Option<&'static T> as TryFromSexp>::NATIVE_BORROW

Source§

type Error = <Option<&'static T> as TryFromSexp>::Error

Source§

fn try_from_root(root: Root<'a>) -> Result<Self, Self::Error>

Source§

impl<'a, T: RNativeType> TryFromRoot<'a> for Cow<'a, [T]>

Source§

impl<'a> TryFromRoot<'a> for Cow<'a, str>

Source§

type Error = SexpError

Source§

fn try_from_root(root: Root<'a>) -> Result<Self, Self::Error>

Implementors§

Source§

impl<'a, T: RNativeType> TryFromRoot<'a> for RCow<'a, T>

Source§

const NATIVE_BORROW: Option<NativeBorrow> = RCow<'a, T>::NATIVE_BORROW

Source§

type Error = SexpTypeError

Source§

impl<'a> TryFromRoot<'a> for StrVec<'a>