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

#15 in No standard library

Download history 1425/week @ 2023-02-09 1315/week @ 2023-02-16 3685/week @ 2023-02-23 1537/week @ 2023-03-02 2229/week @ 2023-03-09 1827/week @ 2023-03-16 2112/week @ 2023-03-23 2574/week @ 2023-03-30 1818/week @ 2023-04-06 3194/week @ 2023-04-13 2537/week @ 2023-04-20 1809/week @ 2023-04-27 1106/week @ 2023-05-04 1702/week @ 2023-05-11 1503/week @ 2023-05-18 1558/week @ 2023-05-25

6,075 downloads per month
Used in 218 crates (13 directly)

MIT license

10KB
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–420KB