c0nst
A procedural macro that enables library authors to write const traits once and automatically provides compatibility for both stable and nightly Rust consumers.
The Problem
Embedded development desperately needs const traits for compile-time computation, and nightly Rust now provides critical const trait support. Many embedded applications are willing to use nightly since they control their compiler toolchain.
However, library authors face a dilemma:
-
On the one hand, if they implement const traits for nightly consumers, their library becomes nightly only and stable consumers are left out.
-
On the other hand, if they don't implement const traits for nightly consumers, then nightly consumers are left out.
-
On the other (third?) hand, if they implement both there is significant code duplication.
The Solution
c0nst bridges this gap by allowing library authors to write const traits
that automatically convert to non-const on stable builds.
Quick Example
Write your trait once with c0nst annotations:
use c0nst;
On stable Rust (default), this works as regular traits:
On nightly Rust (with --features nightly), this becomes:
const
Much more complex scenarios are supported. See the crate documentation.
Usage Guide
1. Library Authors
Set up your library with the nightly feature:
[]
= "0.1"
[]
= ["c0nst/nightly"] # Enable nightly transformations
Write traits once using c0nst syntax:
use c0nst;
// Use conditional const bounds for generic code
>
2. Stable Rust Consumers
Just use your library normally:
[]
= "1.0"
// Works on stable - no const behavior, but same API
let value = default;
3. Nightly Rust Consumers
Enable the nightly feature to get const trait behavior:
[]
= { = "1.0", = ["nightly"] }
// Now works in const contexts!
const VALUE: MyStruct = default;
License
Licensed under the MIT license.