Skip to main content

Module into_r_as

Module into_r_as 

Source
Expand description

Storage-directed conversion to R.

This module provides IntoRAs, a trait for converting Rust values to R SEXPs with explicit target storage type selection.

§Value-Based Semantics

Conversions are runtime-checked: if the actual value fits the target type, it converts; if not, it errors. There is no “lossy” escape hatch - if you want lossy conversion, cast the values yourself first.

§Where this fits among the three outbound paths

  • crate::into_r::IntoRlax default: picks an R storage type for you (e.g. i64INTSXP if it fits, REALSXP otherwise). Silent.
  • crate::strictstrict opt-in via #[miniextendr(strict)]: panics (→ R error) if the value can’t fit INTSXP.
  • IntoRAs (this module) — storage-directed: the caller picks the target R type, the conversion errors with StorageCoerceError if any value doesn’t fit.

Failure mode of reaching for the default IntoR when you actually wanted storage control: an R-side is.integer() check fails because R received a REALSXP your tibble column wasn’t expecting.

§Example

use miniextendr_api::IntoRAs;

// These succeed (values fit)
let x = vec![1_i64, 2, 3];
let sexp = x.into_r_as::<i32>()?;           // OK: all values in i32 range

let y = vec![1.0_f64, 2.0, 3.0];
let sexp = y.into_r_as::<i32>()?;           // OK: all values are integral

// These fail (values don't fit)
let z = vec![1_i64 << 40];
let sexp = z.into_r_as::<i32>()?;           // Error: out of range

let w = vec![1.5_f64];
let sexp = w.into_r_as::<i32>()?;           // Error: not integral

// User wants lossy? Cast first.
let lossy: Vec<i32> = vec![1.5_f64, 2.7].iter().map(|&x| x as i32).collect();
let sexp = lossy.into_r();                  // [1, 2] - user's responsibility

Macros§

impl_into_r_as_scalar 🔒
impl_vec_into_r_as 🔒
impl_vec_into_r_as_f64_infallible 🔒

Enums§

CoerceErrorKind 🔒
Internal enum to unify different coerce error types.
StorageCoerceError
Error type for storage-directed conversion failures.

Traits§

IntoRAs
Storage-directed conversion to R SEXP.

Functions§

map_coerce_error 🔒
try_coerce_scalar 🔒
Try to coerce a scalar value, mapping CoerceError to StorageCoerceError.