Skip to main content

Module coerced_scalars

Module coerced_scalars 

Source
Expand description

Coerced scalar conversions (multi-source numeric) and large integer scalars.

The SEXPTYPE literals here are the source of truth (#882). They are runtime match arms on sexp.type_of(), deliberately accepting several source types and coercing into one target Rust type — there is no single T whose T::SEXP_TYPE they could be folded into (the whole point is the 1:N input fan-in). Leave them.

These types accept multiple R source types (INTSXP, REALSXP, RAWSXP, LGLSXP) and coerce to the target Rust type via TryCoerce.

Covers: i8, i16, u16, u32, f32 (sub-native scalars) and i64, u64, isize, usize (large integers via f64 intermediary).

§Tradeoff

This is the looser inbound path. The strict alternative is the bare TryFromSexp impl on the matching R native type (i32, f64, &[i32], …) — those reject any mismatched SEXPTYPE outright instead of coercing. Failure mode of preferring the coerced path when you wanted strict: an R caller silently passes 1.7 (REALSXP) into a Rust i32 argument and gets a truncated 1.

Outbound counterparts for the large-integer types in this module live in crate::into_r::large_integers (lax, default) and crate::strict (#[miniextendr(strict)] opt-in).

Functions§

coerce_real_value 🔒
coerce_value for a double, with R’s NA kept apart from NaN.
coerce_value 🔒
lglsxp_na_error 🔒
NA-rejecting error for a logical (LGLSXP) scalar that holds NA.
numeric_type_error 🔒
The type error of a coerced numeric scalar given something that is not integer, double, logical or raw: worded in R terms by the argument error (got character), like the vector shells’ (from_numeric_vec_with).
try_from_sexp_numeric_option 🔒
try_from_sexp_numeric_option_unchecked 🔒 ⚠
try_from_sexp_numeric_scalar 🔒
try_from_sexp_numeric_scalar_unchecked 🔒 ⚠