fp-macros
Procedural macros for the fp-library crate.
This crate provides a suite of macros designed to facilitate working with Higher-Kinded Types (HKT) in Rust. It automates the generation of Kind traits, simplifies their implementation for specific "brand" types, and provides a convenient syntax for type application.
Macros
def_kind!
Defines a new Kind trait based on a Higher-Kinded Type signature.
Syntax: def_kind!((Lifetimes), (Types), (OutputBounds));
- Lifetimes: Comma-separated list of lifetimes (e.g.,
'a). - Types: Comma-separated list of types with optional bounds (e.g.,
T: Display). - OutputBounds:
+-separated list of bounds on the output type (e.g.,Display + Clone).
Example:
use def_kind;
use Display;
// Defines a Kind trait for a signature with:
// - 1 lifetime ('a)
// - 1 type parameter (T) bounded by Display
// - Output type bounded by Debug
def_kind!;
impl_kind!
Simplifies the implementation of a generated Kind trait for a specific brand type. It infers the correct Kind trait to implement based on the signature of the associated type Of.
Syntax:
impl_kind!
Example:
use impl_kind;
;
impl_kind!
Apply!
Applies a brand to type arguments, projecting the brand to its concrete type. This macro is useful for using HKTs in function signatures, struct definitions, and type aliases.
Modes:
- Unified Signature Mode (Recommended): Uses a single
signatureparameter. - Explicit Kind Mode (Advanced): Uses explicit
kind,lifetimes, andtypesparameters.
Optional Parameters:
output: Specifies the name of the associated type to project to. Defaults toOf.
Example (Unified Signature):
use Apply;
// Applies OptionBrand to type String.
type Concrete = Apply!;
// Concrete is Option<String>
// Applies a brand with lifetime and bounds
type ConcreteRef<'a> = Apply!;
Example (Explicit Kind):
type Concrete = Apply!;
Example (Custom Output Type):
type ThreadSafeFn = Apply!;
// Projects to <ArcFnBrand as SendClonableFn>::SendOf<'a, i32, i32>
Kind!
Generates the name of a Kind trait based on its signature. This is primarily used internally by other macros but can be useful when you need to refer to the generated trait name directly (e.g., in bounds where macros aren't allowed).
Example:
// Generates the name for a Kind with 1 type parameter T
let name = Kind!;
Installation
Add this to your Cargo.toml:
[]
= "0.1"
License
BlueOak-1.0.0