-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Macros one dot one (Do Not Merge) #28
Changes from all commits
1f43bfb
4738aa0
dd39d21
b9597eb
b5b419a
d616575
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,17 @@ version = "0.2.1" | |
authors = ["Colin Kiegel <[email protected]>", | ||
"Pascal Hertleif <[email protected]>"] | ||
|
||
description = "Rust macro based on custom_derive to automatically implement the builder pattern for arbitrary structs." | ||
description = "Rust macro to automatically implement the builder pattern for arbitrary structs." | ||
repository = "https://github.com/colin-kiegel/rust-derive-builder" | ||
documentation = "https://docs.rs/derive_builder" | ||
|
||
license = "MIT/Apache-2.0" | ||
keywords = ["derive", "macro", "builder", "setter", "struct"] | ||
readme = "README.md" | ||
|
||
[dev-dependencies] | ||
custom_derive = "0.1.5" | ||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = "0.8" | ||
quote = "0.2" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,22 @@ | |
|
||
# Builder pattern derive | ||
|
||
[Rust][rust] macro (based on [custom_derive][custom_derive]) to automatically implement the **builder pattern** for arbitrary structs. A simple `#[derive(Builder)]` will generate code of public setter-methods for all struct fields. | ||
[Rust][rust] macro to automatically implement the **builder pattern** for arbitrary structs. A simple `#[derive(Builder)]` will generate code of public setter-methods for all struct fields. | ||
|
||
**This is a work in progress.** Use it at your own risk. | ||
**This is a work in progress.** Use it at your own risk. | ||
**This currently requires Rust nightly, due to the usage of Macros 1.1** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please mention which nightly you think works; something like "requires Rust nightly (2016-10-09 or newer)" is good. Also: Period at the end. |
||
|
||
And this is how it works: | ||
|
||
```rust | ||
#[macro_use] extern crate custom_derive; | ||
#![feature(proc_macro)] | ||
#[macro_use] extern crate derive_builder; | ||
|
||
custom_derive! { | ||
#[derive(Default, Builder)] | ||
struct Channel { | ||
token: i32, | ||
special_info: i32, | ||
// .. a whole bunch of other fields .. | ||
} | ||
#[derive(Default, Builder)] | ||
struct Channel { | ||
token: i32, | ||
special_info: i32, | ||
// .. a whole bunch of other fields .. | ||
} | ||
|
||
impl Channel { | ||
|
@@ -38,7 +37,7 @@ fn main() { | |
} | ||
``` | ||
|
||
Note that we did not write any implementation of a method called `special_info`. Instead the [`custom_derive!`][custom_derive] macro scans the `#[derive(..)]` attribute of the struct for non-std identifiers – in our case `#[derive(Builder)]` – and delegates the code generation to the `Builder!` macro defined in this crate. | ||
Note that we did not write any implementation of a method called `special_info`. Instead the `derive_builder` crate acts on a `#[derive(Builder)]` and generates the necessary code at compile time. | ||
|
||
The automatically generated setter method for the `special_info` field will look like this: | ||
|
||
|
@@ -70,7 +69,6 @@ The builder pattern is explained [here][builder-pattern], including its variants | |
|
||
[doc]: https://colin-kiegel.github.io/rust-derive-builder | ||
[rust]: https://www.rust-lang.org/ | ||
[custom_derive]: https://crates.io/crates/custom_derive | ||
[builder-pattern]: https://aturon.github.io/ownership/builders.html | ||
[into]: https://doc.rust-lang.org/nightly/std/convert/trait.Into.html | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was fun while it lasted 😢 |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "derive_builder_test" | ||
version = "0.1.0" | ||
|
||
description = "Test derive_builder" | ||
|
||
[dependencies] | ||
derive_builder = { path = "../" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![feature(proc_macro)] | ||
#[macro_use] extern crate derive_builder; | ||
|
||
/// This is a doc comment for the struct | ||
#[warn(missing_docs)] | ||
#[allow(non_snake_case, dead_code)] | ||
#[derive(Builder)] | ||
struct Lorem { | ||
/// This is a doc comment for a field | ||
field_with_doc_comment: String, | ||
#[allow(missing_docs)] | ||
undocumented: String, | ||
#[allow(non_snake_case)] | ||
CamelCase: i32, | ||
#[cfg(target_os = "macos")] | ||
mac_only: bool, | ||
#[allow(non_snake_case)] | ||
#[cfg(target_os = "linux")] | ||
LinuxOnly: (), | ||
} | ||
|
||
#[test] | ||
fn annotations() { | ||
// this is currently just a compile-test (may switch to token comparisons here) | ||
// https://github.com/colin-kiegel/rust-derive-builder/issues/19 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still valid? Is there a test story for nicely comparing syn tokens? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![feature(proc_macro)] | ||
#[macro_use] extern crate derive_builder; | ||
|
||
/// This is a doc comment for the struct | ||
#[warn(missing_docs)] | ||
#[allow(non_snake_case)] | ||
#[derive(Debug, PartialEq, Default, Builder)] | ||
struct IgnoreEmptyStruct { } | ||
|
||
#[test] | ||
fn empty_struct() { | ||
// this is just a compile-test - no run time checks required. | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this gets merged you should be listed here, @badboy!