6
6
7
7
## Description
8
8
9
- Derive macro to simplify deriving standard and other traits with custom
10
- generic type bounds.
9
+ The ` derive_where ` macro can be used just like std's ` #[derive(...)] `
10
+ statements, with the only caveat that it requires to derive ` DeriveWhere `
11
+ ([ #27 ] ):
11
12
12
13
## Usage
13
14
@@ -17,22 +18,26 @@ generic type bounds.
17
18
struct Example <T >(PhantomData <T >);
18
19
```
19
20
20
- This will generate trait implementations for ` Example ` with any ` T ` ,
21
- compared with std's derives, which would only implement these traits with
21
+ This will generate trait implementations for ` Example ` for any ` T ` ,
22
+ as opposed to std's derives, which would only implement these traits with
22
23
` T: Trait ` bound to the corresponding trait.
23
24
24
25
In addition, the following convenience options are available:
25
26
26
27
### Generic type bounds
27
28
29
+ Separated from the list of traits with a semi-colon, types to bind to can be
30
+ specified. This example will restrict the implementation for ` Example ` to
31
+ ` T: Clone ` :
32
+
28
33
``` rust
29
34
#[derive(DeriveWhere )]
30
35
#[derive_where(Clone ; T )]
31
36
struct Example <T , U >(T , PhantomData <U >);
32
37
```
33
38
34
- Separating the list of trait with a semi-colon, types to bind to can be
35
- specified. This will bind implementation for ` Example ` to ` T: Trait ` .
39
+ It is also possible to specify the bounds to be applied. This will
40
+ bind implementation for ` Example ` to ` T: Super ` :
36
41
37
42
``` rust
38
43
trait Super : Clone {}
@@ -42,8 +47,9 @@ trait Super: Clone {}
42
47
struct Example <T >(PhantomData <T >);
43
48
```
44
49
45
- This will bind implementation for ` Example ` to ` T: Super ` . But more complex
46
- trait bounds are possible:
50
+ But more complex trait bounds are possible as well.
51
+ The example below will restrict the implementation for ` Example ` to
52
+ ` T::Type: Clone ` :
47
53
48
54
``` rust
49
55
trait Trait {
@@ -61,12 +67,9 @@ impl Trait for Impl {
61
67
struct Example <T : Trait >(T :: Type );
62
68
```
63
69
64
- This will bind implementation for ` Example ` to ` T::Type: Super ` . Any
65
- combination of options listed here can be used to satisfy a specific
66
- constrain.
67
-
68
- It is also possible to use two separate constrain specification when
69
- required:
70
+ Any combination of options listed here can be used to satisfy a
71
+ specific constrain. It is also possible to use multiple separate
72
+ constrain specifications when required:
70
73
71
74
``` rust
72
75
#[derive(DeriveWhere )]
@@ -257,3 +260,4 @@ conditions.
257
260
[ `Hash` ] : https://doc.rust-lang.org/core/hash/trait.Hash.html
258
261
[ `Zeroize` ] : https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html
259
262
[ `zeroize` ] : https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html#tymethod.zeroize
263
+ [ #27 ] : https://github.com/ModProg/derive-where/issues/27
0 commit comments