c0nst

Crates.ioc0nst
lib.rsc0nst
version0.2.1
created_at2025-08-11 16:24:34.249105+00
updated_at2025-08-25 01:27:40.459696+00
descriptionproc-macro for sharing code between const and non-const traits
homepagehttps://github.com/npmccallum/c0nst
repositoryhttps://github.com/npmccallum/c0nst
max_upload_size
id1790403
size41,147
Nathaniel McCallum (npmccallum)

documentation

https://docs.rs/c0nst

README

c0nst

Crates.io Documentation Build Status License: MIT Rust 1.63+

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()
    }
}

Installation

[dependencies]
c0nst = "0.2"

[features]
nightly = ["c0nst/nightly"]

How it works

Replace const with c0nst - the macro transforms your code based on feature flags:

  • With nightly feature: c0nstconst (modern const trait syntax)
  • Without 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.

Features

  • Zero-cost - Simple keyword replacement, no runtime overhead
  • Forward compatible - Easy migration when const traits stabilize (s/c0nst/const/g)
  • Lightweight - Single proc-macro, minimal dependencies
  • Comprehensive - Handles all syntax from the RFC
  • Destruct support - Automatically resolves Destruct trait references

For Library Authors

Write 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 }
    }
}

For Library Users

Runtime (Stable)

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.

Compile-time (Nightly)

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
Commit count: 24

cargo fmt