Skip to content

Commit 0152e9a

Browse files
committed
Improve documentation on derived trait bounds
The goal of this change is to make it easier to see how the bounds differ from the standard library's derives.
1 parent bc05181 commit 0152e9a

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/lib.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ enum Enum<T> {
179179
180180
###### Generic Parameters Bound to the `Debug` Trait or Others
181181
182-
Generic parameters will be automatically bound to the `Debug` trait if necessary.
182+
Unlike the trait derives provided by the standard library,
183+
the types associated with each field or enum variant will be automatically bound to the `Debug` trait if necessary.
183184
184185
```rust
185186
# #[cfg(feature = "Debug")]
@@ -194,12 +195,14 @@ enum Enum<T, K> {
194195
f1: K,
195196
},
196197
V3(
197-
T
198+
Option<T>,
198199
),
199200
}
200201
# }
201202
```
202203
204+
Here the derived bounds are `K: Debug, Option<T>: Debug`.
205+
203206
Or you can set the where predicates by yourself.
204207
205208
```rust
@@ -214,21 +217,21 @@ fn fmt<D>(_s: &D, f: &mut Formatter<'_>) -> fmt::Result {
214217
}
215218
216219
#[derive(Educe)]
217-
#[educe(Debug(bound(T: std::fmt::Debug)))]
220+
#[educe(Debug(bound(Option<T>: std::fmt::Debug)))]
218221
enum Enum<T, K> {
219222
V1,
220223
V2 {
221224
#[educe(Debug(method(fmt)))]
222225
f1: K,
223226
},
224227
V3(
225-
T
228+
Option<T>,
226229
),
227230
}
228231
# }
229232
```
230233
231-
In the above case, `T` is bound to the `Debug` trait, but `K` is not.
234+
In the above case, `Option<T>` is bound to the `Debug` trait, but `K` is not.
232235
233236
Or, you can have `educe` replicate the behaviour of `std`'s `derive`'s, where a bound is produced for *every* generic parameter, without regard to how it's used in the structure:
234237
@@ -239,14 +242,17 @@ use educe::Educe;
239242
240243
#[derive(Educe)]
241244
#[educe(Debug(bound(*)))]
242-
struct Struct<T> {
245+
struct Struct<T, V> {
243246
#[educe(Debug(ignore))]
244247
f: T,
248+
g: Option<V>,
245249
}
246250
# }
247251
```
248252
249-
This can be useful if you don't want to make the trait implementation part of your permanent public API. In this example, `Struct<T>` doesn't implement `Debug` unless `T` does. I.e., it has a `T: Debug` bound even though that's not needed right now. Later we might want to display `f`; we wouldn't then need to make a breaking API change by adding the bound.
253+
Here the derived bounds are `T: Debug, V: Debug`.
254+
255+
This can be useful if you don't want to make the trait implementation part of your permanent public API. In this example, `Struct<T>` doesn't implement `Debug` unless `T` does. I.e., it has a `T: Debug` bound even though that's not needed right now. Later we might want to display `f`; we wouldn't then need to make a breaking API change by adding the bound. We can also change the type of `g` without affecting the `V: Debug` bound.
250256
251257
This was the behaviour of `Trait(bound)` in educe 0.4.x and earlier.
252258
@@ -336,7 +342,9 @@ enum Enum<T: A> {
336342
337343
###### Generic Parameters Bound to the `Clone` Trait or Others
338344
339-
Generic parameters will be automatically bound to the `Clone` trait if necessary. If the `#[educe(Copy)]` attribute exists, they will be bound to the `Copy` trait.
345+
Unlike the trait derives provided by the standard library,
346+
the types associated with each field or enum variant will be automatically bound to the `Clone` trait if necessary.
347+
If the `#[educe(Copy)]` attribute exists, they will be bound to the `Copy` trait.
340348
341349
```rust
342350
# #[cfg(feature = "Clone")]
@@ -448,7 +456,8 @@ enum Enum {
448456
449457
###### Generic Parameters Bound to the `Copy` Trait or Others
450458
451-
All generic parameters will be automatically bound to the `Copy` trait.
459+
Unlike the trait derives provided by the standard library,
460+
the types associated with each field or enum variant will be automatically bound to the `Copy` trait.
452461
453462
```rust
454463
# #[cfg(all(feature = "Clone", feature = "Copy"))]
@@ -618,7 +627,8 @@ enum Enum<T: A> {
618627
619628
###### Generic Parameters Bound to the `PartialEq` Trait or Others
620629
621-
Generic parameters will be automatically bound to the `PartialEq` trait if necessary.
630+
Unlike the trait derives provided by the standard library,
631+
the types associated with each field or enum variant will be automatically bound to the `PartialEq` trait if necessary.
622632
623633
```rust
624634
# #[cfg(feature = "PartialEq")]
@@ -805,7 +815,8 @@ enum Enum<T: A> {
805815
806816
###### Generic Parameters Bound to the `PartialEq` Trait or Others
807817
808-
Generic parameters will be automatically bound to the `PartialEq` trait if necessary.
818+
Unlike the trait derives provided by the standard library,
819+
the types associated with each field or enum variant will be automatically bound to the `PartialEq` trait if necessary.
809820
810821
```rust
811822
# #[cfg(all(feature = "PartialEq", feature = "Eq"))]
@@ -1020,7 +1031,8 @@ enum Enum {
10201031
10211032
###### Generic Parameters Bound to the `PartialOrd` Trait or Others
10221033
1023-
Generic parameters will be automatically bound to the `PartialOrd` trait if necessary.
1034+
Unlike the trait derives provided by the standard library,
1035+
the types associated with each field or enum variant will be automatically bound to the `PartialOrd` trait if necessary.
10241036
10251037
```rust
10261038
# #[cfg(feature = "PartialOrd")]
@@ -1255,7 +1267,8 @@ enum Enum {
12551267
12561268
###### Generic Parameters Bound to the `Ord` Trait or Others
12571269
1258-
Generic parameters will be automatically bound to the `Ord` trait if necessary.
1270+
Unlike the trait derives provided by the standard library,
1271+
the types associated with each field or enum variant will be automatically bound to the `Ord` trait if necessary.
12591272
12601273
```rust
12611274
# #[cfg(all(feature = "PartialOrd", feature = "Ord"))]
@@ -1406,7 +1419,8 @@ enum Enum<T> {
14061419
14071420
###### Generic Parameters Bound to the `Hash` Trait or Others
14081421
1409-
Generic parameters will be automatically bound to the `Hash` trait if necessary.
1422+
Unlike the trait derives provided by the standard library,
1423+
the types associated with each field or enum variant will be automatically bound to the `Hash` trait if necessary.
14101424
14111425
```rust
14121426
# #[cfg(feature = "Hash")]
@@ -1634,7 +1648,8 @@ union Union {
16341648
16351649
###### Generic Parameters Bound to the `Default` Trait or Others
16361650
1637-
Generic parameters will be automatically bound to the `Default` trait if necessary.
1651+
Unlike the trait derives provided by the standard library,
1652+
the types associated with each field or enum variant will be automatically bound to the `Default` trait if necessary.
16381653
16391654
```rust
16401655
# #[cfg(feature = "Default")]
@@ -1842,7 +1857,8 @@ enum Enum {
18421857
18431858
###### Generic Parameters Bound to the `Into` Trait or Others
18441859
1845-
Generic parameters will be automatically bound to the `Into<type>` trait if necessary.
1860+
Unlike the trait derives provided by the standard library,
1861+
the types associated with each field or enum variant will be automatically bound to the `Into<type>` trait if necessary.
18461862
18471863
```rust
18481864
# #[cfg(feature = "Into")]

0 commit comments

Comments
 (0)