Skip to main content

Module convert

Module convert 

Source
Expand description

Wrapper helpers to force specific IntoR representations.

This module provides two approaches for controlling how Rust types are converted to R:

§1. As* Wrappers (Call-site Control)

Use these wrappers when you want to override the conversion for a single return value:

#[miniextendr]
fn get_data() -> AsList<MyStruct> {
    AsList(MyStruct { x: 1, y: 2 })
}

§2. Prefer* Derive Macros (Type-level Control)

Use these derives when a type should always use a specific conversion:

  • #[derive(IntoList, PreferList)]: Type always converts to R list
  • #[derive(ExternalPtr, PreferExternalPtr)]: Type always converts to external pointer
  • #[derive(RNativeType, PreferRNativeType)]: Newtype always converts to native R scalar
#[derive(IntoList, PreferList)]
struct Point { x: f64, y: f64 }

#[miniextendr]
fn make_point() -> Point {  // Automatically becomes R list
    Point { x: 1.0, y: 2.0 }
}

§3. #[miniextendr(return = "...")] Attribute

Use this when you want to control conversion for a specific #[miniextendr] function without modifying the type itself:

  • return = "list": Wrap result in AsList
  • return = "externalptr": Wrap result in AsExternalPtr
  • return = "native": Wrap result in AsRNative
#[miniextendr(return = "list")]
fn get_as_list() -> MyStruct {
    MyStruct { x: 1 }
}

§Choosing the Right Approach

SituationRecommended Approach
Type should always convert one wayPrefer* derive
Override conversion for one functionAs* wrapper or return attribute
Type has multiple valid representationsDon’t use Prefer*; use As* or return

Structs§

AsDisplay
Wrap a T: Display and convert it to an R character scalar.
AsDisplayVec
Wrap a Vec<T: Display> and convert it to an R character vector.
AsExternalPtr
Wrap a value and convert it to an R external pointer when returned from Rust.
AsFromStr
Wrap a parsed T: FromStr from an R character scalar.
AsFromStrVec
Wrap a Vec<T: FromStr> parsed from an R character vector.
AsList
Wrap a value and convert it to an R list via IntoList when returned from Rust.
AsNamedList
Wrap a tuple pair collection and convert it to a named R list (VECSXP).
AsNamedVector
Wrap a tuple pair collection and convert it to a named atomic R vector (INTSXP, REALSXP, LGLSXP, RAWSXP, or STRSXP).
AsRNative
Wrap a scalar RNativeType and force native R vector conversion.
Collect
Write an ExactSizeIterator of native R types directly into an R vector.
CollectNA
Write an ExactSizeIterator of Option<T> directly into an R vector with NA support.
CollectNAInt
Write an ExactSizeIterator of Option<i32> directly into an R integer vector with NA.
CollectStrings
Write an ExactSizeIterator of String directly into an R character vector.
DataFrame
Convert row-oriented data into a column-oriented R data.frame.
ToDataFrame
Wrap a value and convert it to an R data.frame via IntoDataFrame when returned from Rust.

Traits§

AsExternalPtrExt
Extension trait for wrapping values as AsExternalPtr.
AsListExt
Extension trait for wrapping values as AsList.
AsNamedListExt
Extension trait for wrapping tuple pair collections as AsNamedList.
AsNamedVectorExt
Extension trait for wrapping tuple pair collections as AsNamedVector.
AsRNativeExt
Extension trait for wrapping values as AsRNative.
IntoDataFrame
Trait for types that can be converted into R data frames.
ToDataFrameExt
Extension trait for wrapping values as ToDataFrame.

Functions§

named_vector_from_pairs 🔒
Shared helper: build a named atomic vector from an owning iterator of (key, value) pairs.