Skip to content

Commit

Permalink
Merge pull request #52 from Stargateur/update-doc
Browse files Browse the repository at this point in the history
update doc for 2018 edition for macro import
  • Loading branch information
nrc authored Dec 3, 2021
2 parents a6ba257 + 3991ff7 commit 4d0a6ab
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 91 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ derive-new = "0.5"

Include the macro:

```rust
#[macro_use]
extern crate derive_new;
```
* Rust Edition 2015

```rust
#[macro_use]
extern crate derive_new;
```

* Rust Edition 2018
```rust
use derive_new::new;
```

Generating constructor for a simple struct:

Expand Down
174 changes: 87 additions & 87 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,110 +1,110 @@
//!# A custom derive implementation for `#[derive(new)]`
//! # 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.
//! 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).
//! Implementation uses macros 1.1 custom derive (which works in stable Rust from
//! 1.15 onwards).
//!
//!## Examples
//! ## Examples
//!
//!Cargo.toml:
//! Cargo.toml:
//!
//!```toml
//![dependencies]
//!derive-new = "0.5"
//!```
//! ```toml
//! [dependencies]
//! derive-new = "0.5"
//! ```
//!
//!Include the macro:
//! Include the macro:
//!
//!```rust
//!#[macro_use]
//!extern crate derive_new;
//!fn main() {}
//!```
//! ```rust
//! use derive_new::new;
//!
//! fn main() {}
//! ```
//!
//!Generating constructor for a simple struct:
//! Generating constructor for a simple struct:
//!
//!```rust
//!#[macro_use]
//!extern crate derive_new;
//!#[derive(new)]
//!struct Bar {
//! a: i32,
//! b: String,
//!}
//! ```rust
//! use derive_new::new;
//!
//! #[derive(new)]
//! struct Bar {
//! a: i32,
//! b: String,
//! }
//!
//!fn main() {
//! let _ = Bar::new(42, "Hello".to_owned());
//!}
//!```
//! fn main() {
//! 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:
//! 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:
//!
//!```rust
//!#[macro_use]
//!extern crate derive_new;
//!#[derive(new)]
//!struct Foo {
//! x: bool,
//! #[new(value = "42")]
//! y: i32,
//! #[new(default)]
//! z: Vec<String>,
//!}
//! ```rust
//! use derive_new::new;
//!
//! #[derive(new)]
//! struct Foo {
//! x: bool,
//! #[new(value = "42")]
//! y: i32,
//! #[new(default)]
//! z: Vec<String>,
//! }
//!
//!fn main() {
//! let _ = Foo::new(true);
//!}
//!```
//! fn main() {
//! 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:
//! Generic types are supported; in particular, `PhantomData<T>` fields will be not
//! included in the argument list and will be intialized automatically:
//!
//!```rust
//!#[macro_use]
//!extern crate derive_new;
//!use std::marker::PhantomData;
//! ```rust
//! use derive_new::new;
//!
//! use std::marker::PhantomData;
//!
//!#[derive(new)]
//!struct Generic<'a, T: Default, P> {
//! x: &'a str,
//! y: PhantomData<P>,
//! #[new(default)]
//! z: T,
//!}
//! #[derive(new)]
//! struct Generic<'a, T: Default, P> {
//! x: &'a str,
//! y: PhantomData<P>,
//! #[new(default)]
//! z: T,
//! }
//!
//!fn main() {
//! let _ = Generic::<i32, u8>::new("Hello");
//!}
//!```
//! fn main() {
//! 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:
//! 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:
//!
//!```rust
//!#[macro_use]
//!extern crate derive_new;
//!#[derive(new)]
//!enum Enum {
//! FirstVariant,
//! SecondVariant(bool, #[new(default)] u8),
//! ThirdVariant { x: i32, #[new(value = "vec![1]")] y: Vec<u8> }
//!}
//! ```rust
//! use derive_new::new;
//!
//! #[derive(new)]
//! enum Enum {
//! FirstVariant,
//! SecondVariant(bool, #[new(default)] u8),
//! ThirdVariant { x: i32, #[new(value = "vec![1]")] y: Vec<u8> }
//! }
//!
//!fn main() {
//! let _ = Enum::new_first_variant();
//! let _ = Enum::new_second_variant(true);
//! let _ = Enum::new_third_variant(42);
//!}
//!```
//! fn main() {
//! let _ = Enum::new_first_variant();
//! let _ = Enum::new_second_variant(true);
//! let _ = Enum::new_third_variant(42);
//! }
//! ```
#![crate_type = "proc-macro"]
#![recursion_limit = "192"]

Expand Down

0 comments on commit 4d0a6ab

Please sign in to comment.