1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! A crate for avoiding code duplication for immutable and mutable types.
//!
//! # Reason for existance
//! When writing rust programs, times come when you need two types,
//! one immutable and one mutable.
//!
//! It is possible to remove this duplication with DSTs, such as the
//! standard library's slice type, where `&[T]` and `&mut [T]` are the
//! immutable/mutable counterparts. However, DSTs cannot be created
//! by the programmer, and therefore they are not always applicable.
//!
//! When making two very similar types that are just immutable/mutable
//! counterparts to each other, you may have to implement the same
//! things on both of the types. Here is an example of the duplication;
//!
//! ```
//! struct WrappedSlice<'a, T>(&'a [T]);
//! struct WrappedSliceMut<'a, T>(&'a mut [T]);
//!
//! impl<T> WrappedSlice<'_, T> {
//! pub fn inner(&self) -> &'_ [T] {
//! self.0
//! }
//!
//! pub fn get(&self, index: usize) -> Option<&'_ T> {
//! self.0.get(index)
//! }
//! }
//!
//! impl<T> WrappedSliceMut<'_, T> {
//! pub fn inner(&self) -> &'_ [T] {
//! self.0
//! }
//!
//! pub fn get(&self, index: usize) -> Option<&'_ T> {
//! self.0.get(index)
//! }
//!
//! pub fn get_mut(&mut self, index: usize) -> Option<&'_ mut T> {
//! self.0.get_mut(index)
//! }
//! }
//! ```
//!
//! This can be solved by having a way to implement the same items on both
//! types. That's what this crate is designed for!
//! This is equivalent to the above example but implemented with this
//! crate;
//!
//! ```
//! # use impl_twice::impl_twice;
//! struct WrappedSlice<'a, T>(&'a [T]);
//! struct WrappedSliceMut<'a, T>(&'a mut [T]);
//!
//! impl_twice! (
//! impl<T> WrappedSlice<'_, T>, WrappedSliceMut<'_, T> {
//! pub fn inner(&self) -> &'_ [T] {
//! self.0
//! }
//!
//! pub fn get(&self, index: usize) -> Option<&'_ T> {
//! self.0.get(index)
//! }
//! }
//! );
//!
//! impl<T> WrappedSliceMut<'_, T> {
//! pub fn get_mut(&mut self, index: usize) -> Option<&'_ mut T> {
//! self.0.get_mut(index)
//! }
//! }
//! ```
//!
//! As you can see, the two methods ``inner`` and ``get`` that were duplicated
//! are now only implemented once.
//!
//! # Usage
//! There are quite a few different ways to use the macro based on what you want.
//! ```
//! # use impl_twice::impl_twice;
//!
//! # #[allow(unused)]
//! struct Owned<T>(T);
//! # #[allow(unused)]
//! struct Borrowed<'a, T>(&'a T);
//! # #[allow(unused)]
//! struct BorrowedMut<'a, T>(&'a mut T);
//!
//! trait SomeTrait<T> {
//! fn get(&self) -> &'_ T;
//! }
//!
//! impl_twice!(
//! // Normal impl blocks work just fine
//! impl<T> Owned<T> {
//! fn get(&self) -> &'_ T {
//! &self.0
//! }
//! }
//! );
//! ```
//!
//! If you want to implement the same items on several types,
//! just separate the types with commas;
//!
//! ```
//! # use impl_twice::impl_twice;
//! # #[allow(unused)]
//! # struct Owned<T>(T);
//! # #[allow(unused)]
//! # struct Borrowed<'a, T>(&'a T);
//! # #[allow(unused)]
//! # struct BorrowedMut<'a, T>(&'a mut T);
//! impl_twice!(
//! impl<T>
//! Borrowed<'_, T>,
//! BorrowedMut<'_, T>
//! {
//! fn get(&self) -> &'_ T {
//! self.0
//! }
//! }
//! );
//! ```
//! Adding type bounds is done with 'where'.
//! Here this library differs from normal impls in that you have
//! to add parenthesees after the where.
//! ```
//! # use impl_twice::impl_twice;
//! # #[allow(unused)]
//! # struct Owned<T>(T);
//! # #[allow(unused)]
//! # struct Borrowed<'a, T>(&'a T);
//! # #[allow(unused)]
//! # struct BorrowedMut<'a, T>(&'a mut T);
//! impl_twice!(
//! impl<T>
//! Borrowed<'_, T>,
//! BorrowedMut<'_, T>
//! where (T: Clone) {
//! fn to_owned(&self) -> Owned<T> {
//! Owned(self.0.clone())
//! }
//! }
//! );
//! ```
//! Traits work as well.
//! ```
//! # use impl_twice::impl_twice;
//! # #[allow(unused)]
//! # struct Owned<T>(T);
//! # #[allow(unused)]
//! # struct Borrowed<'a, T>(&'a T);
//! # #[allow(unused)]
//! # struct BorrowedMut<'a, T>(&'a mut T);
//! use std::fmt::{Debug, Formatter, Result};
//! impl_twice!(
//! impl<T>
//! Debug for Borrowed<'_, T>,
//! Debug for BorrowedMut<'_, T>,
//! Debug for Owned<T>
//! where (T: Debug) {
//! fn fmt(&self, f: &mut Formatter<'_>) -> Result {
//! write!(f, "[{:?}]", self.0)
//! }
//! }
//! );
//! ```
//!
//! Sometimes you may want different generics
//! for each type, maybe because they have a different number
//! of generic parameters.
//!
//! In that case, you can simply add another `impl`, and the
//! types specified after that impl will use its generics.
//!
//! You have a separate `where` block for each `impl`.
//!
//! ```
//! # use impl_twice::impl_twice;
//! #[derive(Clone)]
//! struct Complex<A, B>(A, B);
//! #[derive(Clone)]
//! struct Simple<T>(T);
//!
//! impl_twice!(
//! impl<A, B> Complex<A, B> where (A: Clone, B: Clone)
//! impl<T> Simple<T> where (T: Clone) {
//! fn redundant_clone_method_for_example_purposes(&self) -> Self {
//! self.clone()
//! }
//! }
//! );
//! ```
//!
//! # Limitations
//! * Trait, type names and generic parameters are simply tokens. That means, you cannot specify a
//! path with ``::``, so you have to ``use`` the items first before implementing them. This also
//! means that the generic parameters cannot depend on other generic parameters. This
//! might get implemented eventually however.
//!
/// A macro for avoiding code duplication for immutable and mutable types.
/// Check out the crate level documentation for more information
$*) => ;
=> ;
=>