Skip to main content

Module generic_repr

Module generic_repr 

Source
Expand description

Datatype-generic programming via Sum/Product/Unit encoding.

Maps Rust structs and enums to a universal representation:

  • Structs → Product chains (e.g., Product<A, Product<B, Unit>>)
  • Enums → Sum chains (e.g., Sum<A, Sum<B, Void>>)
  • Empty → Unit or Void

This enables generic algorithms like Diff, Patch, and StableHash that operate on the representation type and work for any type that implements GenericRepr.

§Example

use ftui_core::generic_repr::*;

#[derive(Clone, Debug, PartialEq)]
struct Point { x: f64, y: f64 }

impl GenericRepr for Point {
    type Repr = Product<f64, Product<f64, Unit>>;

    fn into_repr(self) -> Self::Repr {
        Product(self.x, Product(self.y, Unit))
    }

    fn from_repr(repr: Self::Repr) -> Self {
        Point { x: repr.0, y: repr.1.0 }
    }
}

let p = Point { x: 1.0, y: 2.0 };
let repr = p.clone().into_repr();
let back = Point::from_repr(repr);
assert_eq!(p, back);

Structs§

Field
A named field wrapping a value.
Product
A product of two types (struct field pair).
Unit
The empty product (unit type for terminating product chains).
Variant
A named variant wrapping a value.

Enums§

Sum
A sum of two types (enum with two variants).
Void
The empty sum (uninhabited type for terminating sum chains).

Traits§

GenericRepr
Convert between a concrete type and its generic representation.
ProductLen
Count the number of fields in a product chain.
SumLen
Count the number of variants in a sum chain.