Skip to content

Commit 7d53210

Browse files
daxpeddaModProg
authored andcommitted
Avoid using only one trait in examples
1 parent 4aa3079 commit 7d53210

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Multiple `derive_where` attributes can be added to an item, but only the
2727
first one must use any path qualifications.
2828

2929
```rust
30-
#[derive_where::derive_where(Clone)]
31-
#[derive_where(Debug)]
30+
#[derive_where::derive_where(Clone, Debug)]
31+
#[derive_where(Eq, PartialEq)]
3232
struct Example1<T>(PhantomData<T>);
3333
```
3434

@@ -49,23 +49,23 @@ specified. This example will restrict the implementation for `Example` to
4949
`T: Clone`:
5050

5151
```rust
52-
#[derive_where(Clone; T)]
52+
#[derive_where(Clone, Debug; T)]
5353
struct Example<T, U>(T, PhantomData<U>);
5454
```
5555

5656
It is also possible to specify the bounds to be applied. This will
5757
bind implementation for `Example` to `T: Super`:
5858

5959
```rust
60-
trait Super: Clone {}
60+
trait Super: Clone + Debug {}
6161

62-
#[derive_where(Clone; T: Super)]
62+
#[derive_where(Clone, Debug; T: Super)]
6363
struct Example<T>(PhantomData<T>);
6464
```
6565

6666
But more complex trait bounds are possible as well.
67-
The example below will restrict the implementation for `Example` to
68-
`T::Type: Clone`:
67+
The example below will restrict the [`Clone`] implementation for `Example`
68+
to `T::Type: Clone`:
6969

7070
```rust
7171
trait Trait {
@@ -78,7 +78,7 @@ impl Trait for Impl {
7878
type Type = i32;
7979
}
8080

81-
#[derive_where(Clone; T::Type)]
81+
#[derive_where(Clone, Debug; T::Type)]
8282
struct Example<T: Trait>(T::Type);
8383
```
8484

@@ -87,8 +87,8 @@ specific constrain. It is also possible to use multiple separate
8787
constrain specifications when required:
8888

8989
```rust
90-
#[derive_where(Clone; T)]
91-
#[derive_where(Debug; U)]
90+
#[derive_where(Clone, Debug; T)]
91+
#[derive_where(Eq, PartialEq; U)]
9292
struct Example<T, U>(PhantomData<T>, PhantomData<U>);
9393
```
9494

@@ -99,7 +99,7 @@ Since Rust 1.62 deriving [`Default`] on an enum is possible with the
9999
`#[derive_where(default)]` attribute:
100100

101101
```rust
102-
#[derive_where(Default)]
102+
#[derive_where(Clone, Default)]
103103
enum Example<T> {
104104
#[derive_where(default)]
105105
A(PhantomData<T>),
@@ -123,19 +123,21 @@ assert_eq!(Example(42), Example(0));
123123
It is also possible to skip all fields in an item or variant if desired:
124124

125125
```rust
126-
#[derive_where(Debug)]
126+
#[derive_where(Debug, PartialEq)]
127127
#[derive_where(skip_inner)]
128128
struct StructExample<T>(T);
129129

130130
assert_eq!(format!("{:?}", StructExample(42)), "StructExample");
131+
assert_eq!(StructExample(42), StructExample(0));
131132

132-
#[derive_where(Debug)]
133+
#[derive_where(Debug, PartialEq)]
133134
enum EnumExample<T> {
134135
#[derive_where(skip_inner)]
135136
A(T),
136137
}
137138

