Expand description
Implementation of higher-kinded types in Rust using associated types.
This is a variation of
Edmund Smith’s
method where instead of unplugging and plugging generic arguments between
fully defined types (i.e. Vec<X> <-> Vec<Y>), higher-kinded types are
represented as structs that implement either PlugLifetime or
PlugType. We refer to these structs as HKT forms. To be explicit,
sometimes we refer to types that are not HKT forms as concrete types or
H0 types.
For example, H1Vec, the higher-kinded form of Vec, implements
PlugType. Then, to get the concrete Vec<T> for a type T:
<H1Vec as PlugType<T>>::T§Conventions for HTK Forms
As a convention, all HKT forms must be zero-sized structs, only implement
PlugLifetime or PlugType and respect the following naming convention:
H<n><t>Where <n> is the number of lifetype + type arguments left to be filled,
also referred to as slots, and <t> is the name of the concrete type. For
example, Cow has two HKT forms:
H2Cowwhich implementsPlugLifetime, yieldingH1Cow<'a>H1Cow<'a>which implementsPlugType, yielding a concreteCow<'a, T>
The generic arguments are always filled from left to right, lifetimes
first. In some cases it might be useful to plug those out of order. In
those cases we prepend something descriptive to the type name. See for
example TypedH1Reference.
§HKT-Compatible Concrete Types
The PlugLifetime and PlugType may also be implemented for concrete
types, in which case Type is just itself. This is useful to implement
streaming iterators and similar constructs. H0 is a
type wrapper for exactly this case.
Structs§
- H0
- Type-level wrapper that yields
Tunmodified whenPlugLifetimeorPlugTypeare applied.
Traits§
- Plug
Lifetime - Trait enabling a lifetime to plugged to HKT forms.
- Plug
Type - Trait enabling a type to be plugged to HKT forms.