Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/idanarye/rust-smart-default

Rust macro for automatically generating default
https://github.com/idanarye/rust-smart-default

Last synced: 17 days ago
JSON representation

Rust macro for automatically generating default

Awesome Lists containing this project

README

        

[![Build Status](https://github.com/idanarye/rust-smart-default/workflows/CI/badge.svg)](https://github.com/idanarye/rust-smart-default/actions)
[![Latest Version](https://img.shields.io/crates/v/smart-default.svg)](https://crates.io/crates/smart-default)
[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://idanarye.github.io/rust-smart-default/)

# Rust SmartDefault

Custom derive for automatically implementing the `Default` trait with customized default values:

```rust
use smart_default::SmartDefault;

#[derive(SmartDefault)]
enum Foo {
Bar,
#[default]
Baz {
#[default = 12]
a: i32,
b: i32,
#[default(Some(Default::default()))]
c: Option,
#[default(_code = "vec![1, 2, 3]")]
d: Vec,
#[default = "four"]
e: String,
},
Qux(i32),
}

assert!(Foo::default() == Foo::Baz {
a: 12,
b: 0,
c: Some(0),
d: vec![1, 2, 3],
e: "four".to_owned(),
});
```

Requires Rust 1.30+ (for non-string values in attributes), and version 1.56
due to 2021 edition.