Skip to content

Commit c4e01dc

Browse files
ModProgdaxpedda
andauthored
derive_where attribute macro instead of derive macro (#33)
* Code changes for migrating to attribute_macro * documentation for attribute macro * formatted * Fixing tests * Formatted * Fixed test * Remove empty lines * Apply suggestions from code review Co-authored-by: daxpedda <[email protected]> * Apply suggestions * fix removing item on error * resort imports * Use span of cleaned item Co-authored-by: daxpedda <[email protected]>
1 parent 74dbe2c commit c4e01dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+366
-449
lines changed

README.md

+4-17
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ generic type bounds.
1212
## Usage
1313

1414
The `derive_where` macro can be used just like std's `#[derive(...)]`
15-
statements, with the only caveat that it requires to derive `DeriveWhere`
16-
([#27]):
15+
statements:
1716

1817
```rust
19-
#[derive(DeriveWhere)]
2018
#[derive_where(Clone, Debug)]
2119
struct Example<T>(PhantomData<T>);
2220
```
@@ -34,7 +32,6 @@ specified. This example will restrict the implementation for `Example` to
3432
`T: Clone`:
3533

3634
```rust
37-
#[derive(DeriveWhere)]
3835
#[derive_where(Clone; T)]
3936
struct Example<T, U>(T, PhantomData<U>);
4037
```
@@ -45,7 +42,6 @@ bind implementation for `Example` to `T: Super`:
4542
```rust
4643
trait Super: Clone {}
4744

48-
#[derive(DeriveWhere)]
4945
#[derive_where(Clone; T: Super)]
5046
struct Example<T>(PhantomData<T>);
5147
```
@@ -65,7 +61,6 @@ impl Trait for Impl {
6561
type Type = i32;
6662
}
6763

68-
#[derive(DeriveWhere)]
6964
#[derive_where(Clone; T::Type)]
7065
struct Example<T: Trait>(T::Type);
7166
```
@@ -75,7 +70,6 @@ specific constrain. It is also possible to use multiple separate
7570
constrain specifications when required:
7671

7772
```rust
78-
#[derive(DeriveWhere)]
7973
#[derive_where(Clone; T)]
8074
#[derive_where(Debug; U)]
8175
struct Example<T, U>(PhantomData<T>, PhantomData<U>);
@@ -87,7 +81,6 @@ Deriving [`Default`] on an enum is not possible in Rust at the moment.
8781
Derive-where allows this with a `default` attribute:
8882

8983
```rust
90-
#[derive(DeriveWhere)]
9184
#[derive_where(Default)]
9285
enum Example<T> {
9386
#[derive_where(default)]
@@ -102,7 +95,6 @@ that allow it, which are: [`Debug`], [`Hash`], [`Ord`](https://doc.rust-lang.org
10295
[`PartialEq`](https://doc.rust-lang.org/core/cmp/trait.PartialEq.html), [`Zeroize`] and [`ZeroizeOnDrop`].
10396

10497
```rust
105-
#[derive(DeriveWhere)]
10698
#[derive_where(Debug, PartialEq; T)]
10799
struct Example<T>(#[derive_where(skip)] T);
108100

@@ -113,14 +105,12 @@ assert_eq!(Example(42), Example(0));
113105
It is also possible to skip all fields in an item or variant if desired:
114106

115107
```rust
116-
#[derive(DeriveWhere)]
117108
#[derive_where(Debug)]
118109
#[derive_where(skip_inner)]
119110
struct StructExample<T>(T);
120111

121112
assert_eq!(format!("{:?}", StructExample(42)), "StructExample");
122113

123-
#[derive(DeriveWhere)]
124114
#[derive_where(Debug)]
125115
enum EnumExample<T> {
126116
#[derive_where(skip_inner)]
@@ -134,7 +124,6 @@ Selective skipping of fields for certain traits is also an option, both in
134124
`skip` and `skip_inner`:
135125

136126
```rust
137-
#[derive(DeriveWhere)]
138127
#[derive_where(Debug, PartialEq)]
139128
#[derive_where(skip_inner(Debug))]
140129
struct Example<T>(i32, PhantomData<T>);
@@ -156,7 +145,6 @@ assert_ne!(
156145
This is to avoid ambiguity between another method also called `zeroize`.
157146

158147
```rust
159-
#[derive(DeriveWhere)]
160148
#[derive_where(Zeroize(crate = "zeroize_"))]
161149
struct Example(#[derive_where(Zeroize(fqs))] i32);
162150

@@ -189,8 +177,7 @@ and can be implemented without [`Zeroize`], otherwise it only implements
189177
- `crate`: an item-level option which specifies a path to the `zeroize`
190178
crate in case of a re-export or rename.
191179

192-
```
193-
#[derive(DeriveWhere)]
180+
```rust
194181
#[derive_where(ZeroizeOnDrop(crate = "zeroize_"))]
195182
struct Example(i32);
196183

@@ -253,8 +240,8 @@ accompanied by a minor version bump. If MSRV is important to you, use
253240

254241
[derivative](https://crates.io/crates/derivative)
255242
([![Crates.io](https://img.shields.io/crates/v/derivative.svg)](https://crates.io/crates/derivative))
256-
is a great alternative with many options. Notably it has no `no_std`
257-
support.
243+
is a great alternative with many options. Notably it doesn't support `no_std`
244+
and requires an extra `#[derive(Derivative)]` to use.
258245

259246
## Changelog
260247

ensure-no-std/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ extern crate zeroize_ as zeroize;
55

66
use core::marker::PhantomData;
77

8-
use derive_where::DeriveWhere;
8+
use derive_where::derive_where;
99

10-
#[derive(DeriveWhere)]
1110
#[derive_where(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
1211
#[cfg_attr(feature = "zeroize", derive_where(Zeroize))]
1312
pub struct Test<T>(PhantomData<T>);

non-msrv-tests/tests/ui/default.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
use std::marker::PhantomData;
22

3-
use derive_where::DeriveWhere;
3+
use derive_where::derive_where;
44

5-
#[derive(DeriveWhere)]
65
#[derive_where(Debug)]
76
struct DefaultOnStruct<T>(#[derive_where(default)] PhantomData<T>);
87

9-
#[derive(DeriveWhere)]
108
#[derive_where(Clone)]
119
enum DefaultWithoutTrait<T> {
1210
#[derive_where(default)]
1311
A(PhantomData<T>),
1412
}
1513

16-
#[derive(DeriveWhere)]
1714
#[derive_where(Default)]
1815
enum MissingDefault<T> {
1916
A(PhantomData<T>),
2017
}
2118

22-
#[derive(DeriveWhere)]
2319
#[derive_where(Default)]
2420
enum DuplicateDefaultSeparate<T> {
2521
#[derive_where(default)]
@@ -28,14 +24,12 @@ enum DuplicateDefaultSeparate<T> {
2824
B(PhantomData<T>),
2925
}
3026

31-
#[derive(DeriveWhere)]
3227
#[derive_where(Default)]
3328
enum DuplicateDefaultSame<T> {
3429
#[derive_where(default, default)]
3530
A(PhantomData<T>),
3631
}
3732

38-
#[derive(DeriveWhere)]
3933
#[derive_where(Default)]
4034
enum DuplicateDefaultSameSeparate<T> {
4135
#[derive_where(default)]
+14-15
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
error: unknown option
2-
--> tests/ui/default.rs:7:42
2+
--> tests/ui/default.rs:6:42
33
|
4-
7 | struct DefaultOnStruct<T>(#[derive_where(default)] PhantomData<T>);
4+
6 | struct DefaultOnStruct<T>(#[derive_where(default)] PhantomData<T>);
55
| ^^^^^^^
66

77
error: `default` is only supported if `Default` is being implemented
8-
--> tests/ui/default.rs:12:17
8+
--> tests/ui/default.rs:10:17
99
|
10-
12 | #[derive_where(default)]
10+
10 | #[derive_where(default)]
1111
| ^^^^^^^
1212

1313
error: required `default` option on a variant if `Default` is being implemented
14-
--> tests/ui/default.rs:17:1
14+
--> tests/ui/default.rs:15:1
1515
|
16-
17 | / #[derive_where(Default)]
17-
18 | | enum MissingDefault<T> {
18-
19 | | A(PhantomData<T>),
19-
20 | | }
16+
15 | / enum MissingDefault<T> {
17+
16 | | A(PhantomData<T>),
18+
17 | | }
2019
| |_^
2120

2221
error: multiple `default` options in enum
23-
--> tests/ui/default.rs:27:17
22+
--> tests/ui/default.rs:23:17
2423
|
25-
27 | #[derive_where(default)]
24+
23 | #[derive_where(default)]
2625
| ^^^^^^^
2726

2827
error: duplicate `default` option
29-
--> tests/ui/default.rs:34:26
28+
--> tests/ui/default.rs:29:26
3029
|
31-
34 | #[derive_where(default, default)]
30+
29 | #[derive_where(default, default)]
3231
| ^^^^^^^
3332

3433
error: duplicate `default` option
35-
--> tests/ui/default.rs:42:17
34+
--> tests/ui/default.rs:36:17
3635
|
37-
42 | #[derive_where(default)]
36+
36 | #[derive_where(default)]
3837
| ^^^^^^^

non-msrv-tests/tests/ui/item.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
use std::marker::PhantomData;
22

3-
use derive_where::DeriveWhere;
3+
use derive_where::derive_where;
44

5-
#[derive(DeriveWhere)]
6-
struct NoAttribute<T>(PhantomData<T>);
7-
8-
#[derive(DeriveWhere)]
95
#[derive_where]
106
struct NoOption<T>(PhantomData<T>);
117

12-
#[derive(DeriveWhere)]
138
#[derive_where()]
149
struct EmptyAttribute<T>(PhantomData<T>);
1510

16-
#[derive(DeriveWhere)]
1711
#[derive_where(Clone; T;)]
1812
struct SemiColonAtTheEnd<T, U>(T, PhantomData<U>);
1913

20-
#[derive(DeriveWhere)]
2114
#[derive_where(Clone; T,,)]
2215
struct DoubleColonAtTheEnd<T, U>(T, PhantomData<U>);
2316

24-
#[derive(derive_where::DeriveWhere)]
2517
#[derive_where(Clone; where)]
2618
struct InvalidGeneric<T>(PhantomData<T>);
2719

28-
#[derive(derive_where::DeriveWhere)]
2920
#[derive_where(Clone Debug)]
3021
struct MissingCommaBetweenTraits<T>(PhantomData<T>);
3122

32-
#[derive(derive_where::DeriveWhere)]
3323
#[derive_where(Clone; T U)]
3424
struct MissingCommaBetweenGenerics<T, U, V>(T, PhantomData<(U, V)>);
3525

36-
#[derive(DeriveWhere)]
3726
#[derive_where("Clone")]
3827
struct InvalidTrait<T>(PhantomData<T>);
3928

non-msrv-tests/tests/ui/item.stderr

+23-25
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,51 @@
1-
error: no traits found to implement, use `#[derive_where(..)` to specify some
2-
--> tests/ui/item.rs:6:1
1+
error: empty `derive_where` found
2+
--> tests/ui/item.rs:5:1
33
|
4-
6 | struct NoAttribute<T>(PhantomData<T>);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
7-
error: unexpected option syntax
8-
--> tests/ui/item.rs:9:3
4+
5 | #[derive_where]
5+
| ^^^^^^^^^^^^^^^
96
|
10-
9 | #[derive_where]
11-
| ^^^^^^^^^^^^
7+
= note: this error originates in the attribute macro `derive_where` (in Nightly builds, run with -Z macro-backtrace for more info)
128

139
error: empty `derive_where` found
14-
--> tests/ui/item.rs:13:3
15-
|
16-
13 | #[derive_where()]
17-
| ^^^^^^^^^^^^^^
10+
--> tests/ui/item.rs:8:1
11+
|
12+
8 | #[derive_where()]
13+
| ^^^^^^^^^^^^^^^^^
14+
|
15+
= note: this error originates in the attribute macro `derive_where` (in Nightly builds, run with -Z macro-backtrace for more info)
1816

1917
error: expected `,`
20-
--> tests/ui/item.rs:17:24
18+
--> tests/ui/item.rs:11:24
2119
|
22-
17 | #[derive_where(Clone; T;)]
20+
11 | #[derive_where(Clone; T;)]
2321
| ^
2422

2523
error: expected type to bind to, expected one of: `for`, parentheses, `fn`, `unsafe`, `extern`, identifier, `::`, `<`, square brackets, `*`, `&`, `!`, `impl`, `_`, lifetime
26-
--> tests/ui/item.rs:21:25
24+
--> tests/ui/item.rs:14:25
2725
|
28-
21 | #[derive_where(Clone; T,,)]
26+
14 | #[derive_where(Clone; T,,)]
2927
| ^
3028

3129
error: expected type to bind to, expected one of: `for`, parentheses, `fn`, `unsafe`, `extern`, identifier, `::`, `<`, square brackets, `*`, `&`, `!`, `impl`, `_`, lifetime
32-
--> tests/ui/item.rs:25:23
30+
--> tests/ui/item.rs:17:23
3331
|
34-
25 | #[derive_where(Clone; where)]
32+
17 | #[derive_where(Clone; where)]
3533
| ^^^^^
3634

3735
error: expected `;` or `,
38-
--> tests/ui/item.rs:29:22
36+
--> tests/ui/item.rs:20:22
3937
|
40-
29 | #[derive_where(Clone Debug)]
38+
20 | #[derive_where(Clone Debug)]
4139
| ^^^^^
4240

4341
error: expected `,`
44-
--> tests/ui/item.rs:33:25
42+
--> tests/ui/item.rs:23:25
4543
|
46-
33 | #[derive_where(Clone; T U)]
44+
23 | #[derive_where(Clone; T U)]
4745
| ^
4846

4947
error: unexpected option syntax
50-
--> tests/ui/item.rs:37:16
48+
--> tests/ui/item.rs:26:16
5149
|
52-
37 | #[derive_where("Clone")]
50+
26 | #[derive_where("Clone")]
5351
| ^^^^^^^

non-msrv-tests/tests/ui/item_option_syntax.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
use std::marker::PhantomData;
22

3-
use derive_where::DeriveWhere;
3+
use derive_where::derive_where;
44

5-
#[derive(DeriveWhere)]
6-
// Rust itself already fails to parse this and will provide a separate error message.
7-
#[derive_where = invalid]
8-
struct InvalidAttribute<T>(PhantomData<T>);
9-
10-
#[derive(DeriveWhere)]
115
#[derive_where = "invalid"]
126
struct WrongAttributeSyntax<T>(PhantomData<T>);
137

14-
#[derive(DeriveWhere)]
158
#[derive_where()]
169
struct EmptyAttribute<T>(PhantomData<T>);
1710

18-
#[derive(DeriveWhere)]
1911
#[derive_where(Debug = "option")]
2012
struct WrongOptionSyntax<T>(PhantomData<T>);
2113

22-
#[derive(DeriveWhere)]
2314
#[derive_where(Debug())]
2415
struct EmptyOption<T>(PhantomData<T>);
2516

26-
#[derive(derive_where::DeriveWhere)]
2717
#[derive_where(Debug(option))]
2818
struct UnsupportedOption<T>(PhantomData<T>);
2919

0 commit comments

Comments
 (0)