Typekin
typekin is a proc-macro crate for generating following no_std code:
#[typekin::integral]: integer new-types with explicit interoperability#[typekin::bitflag]: enum-backed flags plus a generated value type (modelled after pkg:cargo/bitflags@2.13.1)
The generated code will always be zero-dependency and is fully no_std, and
everything is const by default. The dependency on typekin proc-macro itself
can be also dropped via expanding the generated code (cargo-expand) and
reformatting the output to a readable form via the main.rs in this macro.
The important rule is simple: The type only interoperates with the types listed
in friends = [...].
This repository already contains some 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 at the end.
Constness Status
By default, the generated code is nightly-only due to const features, but
the const impls can be disabled with the proc-macro attribute, which makes the
code valid in stable rust without any unstable features:
without = [konst]
#[typekin::integral]
Applied to a single-field tuple struct over an integral primitive (and annotated
with #[repr(transparent)]) turns into a bitflag very similar to
how https://crates.io/crates/bitflags works, albeit separating individual flags
as enum and keeping their combined values in a dedicated type.
Demo
)]
;
;
Some supported operations:
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
;
Attribute shape
The syntax you will most likely need looks like this:
;
Friend levels used by the macro:
- Full: Shorthand for Make + Rel + Bit + Math
- Rel
- Bit
- Math
- Make
- None
- AnythingElse: Not used by Typekin, but available at type level for custom logic
TODO: prevent feature clash, either use Custom(Anything) or prefix it with Custom
#[typekin::bitflag]
It is modeled (and mostly copied) from pkg:cargo/bitflags@2.13.1, but with some
different design decisions. Ihe macro is applied to a unit enum with an integral
repr. The macro generates:
- the enum you wrote
- a value type, by default named as
<EnumName>Value - flag/value helpers such as
name(),items(),from\_name(),contains(),iter()
The enum is a bitwise and relational friend of its generated value type. Mixed
bitwise operations (Perm::Read | value, value | Perm::Read) return
PermValue; a combined bit pattern is never coerced back into the enum.
Arithmetic friendship is intentionally not generated.
bitflag also accepts its own friends = [...] list, independent from
integral = [friends = ...]. These friends are available to enum-left bitwise
operations, so Perm::Read & friend returns PermValue. The generated enum
and value are registered automatically.
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 is on by default. Eventually when rust stabilizes all const features, the generated code will work on stable rust out of the box, but until then, enabling these features (as of writing) are required:
;
const START: Counter = of;
const NEXT: Counter = START + 1u32;
But const impl can be skipped in favor of plain implementations, by disabling
the const flag:
;
Development
# regenerate expanded examples
Development
2026-08-01: Read this project and create comprehensive readme. This is crate for
the new-type pattern, borrowing the concept of `friend classes` from c++.
---
2026-08-09: This project has evolved, rewrite the readme, and do not make it
fluff or an ad. Just say what's important, and more importantly try to
demonstrate with code example rather than text.
NOTE: this version is manually modified, might have typos.