Skip to main content

Crate alga2_derive

Crate alga2_derive 

Source
Expand description

#[derive(Alga)] — one line brings a struct into the alga2 tower.

Generates the algebraic chain as component-wise forwarders. The target level is controlled by #[alga(level = "...")] (default "Ring"; the field types must reach it — Rust rejects a level the fields cannot satisfy, which is exactly the check you want):

  • "Monoid" — additive Magma → Monoid, multiplicative Magma → Monoid
  • "Group" — + additive Group/AbelianGroup (and Quasigroup/Loop on the additive side; the multiplicative ladder stops at Monoid)
  • "Ring" (default) — + Semiring → CommutativeRing, plus Module/VectorSpace
  • "Field" — + DivisionRing → Field (field-typed fields only)

Requirements and bounds:

  • Structs only (named, tuple, or generic — including where-clause constraints); enums, unions, unit and empty structs are rejected with a clear error.
  • Every field must reach the target level; mixed field types need a common Module::Scalar (the first field’s scalar is used).
  • No tower imports are needed at the call site: the generated method bodies resolve through the impls they live in.
use alga2::op::Additive;
use alga2::tower::Magma;
use alga2_derive::Alga;

#[derive(Alga)]
#[alga(level = "Field")]
struct Vec2 { x: f64, y: f64 }

let a = Vec2 { x: 1.0, y: 2.0 };
let b = Vec2 { x: 3.0, y: 4.0 };
assert_eq!(<Vec2 as Magma<Additive>>::combine(&a, &b).x, 4.0);

Derive Macros§

Alga
Generates the tower impls for a struct.