Skip to main content

Module altrep_derive

Module altrep_derive 

Source
Expand description

Derive macros for ALTREP data traits.

These macros auto-implement AltrepLen and Alt*Data traits for simple field-based ALTREP types, reducing boilerplate for users.

§Two paths: field-based vs manual

#[derive(AltrepInteger)] (and the Real / Logical / Raw / String / Complex / List variants) drive one of two code paths:

  1. Field-based (default) — point the derive at struct fields that hold the vector length and a constant element value:

    #[derive(AltrepInteger)]
    #[altrep(len = "n", elt = "value", class = "Repeat")]
    struct Repeat { n: usize, value: i32 }

    The derive emits AltrepLen, AltIntegerData, Altrep, AltVec, RegisterAltrep, and the R_make_altinteger_class registration. No extra trait impls required.

  2. Manual#[altrep(manual)] suppresses AltrepLen / AltIntegerData generation so you can write the element-access logic yourself. The low-level trait impls (Altrep, AltVec, family methods, InferBase) are still emitted — #[altrep(no_lowlevel)] is the additional escape hatch when you also want to control Altrep / AltVec directly.

Pick by what your data looks like: a finite buffer that fits in a struct field → field-based; a computed element (fibonacci(i)) → manual.

§Guard mode attribute

The derive recognises #[altrep(unsafe)] / #[altrep(rust_unwind)] / #[altrep(r_unwind)] on the struct, which sets the runtime Altrep::GUARD const that the trampolines in altrep_bridge dispatch on. Default is r_unwind (safe for callbacks that call R APIs).

§Derive validation rules

Several #[altrep(...)] options conflict; the derive rejects illegal combinations at compile time:

OptionAllowed on familiesConflicts with
subsetall atomic (integer, real, logical, raw, string, complex)dataptr, list
dataptrinteger, real, logical, raw, string, complexsubset, list
serializeall(none)
manualall(none — turns off AltrepLen/Alt*Data generation)
no_lowlevelall(none — also suppresses the low-level trait impls)

The list family rejects dataptr and subset — list elements are arbitrary SEXPs, so there is no contiguous buffer to expose.

Structs§

AltrepAttrs 🔒
Parsed #[altrep(...)] attributes controlling ALTREP derive code generation.
AltrepFamilyConfig 🔒
Per-family configuration controlling low-level code generation for an ALTREP type family.

Functions§

derive_altrep_complex
Derive macro entry point for AltrepComplex.
derive_altrep_generic 🔒
Shared implementation for all non-list ALTREP derive macros.
derive_altrep_integer
Derive macro entry point for AltrepInteger.
derive_altrep_list
Derive macro entry point for AltrepList.
derive_altrep_logical
Derive macro entry point for AltrepLogical.
derive_altrep_raw
Derive macro entry point for AltrepRaw.
derive_altrep_real
Derive macro entry point for AltrepReal.
derive_altrep_string
Derive macro entry point for AltrepString.
generate_altrep_len 🔒
Generates an impl AltrepLen for T block that delegates to a named struct field.