Skip to main content

Adapter

Derive Macro Adapter 

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

Derives flense’s Adapter implementations for the annotated fields of a struct.

Annotate each field that should be lensable with #[field(...)]. Each entry is either:

  • Marker - reference an existing Field implementation. The macro emits an unsafe impl Adapter<Marker> for the struct and a compile-time check that the field’s type is exactly <Marker as Field>::Type.
  • new Marker - also mints a fresh marker type Marker (inheriting the struct’s visibility) whose Field::Type is the field’s declared type.
  • new reflexive Marker - as new Marker, and additionally emits an Adapter<Marker> impl for the field type itself (<Marker as Field>::Type at offset zero), so a bare value of that type can be lensed as Marker. You could write this unsafe impl yourself; letting the derive emit it avoids needing to author and maintain a unsafe block.

A single #[field(...)] may list several comma-separated entries, and the attribute may be repeated on one field.

use flense::prelude::*;

enum Position {}
impl Field for Position {
    type Type = [f32; 3];
}

#[derive(Adapter)]
struct Vertex {
    #[field(Position)]
    pos: [f32; 3],
    #[field(new Color)]
    color: [u8; 3],
}

#[derive(Adapter)]
struct Mesh {
    #[field(Position)]
    world_pos: [f32; 3],
    #[field(Color)]
    color: [u8; 3],
}