Typekin
typekin is a proc-macro for defining integer newtypes and enum-backed bitflags
without hand-writing their conversions and operator implementations, and making
sure the type is inlined to plain numbers and erased at runtime.
Use it when a domain value must retain an integer representation but only
interoperate with explicitly approved types. friends = [...] is an allowlist:
it controls generated construction, conversion, comparison, and operators. This
keeps unit, identifier, range, and permission types from being mixed by accident
while keeping their runtime representation unchanged.
#[typekin::integral]generates an integral newtype API.#[typekin::bitflag]generates enum flags plus a separate value type for combined and unknown bit patterns.
Generated implementations use core only. The macro output can be expanded and
retained when a proc-macro dependency is undesirable.
Working examples:
crates/typekin/examples/my_u32.rscrates/typekin/examples/my_u32_non_const.rscrates/typekin/examples/my_flag.rs
AI Disclaimer: The code is handwritten, but the README is AI generated, check
the AI prompts in PROMPTS.md. Some tests are written with the help of AI.
Constness Status
Generated constness is explicit. Set konst = true for nightly-only const
generation, or konst = false for plain implementations that compile on stable
Rust.
#[typekin::integral]
Applied to a single-field tuple struct over a primitive integer and annotated
with #[repr(transparent)], it generates construction, conversion, comparison,
and operator APIs. Interoperation is deny-by-default: add a type to friends
only for operations that are valid for the domain.
Minimal use
;
Friendship is explicit
If a type is not listed in friends, it does not get to construct, compare
with, or operate on your new-type.
;
;
Validation
Use fn_validator when raw values are not always valid.
const
;
Configuration
Generation of individual features can be disabled (check integral's cfg and bitflag's cfg)
;
Friendship levels: Full expands to Make, Rel, Bit, and Math; use an
individual level to narrow access. None grants no generated operations.
#[typekin::bitflag]
Apply bitflag to a unit enum with an integral repr. It generates the enum,
a {Enum}Value type that represents combined or unknown bits, and flag/value
helpers such as name(), items(), from_name(), and contains().
konst is required inside integral = [...]; it controls generated code for
the value type. bitflag has its own friends = [...] list for enum-left
bitwise operations. The enum and generated value type are registered as friends
automatically; arithmetic friendship is not generated.
Example
Unknown bits stay representable
The generated value type is still an integral new-type, so raw bit patterns are kept even when they do not map to a named flag. For this to work, the named flags (i.e. enum variants) are separated from values (value bits).
Const mode
Const generation requires konst = true. Until the relevant const features
stabilize, it remains nightly-only:
;
const START: Counter = of;
const NEXT: Counter = START + 1u32;
Use konst = false to generate plain implementations instead:
;
Development
# regenerate expanded examples
# Or more simply: