const-default

Crates.ioconst-default
lib.rsconst-default
version1.0.0
sourcesrc
created_at2020-08-04 19:32:11.81878
updated_at2021-11-19 20:35:25.325618
descriptionA const Default trait
homepage
repositoryhttps://github.com/AerialX/const-default.rs
max_upload_size
id273001
size21,462
Aaron Lindsay (AerialX)

documentation

https://docs.rs/const-default

README

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(),
    );
}
Commit count: 45

cargo fmt