Skip to main content

TryFromSexpElement

Trait TryFromSexpElement 

Source
pub trait TryFromSexpElement: Sized {
    type Inner;

    // Required method
    fn from_inner(inner: Self::Inner) -> Self;

    // Provided method
    fn check_sexp(_sexp: SEXP) -> Result<(), SexpError> { ... }
}
Expand description

The element side of the TryFromSexp impls for Vec<T>, Option<T> and Vec<Option<T>> (R → Rust).

Each container of Self is read as the same container of Inner, then every element is wrapped with from_inner: Vec<Self> reads Vec<Self::Inner>, Option<Self> reads Option<Self::Inner>, and Vec<Option<Self>> reads Vec<Option<Self::Inner>>. Each shape exists when the inner container converts, independently of the other two, and keeps its NA policy and metadata (NATIVE_BORROW, CHARACTER_ONLY); its error converts into SexpError.

§Forwarding

No container calls <Self as TryFromSexp>::try_from_sexp. They read the inner type and wrap it, so the element’s own try_from_sexp, when it has one, must do the same: check_sexp, then the inner type’s conversion, then from_inner. Anything else it did would hold for a scalar argument and silently not for Option<Self>, Vec<Self> or Vec<Option<Self>>. A value the inner type accepts but Self refuses is refused in check_sexp, which every shape runs.

Emitted by #[derive(TryFromSexp)] (the inner type is the newtype’s field; #[try_from_sexp(validate = path)] gives the check) and by try_from_sexp_via_str_parse! (the inner type is ParsedRStr<Self>). See the module docs for why both share this one trait.

Required Associated Types§

Source

type Inner

The type whose containers are read first.

Required Methods§

Source

fn from_inner(inner: Self::Inner) -> Self

Wrap an inner value into Self.

Provided Methods§

Source

fn check_sexp(_sexp: SEXP) -> Result<(), SexpError>

Refuse an R value before it is read: Err for a value the inner type would accept but Self must not, such as a number that carries a unit class (difftime, Date, POSIXct).

Called with the input of every shape, before its inner conversion: by the scalar TryFromSexp (the derive’s calls it), for Option<Self> on any input but NULL, which stays “not given”, and for Vec<Self> and Vec<Option<Self>> where R keeps the element’s class: once on the whole vector for an atomic inner type, on each element (but NULL) for a list of R objects (Self::Inner is List, or a newtype over it). It sees the value as R holds it, attributes and all, before anything is read from it.

Return an RError, or any RConditionData type, with ? or .into() to give the refusal its own classes and fields (SexpError::Condition). As an argument error it keeps the wrapper’s context: '<p>' must be <expected>: <message>, the classes before the crate’s conversion_error_class, e$param and e$rust_type. A plain SexpError (SexpError::InvalidValue) works too, with no classes.

The default accepts every value; it is what the derive emits without validate and what a string-parsed type has.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§