diff --git a/README.md b/README.md index f376e75..2dc0746 100644 --- a/README.md +++ b/README.md @@ -42,11 +42,13 @@ impl MyStruct { } } -let mut mine = MyStruct::builder().a(2).b(3).build(); -assert_eq!(mine.sum, 5); - -mine.more().c(1).d(2).add(); -assert_eq!(mine.sum, 11); +fn main() { + let mut mine = MyStruct::builder().a(2).b(3).build(); + assert_eq!(mine.sum, 5); + + mine.more().c(1).d(2).add(); + assert_eq!(mine.sum, 11); +} ``` ## Derive usage @@ -60,8 +62,10 @@ pub struct MyStruct { simple: usize, } -let mut mine = MyStruct::builder().simple(2).build(); -assert_eq!(mine.simple, 2); +fn main() { + let mut mine = MyStruct::builder().simple(2).build(); + assert_eq!(mine.simple, 2); +} ``` The generated constructor will have private visibility and the builder will match the visibility of the struct. @@ -118,14 +122,16 @@ impl MyStruct { } } -let mine = MyStruct::builder().simple(2).build(); -assert_eq!(mine.simple, 2); +fn main() { + let mine = MyStruct::builder().simple(2).build(); + assert_eq!(mine.simple, 2); -let mine = MyStruct::try_builder().simple(2).build(); -assert_eq!(mine.simple, 2); + let mine = MyStruct::try_builder().simple(2).build(); + assert_eq!(mine.simple, 2); -let mine = MyStruct::random().simple(2).create(); -assert_eq!(mine.simple, 2); + let mine = MyStruct::random().simple(2).create(); + assert_eq!(mine.simple, 2); +} ``` ### Methods @@ -159,13 +165,15 @@ impl MyStruct { } } -MyStruct::default().query().simple("3".to_string()).call(); // self +fn main() { + MyStruct::default().query().simple("3".to_string()).call(); // self -let mine = MyStruct::default(); -mine.query_ref().simple("3".to_string()).stop(); // &self + let mine = MyStruct::default(); + mine.query_ref().simple("3".to_string()).stop(); // &self -let mut mine = MyStruct::default(); -mine.query_ref_mut().simple("3".to_string()).go(); // &mut self + let mut mine = MyStruct::default(); + mine.query_ref_mut().simple("3".to_string()).go(); // &mut self +} ``` ### Optional field @@ -185,12 +193,14 @@ impl MyStruct { } } -let mine = MyStruct::builder().param(2).build(); -assert_eq!(mine.param, 2); -let mine = MyStruct::builder().and_param(Some(2)).build(); -assert_eq!(mine.param, 2); -let mine = MyStruct::builder().build(); -assert_eq!(mine.param, 3); +fn main() { + let mine = MyStruct::builder().param(2).build(); + assert_eq!(mine.param, 2); + let mine = MyStruct::builder().and_param(Some(2)).build(); + assert_eq!(mine.param, 2); + let mine = MyStruct::builder().build(); + assert_eq!(mine.param, 3); +} ``` Note that if a field is an `Option` or collection then if a user forgets to set it a compile error will be generated. @@ -221,8 +231,10 @@ impl MyStruct { } } -let mine = MyStruct::builder().param("Hi").build(); -assert_eq!(mine.param, "Hi"); +fn main() { + let mine = MyStruct::builder().param("Hi").build(); + assert_eq!(mine.param, "Hi"); +} ``` ### Async @@ -267,8 +279,10 @@ impl MyStruct { } } -let mine = MyStruct::builder().param(2).build().unwrap(); -assert_eq!(mine.param, 2); +fn main() { + let mine = MyStruct::builder().param(2).build().unwrap(); + assert_eq!(mine.param, 2); +} ``` ### Collections and maps @@ -289,15 +303,17 @@ impl MyStruct { } } -let mine = MyStruct::builder() - .address("Amsterdam".to_string()) - .address("Fakenham") - .addresses(vec!["Norwich".to_string(), "Bristol".to_string()]) - .build(); -assert_eq!(mine.addresses, vec!["Amsterdam".to_string(), - "Fakenham".to_string(), - "Norwich".to_string(), - "Bristol".to_string()]); +fn main() { + let mine = MyStruct::builder() + .address("Amsterdam".to_string()) + .address("Fakenham") + .addresses(vec!["Norwich".to_string(), "Bristol".to_string()]) + .build(); + assert_eq!(mine.addresses, vec!["Amsterdam".to_string(), + "Fakenham".to_string(), + "Norwich".to_string(), + "Bristol".to_string()]); +} ``` #### Supported types @@ -359,8 +375,10 @@ pub mod foo { } } -let mine = foo::MyStruct::builder().param(2).build(); -assert_eq!(mine.param, 2); +fn main() { + let mine = foo::MyStruct::builder().param(2).build(); + assert_eq!(mine.param, 2); +} ``` diff --git a/src/lib.rs b/src/lib.rs index ba87beb..e691eae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![doc = include_str!("../README.md")] +#![allow(clippy::needless_doctest_main)] extern crate core; use proc_macro::TokenStream;