6 releases (1 stable)

1.0.0 Nov 19, 2021
1.0.0-beta1 Nov 11, 2021
0.3.1 Oct 18, 2021
0.2.0 Sep 9, 2021
0.1.0 Aug 4, 2020

#59 in No standard library

Download history 4375/week @ 2023-10-19 4594/week @ 2023-10-26 4480/week @ 2023-11-02 5124/week @ 2023-11-09 4470/week @ 2023-11-16 3441/week @ 2023-11-23 3623/week @ 2023-11-30 3130/week @ 2023-12-07 3713/week @ 2023-12-14 2526/week @ 2023-12-21 2608/week @ 2023-12-28 3580/week @ 2024-01-04 2947/week @ 2024-01-11 3258/week @ 2024-01-18 2435/week @ 2024-01-25 1841/week @ 2024-02-01

11,071 downloads per month
Used in 233 crates (19 directly)

MIT license

11KB
204 lines

ConstDefault Trait

Crates.io Crates.io docs.rs actions

A Default-like trait and derive macros for const evaluation contexts.

This crate defines the ConstDefault trait and implements it for Rust primitives, prelude types, tuples and arrays. Furthermore it provides a derive macro so that users can implement ConstDefault easily for their custom types.

  • 100% safe Rust
  • no_std compatible
  • Full macro hygiene
  • No dependencies

Usage

Add

[dependencies]
const-default = { version = "1.0", features = ["derive"] }

to your Cargo.toml to start using it.

Examples

Rust Primitives

use const_default::ConstDefault;

fn main() {
    assert_eq!(<i32 as ConstDefault>::DEFAULT, 0);
    assert_eq!(<Option<i32> as ConstDefault>::DEFAULT, None);
    assert_eq!(<String as ConstDefault>::DEFAULT, String::new());
    assert_eq!(<Vec<u8> as ConstDefault>::DEFAULT, Vec::new());
}

Derive

use const_default::ConstDefault;

#[derive(ConstDefault, Debug, Default, PartialEq)]
pub struct Color {
    r: u8,
    g: u8,
    b: u8,
}

fn main() {
    assert_eq!(
        <Color as ConstDefault>::DEFAULT,
        Color::default(),
    );
}

Dependencies

~0–510KB
~11K SLoC