Skip to main content

Module newtype_derive

Module newtype_derive 

Source
Expand description

#[derive(TryFromSexp)] / #[derive(IntoR)] — forward R↔Rust conversions from a single-field newtype to its inner type.

For a newtype struct UserId(Uuid) (or struct UserId { inner: Uuid }):

  • #[derive(TryFromSexp)] generates the R → Rust direction: a scalar TryFromSexp impl that forwards to the inner type, plus a FromRNewtype marker impl. The marker lets the container blankets in miniextendr_api::newtype light up Vec<UserId> / Option<UserId> / Vec<Option<UserId>> — see issue #844.
  • #[derive(IntoR)] generates the Rust → R direction: a scalar IntoR impl that forwards to the inner type, plus IntoRNewtype (for Option / Vec<Option>) and a concrete IntoRVecElement impl (for Vec).

Direction is chosen by which derive you list — there are no attributes. Some inner types are convertible in only one direction (a compiled regex::Regex reads from R but cannot be written back): derive only TryFromSexp for those.

#[derive(TryFromSexp)]            // R -> Rust only
struct Pattern(regex::Regex);

#[derive(TryFromSexp, IntoR)]     // round-trip
struct UserId(uuid::Uuid);

Do not derive both IntoR and MatchArg on the same type: both would feed the single IntoR for Vec<T> blanket slot and collide (E0119).

Structs§

Newtype 🔒
The single field of a newtype: its inner type plus how to wrap/unwrap it.

Functions§

derive_into_r
#[derive(IntoR)]: scalar forwarding IntoR + IntoRNewtype marker (for Option/Vec<Option> blankets) + concrete IntoRVecElement (for Vec).
derive_try_from_sexp
#[derive(TryFromSexp)]: scalar forwarding TryFromSexp + FromRNewtype marker.
parse_newtype 🔒
where_with 🔒
Build a where clause that merges the struct’s existing predicates with one extra predicate (the Inner: Trait bound the forwarding impl requires).