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
#![deny(rust_2018_idioms, clippy::all, clippy::pedantic)]
#![no_std]

//! A crate for avoiding code duplication for immutable and mutable types.

//! Check out the [`impl_twice`] macro for more information.

//!

//! # 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;

//! # use std::fmt::Debug;

//! struct Type;

//! struct TypeMut;

//!

//! impl_twice!(

//!     // The types are separated by commas. There can only be exactly two

//!     // types.

//!     impl Type, TypeMut {

//!         fn hello(&self) {

//!             println!("Hello, World!");

//!         }

//!     }

//!

//!     // Traits work as well

//!     impl Default for Type, TypeMut {

//!         fn default() -> Self {

//!             Self

//!         }

//!     }

//! );

//!

//! struct GenericType<'a, T>(&'a T);

//! struct GenericTypeMut<'a, T>(&'a mut T);

//!

//! trait SomeTrait<T> {

//!     fn get(&self) -> &'_ T;

//! }

//!

//! impl_twice!(

//!     // Generics work as well.

//!     impl<T> GenericType<'_, T>, GenericTypeMut<'_, T> {

//!         pub fn get(&self) -> &'_ T {

//!             self.0

//!         }

//!     }

//!

//!     // Implementing traits with generics works as well.

//!     // However, the things after where clauses have to have

//!     // parenthesees around them. Bounds on the generic parameters

//!     // only work with a where clause, so ``impl<T: ToOwned>`` wouldn't work.

//!     impl<T> Debug for GenericType<'_, T>, GenericTypeMut<'_, T> where (T: Debug) {

//!         fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

//!             write!(f, "{:?}", self.0)

//!         }

//!     }

//!

//!     // You may want different generics or generic bounds on the two different types.

//!     // For that reason you can have two sets of generic parameters.

//!     // This is a bad example because there is actually no reason to do this in this

//!     // case, but it's here if you want it.

//!     impl<T> GenericType<'_, T> where (T: ToString)

//!     impl<T> GenericTypeMut<'_, T> where (T: ToString + Clone) {

//!         fn stuff(&self) -> String {

//!             self.0.to_string()

//!         }

//!     }

//!

//!     // The above also works with traits. (this is also a bad example).

//!     impl<T> SomeTrait<T> for GenericType<'_, T>

//!     impl<T> SomeTrait<T> for GenericTypeMut<'_, T> where (T: Iterator) {

//!         fn get(&self) -> &'_ T {

//!             &self.0

//!         }

//!     }

//! );

//! ```

//!

//! # Limitations

//! The generics in these macros may look the same as generics on real impl blocks,

//! but they are much more limited. That is simply because there seems to be no good

//! way to do generics like this in macros yet. So for now, the generics you can do

//! are quite limited.

//!


/// A macro for avoiding code duplication for immutable and mutable types.

/// Check out the crate level documentation for more information

#[macro_export]
macro_rules! impl_twice {
    () => {};

    (impl $(<$($gen_args:tt),*>)?
        $name1:ident$(<$($name1_param:tt),*>)?,
        $name2:ident$(<$($name2_param:tt),*>)?
        $(where ($($where_args:tt)*))? {
            $($content:item)*
    }$($extra:tt)*) => {
        impl$(<$($gen_args),*>)? $name1 $(<$($name1_param),*>)? $(where $($where_args)*)? {
            $($content)*
        }
        impl$(<$($gen_args),*>)? $name2 $(<$($name2_param),*>)? $(where $($where_args)*)? {
            $($content)*
        }
        impl_twice!($($extra)*);
    };

    (impl $(<$($gen_args:tt),*>)?
        $name1:ident$(<$($name1_param:tt),*>)?
        $(where ($($where_args:tt)*))?
     impl $(<$($gen_args2:tt),*>)?
        $name2:ident$(<$($name2_param:tt),*>)?
        $(where ($($where_args2:tt)*))? {
            $($content:item)*
    }$($extra:tt)*) => {
        impl$(<$($gen_args),*>)? $name1 $(<$($name1_param),*>)? $(where $($where_args)*)? {
            $($content)*
        }
        impl$(<$($gen_args2),*>)? $name2 $(<$($name2_param),*>)? $(where $($where_args2)*)? {
            $($content)*
        }
        impl_twice!($($extra)*);
    };

    (impl $(<$($gen_args:tt),*>)?
        $trait:ident$(<$($trait_param:tt),*>)? for
        $name1:ident$(<$($name1_param:tt),*>)?,
        $name2:ident$(<$($name2_param:tt),*>)?
        $(where ($($where_args:tt)*))? {
            $($content:item)*
    }$($extra:tt)*) => {
        impl$(<$($gen_args),*>)? $trait $(<$($trait_param),*>)? for $name1 $(<$($name1_param),*>)? $(where $($where_args)*)? {
            $($content)*
        }
        impl$(<$($gen_args),*>)? $trait $(<$($trait_param),*>)? for $name2 $(<$($name2_param),*>)? $(where $($where_args)*)? {
            $($content)*
        }
        impl_twice!($($extra)*);
    };

    (impl $(<$($gen_args:tt),*>)?
        $trait:ident$(<$($trait_param:tt),*>)? for
        $name1:ident$(<$($name1_param:tt),*>)?
        $(where ($($where_args:tt)*))?
     impl $(<$($gen_args2:tt),*>)?
        $trait2:ident$(<$($trait_param2:tt),*>)? for
        $name2:ident$(<$($name2_param:tt),*>)?
        $(where ($($where_args2:tt)*))? {
            $($content:item)*
    }$($extra:tt)*) => {
        impl$(<$($gen_args),*>)? $trait $(<$($trait_param),*>)? for $name1 $(<$($name1_param),*>)? $(where $($where_args)*)? {
            $($content)*
        }
        impl$(<$($gen_args2),*>)? $trait2 $(<$($trait_param2),*>)? for $name2 $(<$($name2_param),*>)? $(where $($where_args2)*)? {
            $($content)*
        }
        impl_twice!($($extra)*);
    };
}