Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
bryn committed Feb 13, 2025
1 parent c2d0e00 commit 27dcce1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 39 deletions.
96 changes: 57 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
```


Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![doc = include_str!("../README.md")]
#![allow(clippy::needless_doctest_main)]
extern crate core;

use proc_macro::TokenStream;
Expand Down

0 comments on commit 27dcce1

Please sign in to comment.