Skip to main content

RustConversionBuilder

Struct RustConversionBuilder 

Source
pub struct RustConversionBuilder {
    coerce_all: bool,
    coerce_params: Vec<String>,
    strict: bool,
    match_arg_several_ok_params: Vec<String>,
}
Expand description

Builder for generating Rust conversion statements from R SEXP parameters.

Handles:

  • Unit types () → identity binding
  • &Dots → special wrapper with storage
  • Slices &[T] → TryFromSexp
  • &str → String + Borrow (for worker thread compatibility)
  • Scalar references → DATAPTR_RO_unchecked
  • Coercion → extract R native type + TryCoerce
  • Default → TryFromSexp

Fields§

§coerce_all: bool

Enable coercion for all parameters

§coerce_params: Vec<String>

Parameter names that should use coercion

§strict: bool

Enable strict input conversion for lossy types

§match_arg_several_ok_params: Vec<String>

Parameter names with match_arg + several_ok — use match_arg_vec_from_sexp instead of TryFromSexp.

Implementations§

Source§

impl RustConversionBuilder

Source

pub fn new() -> Self

Create a new conversion builder.

Source

pub fn with_coerce_all(self) -> Self

Enable coercion for all parameters.

Source

pub fn with_coerce_param(self, param_name: String) -> Self

Add a single parameter name that should use coercion.

param_name is matched against the identifier in the function signature. Can be called multiple times to add several parameters.

Source

pub fn with_strict(self) -> Self

Enable strict input conversion for lossy types (i64/u64/isize/usize + Vec variants).

Source

pub fn with_match_arg_several_ok(self, param_name: String) -> Self

Mark a parameter as match_arg + several_ok — uses match_arg_vec_from_sexp instead of TryFromSexp for converting STRSXP → Vec<EnumType>.

Source

fn should_coerce(&self, param_name: &str) -> bool

Check if a parameter should use coercion.

Returns true if coerce_all is set or param_name appears in the per-parameter list.

Source

fn conversion_stmt( &self, try_expr: TokenStream, error_msg: &str, ident: &Ident, ty: &Type, span: Span, ) -> TokenStream

Generate a conversion expression that returns a tagged condition SEXP on failure.

The R wrapper inspects .val and raises a structured rust_* condition; the return happens from inside the C wrapper body before any further conversion.

  • try_expr: The Result<T, E>-producing expression
  • error_msg: Human-readable error message for the failure
  • ident: The binding name for the converted value
  • ty: The target Rust type (for the let binding)
  • span: Source span for error reporting
Source

fn conversion_stmt_untyped( &self, try_expr: TokenStream, error_msg: &str, ident: &Ident, span: Span, ) -> TokenStream

Like [conversion_stmt] but without a type annotation on the binding.

Source

pub fn build_conversion( &self, pat_type: &PatType, sexp_ident: &Ident, ) -> Vec<TokenStream>

Generate conversion statement for a single parameter.

This is the non-split variant: owned conversions and borrow statements are concatenated into a single list, suitable for main-thread execution where everything runs in the same scope.

  • pat_type: the typed pattern from the function signature (e.g., x: i32).
  • sexp_ident: the identifier of the raw SEXP variable holding the R argument.

Returns a flat list of let binding statements that convert sexp_ident into the Rust type declared in pat_type.

Source

pub fn build_conversion_split( &self, pat_type: &PatType, sexp_ident: &Ident, ) -> (Vec<TokenStream>, Vec<TokenStream>)

Generate conversion statements split into two phases for worker thread execution.

For reference types like &str, we need to:

  1. Convert SEXP to owned type (String) – runs on the main thread before the worker closure, so the owned value can be moved into the closure.
  2. Borrow from the owned type (&str) – runs inside the worker closure.

For non-reference types (scalars, Vec, etc.) everything goes into the first phase and the second vec is empty.

  • pat_type: the typed pattern from the function signature (e.g., s: &str).
  • sexp_ident: the identifier of the raw SEXP variable holding the R argument.

Returns (owned_conversions, borrow_statements) where each element is a list of let binding token streams.

Source

pub fn build_conversions( &self, inputs: &Punctuated<FnArg, Comma>, sexp_idents: &[Ident], ) -> Vec<TokenStream>

Generate conversion statements for all parameters in a function signature.

Iterates over inputs (the function’s parameter list) paired with sexp_idents (the corresponding SEXP variable names), calling build_conversion for each typed parameter. Receiver parameters (self) are silently skipped.

Returns a flat list of all conversion statements, in parameter order.

Trait Implementations§

Source§

impl Default for RustConversionBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> SizedTypeProperties for T

Source§

#[doc(hidden)]
const SIZE: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGNMENT: Alignment = _

🔬This is a nightly-only experimental API. (ptr_alignment_type)
Source§

#[doc(hidden)]
const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
Source§

#[doc(hidden)]
const LAYOUT: Layout = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const MAX_SLICE_LEN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
The largest safe length for a [Self]. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.

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: 56 bytes