• Stars
    star
    525
  • Rank 84,404 (Top 2 %)
  • Language
    Rust
  • License
    MIT License
  • Created about 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

derive simple constructor functions for Rust structs

A custom derive implementation for #[derive(new)]

A derive(new) attribute creates a new constructor function for the annotated type. That function takes an argument for each field in the type giving a trivial constructor. This is useful since as your type evolves you can make the constructor non-trivial (and add or remove fields) without changing client code (i.e., without breaking backwards compatibility). It is also the most succinct way to initialise a struct or an enum.

Implementation uses macros 1.1 custom derive (which works in stable Rust from 1.15 onwards).

#[no_std] is fully supported if you switch off the default feature "std".

Examples

Cargo.toml:

[dependencies]
derive-new = "0.5"

Include the macro:

  • Rust Edition 2015

    #[macro_use]
    extern crate derive_new;
  • Rust Edition 2018

    use derive_new::new;

Generating constructor for a simple struct:

#[derive(new)]
struct Bar {
    a: i32,
    b: String,
}

let _ = Bar::new(42, "Hello".to_owned());

Default values can be specified either via #[new(default)] attribute which removes the argument from the constructor and populates the field with Default::default(), or via #[new(value = "..")] which initializes the field with a given expression:

#[derive(new)]
struct Foo {
    x: bool,
    #[new(value = "42")]
    y: i32,
    #[new(default)]
    z: Vec<String>,
}

let _ = Foo::new(true);

Generic types are supported; in particular, PhantomData<T> fields will be not included in the argument list and will be intialized automatically:

use std::marker::PhantomData;

#[derive(new)]
struct Generic<'a, T: Default, P> {
    x: &'a str,
    y: PhantomData<P>,
    #[new(default)]
    z: T,
}

let _ = Generic::<i32, u8>::new("Hello");

For enums, one constructor method is generated for each variant, with the type name being converted to snake case; otherwise, all features supported for structs work for enum variants as well:

#[derive(new)]
struct Enum {
    FirstVariant,
    SecondVariant(bool, #[new(default)] u8),
    ThirdVariant { x: i32, #[new(value = "vec![1]")] y: Vec<u8> }
}

let _ = Enum::new_first_variant();
let _ = Enum::new_second_variant(true);
let _ = Enum::new_third_variant(42);

More Repositories

1

r4cppp

Rust for C++ programmers
Rust
3,348
star
2

libhoare

Design by contract style assertions for Rust
Rust
247
star
3

graphql

A Rust GraphQL server framework
Rust
236
star
4

xmas-elf

elf parser and navigation tool, pure Rust
Rust
157
star
5

rustaceans.org

Backing data for
150
star
6

ezio

Easy IO for Rust
Rust
104
star
7

portable-interoperable

Async fundamentals initiative: portable and interoperable
75
star
8

apr-intro

An alternate introdcution to the APR book
60
star
9

talks

Slides and artifacts for talks
58
star
10

stupid-stats

Tutorial and demo of rust compiler replacement tooling
Rust
54
star
11

proc-macro-rules

Macro-rules-style syntax matching for procedural macros
Rust
51
star
12

error-docs

Documentation of Rust error handling
48
star
13

zero

A Rust library for zero-allocation parsing of binary data.
Rust
47
star
14

darkly

Rust
40
star
15

callgraph.rs

Callgraphs for Rust programs
Rust
32
star
16

rfc-index

A curated index of Rust RFCs
Rust
27
star
17

find-work

find something Rusty to work on
Rust
20
star
18

big-book-ffi

The Big Book of Rust Interop
20
star
19

provide-any

Proposed API for type-driven member access
Rust
12
star
20

leb128

Implementation of LEB128 encoding in Rust
Rust
9
star
21

mentor-rfcs

A place to improve your RFC writing skills and collaborate on writing RFCs
9
star
22

async-io-traits

Async versions of io traits
Rust
8
star
23

tikv-bench

Rust
7
star
24

rust-dxr

Rust indexing in DXR
7
star
25

rustdoc-highlight

A Rust syntax highlighting library
Rust
5
star
26

box-error

A library for error handling using boxed errors
Rust
4
star
27

clyde

wip
Rust
3
star
28

grpc-snoop

A tool to capture TiKV gRPC messages
Go
3
star
29

gh-velocity

measure the velocity of PRs landing in GitHub
Rust
3
star
30

survey-processing

Utility code for processing Rust's annual survey
Rust
3
star
31

N

PL Semantics Tool
Python
3
star
32

parcom

Rust
2
star
33

tmit

This Month in TiKV
2
star
34

owned-buf

An owned buffer type for reading into possibly uninitialized memory
Rust
2
star
35

github-issues-import

Fork of IQAndreas/github-issues-import
Python
2
star
36

triage

Scripts for helping with Rust issue triage
JavaScript
2
star
37

rustc-perf-data

1
star
38

grpc-benchmark

Shell
1
star
39

macro-libs

Design a proc macro library for Rust
1
star
40

collections

having some fun implementing basic data structures in Rust
Rust
1
star
41

fmtfmt

very WIP ideas for a generic formatting tool
Rust
1
star
42

read-buf

Very sketchy experimentation with ReadBuf things
Rust
1
star
43

raft-proto

Rust
1
star
44

ios-apnea

experiments in ios apps (an apnea timer)
Swift
1
star
45

ray

Rust and JS ray tracers
Rust
1
star
46

derive-display-rfc

repo for collaborating on an RFC for derive display
1
star
47

grpc-rs-squashed

grpc-rs without any Git history
Rust
1
star