138139
assert_eq!(format!("{:?}", EnumExample::A(42)), "A");
140+
assert_eq!(EnumExample::A(42), EnumExample::A(0));
139141
```
140142

141143
Selective skipping of fields for certain traits is also an option, both in

src/lib.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
//!
3131
//! ```
3232
//! # use std::marker::PhantomData;
33-
//! #[derive_where::derive_where(Clone)]
34-
//! #[derive_where(Debug)]
33+
//! #[derive_where::derive_where(Clone, Debug)]
34+
//! #[derive_where(Eq, PartialEq)]
3535
//! struct Example1<T>(PhantomData<T>);
3636
//! ```
3737
//!
@@ -57,25 +57,26 @@
5757
//! ```
5858
//! # use std::marker::PhantomData;
5959
//! # use derive_where::derive_where;
60-
//! #[derive_where(Clone; T)]
60+
//! #[derive_where(Clone, Debug; T)]
6161
//! struct Example<T, U>(T, PhantomData<U>);
6262
//! ```
6363
//!
6464
//! It is also possible to specify the bounds to be applied. This will
6565
//! bind implementation for `Example` to `T: Super`:
6666
//!
6767
//! ```
68+
//! # use std::fmt::Debug;
6869
//! # use std::marker::PhantomData;
6970
//! # use derive_where::derive_where;
70-
//! trait Super: Clone {}
71+
//! trait Super: Clone + Debug {}
7172
//!
72-
//! #[derive_where(Clone; T: Super)]
73+
//! #[derive_where(Clone, Debug; T: Super)]
7374
//! struct Example<T>(PhantomData<T>);
7475
//! ```
7576
//!
7677
//! But more complex trait bounds are possible as well.
77-
//! The example below will restrict the implementation for `Example` to
78-
//! `T::Type: Clone`:
78+
//! The example below will restrict the [`Clone`] implementation for `Example`
79+
//! to `T::Type: Clone`:
7980
//!
8081
//! ```
8182
//! # use std::marker::PhantomData;
@@ -90,7 +91,7 @@
9091
//! type Type = i32;
9192
//! }
9293
//!
93-
//! #[derive_where(Clone; T::Type)]
94+
//! #[derive_where(Clone, Debug; T::Type)]
9495
//! struct Example<T: Trait>(T::Type);
9596
//! ```
9697
//!
@@ -101,8 +102,8 @@
101102
//! ```
102103
//! # use std::marker::PhantomData;
103104
//! # use derive_where::derive_where;
104-
//! #[derive_where(Clone; T)]
105-
//! #[derive_where(Debug; U)]
105+
//! #[derive_where(Clone, Debug; T)]
106+
//! #[derive_where(Eq, PartialEq; U)]
106107
//! struct Example<T, U>(PhantomData<T>, PhantomData<U>);
107108
//! ```
108109
//!
@@ -115,7 +116,7 @@
115116
//! ```
116117
//! # use std::marker::PhantomData;
117118
//! # use derive_where::derive_where;
118-
//! #[derive_where(Default)]
119+
//! #[derive_where(Clone, Default)]
119120
//! enum Example<T> {
120121
//! #[derive_where(default)]
121122
//! A(PhantomData<T>),
@@ -143,19 +144,21 @@
143144
//! ```
144145
//! # use std::marker::PhantomData;
145146
//! # use derive_where::derive_where;
146-
//! #[derive_where(Debug)]
147+
//! #[derive_where(Debug, PartialEq)]
147148
//! #[derive_where(skip_inner)]
148149
//! struct StructExample<T>(T);
149150
//!
150151
//! assert_eq!(format!("{:?}", StructExample(42)), "StructExample");
152+
//! assert_eq!(StructExample(42), StructExample(0));
151153
//!
152-
//! #[derive_where(Debug)]
154+
//! #[derive_where(Debug, PartialEq)]
153155
//! enum EnumExample<T> {
154156
//! #[derive_where(skip_inner)]
155157
//! A(T),
156158
//! }
157159
//!
158160
//! assert_eq!(format!("{:?}", EnumExample::A(42)), "A");
161+
//! assert_eq!(EnumExample::A(42), EnumExample::A(0));
159162
//! ```
160163
//!
161164
//! Selective skipping of fields for certain traits is also an option, both in

0 commit comments

Comments
 (0)