Skip to main content

AltrepInteger

Derive Macro AltrepInteger 

Source
#[derive(AltrepInteger)]
{
    // Attributes available to this derive:
    #[altrep]
}
Expand description

Derive macro for ALTREP integer vector data types.

Auto-implements AltrepLen, AltIntegerData, and calls impl_altinteger_from_data!.

§Attributes

  • #[altrep(len = "field_name")] - Specify length field (auto-detects “len” or “length”)
  • #[altrep(elt = "field_name")] - For constant vectors, specify which field provides elements
  • #[altrep(dataptr)] - Pass dataptr option to low-level macro
  • #[altrep(serialize)] - Pass serialize option to low-level macro
  • #[altrep(subset)] - Pass subset option to low-level macro
  • #[altrep(no_lowlevel)] - Skip automatic impl_altinteger_from_data! call

§Example (Constant Vector - Zero Boilerplate!)

#[derive(ExternalPtr, AltrepInteger)]
#[altrep(elt = "value")]  // All elements return this field
pub struct ConstantIntData {
    value: i32,
    len: usize,
}

// That's it! 3 lines instead of 30!
// AltrepLen, AltIntegerData, and low-level impls are auto-generated

#[miniextendr(class = "ConstantInt")]
pub struct ConstantIntClass(pub ConstantIntData);

§Example (Custom elt() - Override One Method)

#[derive(ExternalPtr, AltrepInteger)]
pub struct ArithSeqData {
    start: i32,
    step: i32,
    len: usize,
}

// Auto-generates AltrepLen and stub AltIntegerData
// Just override elt() for custom logic:
impl AltIntegerData for ArithSeqData {
    fn elt(&self, i: usize) -> i32 {
        self.start + (i as i32) * self.step
    }
}