| Crates.io | c0nst |
| lib.rs | c0nst |
| version | 0.2.1 |
| created_at | 2025-08-11 16:24:34.249105+00 |
| updated_at | 2025-08-25 01:27:40.459696+00 |
| description | proc-macro for sharing code between const and non-const traits |
| homepage | https://github.com/npmccallum/c0nst |
| repository | https://github.com/npmccallum/c0nst |
| max_upload_size | |
| id | 1790403 |
| size | 41,147 |
Write const trait code once, run on both nightly and stable Rust.
#![cfg_attr(feature = "nightly", feature(const_trait_impl))]
c0nst::c0nst! {
pub c0nst trait Default {
fn default() -> Self;
}
impl c0nst Default for () {
fn default() -> Self {}
}
pub c0nst fn default<T: [c0nst] Default>() -> T {
T::default()
}
}
[dependencies]
c0nst = "0.2"
[features]
nightly = ["c0nst/nightly"]
Replace const with c0nst - the macro transforms your code based on feature
flags:
nightly feature: c0nst → const (modern const trait syntax)nightly feature: c0nst and [c0nst] are removed (stable
compatibility)Perfect for library authors - write once, let users choose between nightly const traits or stable compatibility.
s/c0nst/const/g)Destruct trait referencesWrite const-optional traits that work for everyone! First, expose the choice to your library users:
# Cargo.toml
[features]
nightly = ["c0nst/nightly"]
Then, define and implement const traits using the nightly syntax (with the
c0nst variation):
// src/lib.rs
#![cfg_attr(feature = "nightly", feature(const_trait_impl))]
c0nst::c0nst! {
pub c0nst trait Compute {
fn calculate(&self) -> u32;
}
impl c0nst Compute for u32 {
fn calculate(&self) -> u32 { *self * 2 }
}
}
If you want to run on stable rust, use the library like normal. First, add the dependency:
# Cargo.toml
[dependencies]
my-lib = "1.0"
Then use the dependency.
// src/main.rs
let value: u32 = 42u32.calculate(); // ✅ Runtime
That's it. You can compile on stable rust and get runtime behavior.
On the other hand, if you want compile-time behavior and are willing to accept
the requirement to compile only on nightly, then just use the nightly feature:
# Cargo.toml
[dependencies]
my-lib = { version = "1.0", features = ["nightly"] }
Then, you get compile-time behavior:
// src/main.rs
#![feature(const_trait_impl)]
const VALUE: u32 = 42u32.calculate(); // ✅ Compile-time