1#[macro_export]
66macro_rules! impl_from {
67 (<$($generic:ident),+> in $($rest:tt)+) => {
68 $crate::impl_from!(@impl [$($generic),+] $($rest)+);
69 };
70
71 (<const $($rest:tt)+) => {
72 $crate::__impl_more_parse_generics!(@parse impl_from [] [] const $($rest)+);
73 };
74
75 (<$generic:ident, $($rest:tt)+) => {
76 $crate::__impl_more_parse_generics!(@parse impl_from [] [] $generic, $($rest)+);
77 };
78
79 (@impl [$($generic:tt)*] $from:ty => $this:ty $(,)?) => {
80 impl <$($generic)*> ::core::convert::From<$from> for $this {
81 fn from(from: $from) -> Self {
82 Self(from)
83 }
84 }
85 };
86
87 (@impl [$($generic:tt)*] $from:ty => $this:ty : $field:ident in $($wrapper:tt)+) => {
88 $crate::impl_from!(@wrap [$($generic)*] $from => $this : $field in $($wrapper)+);
89 };
90
91 (@impl [$($generic:tt)*] $from:ty => $this:ty : $field:ident $(,)?) => {
92 impl <$($generic)*> ::core::convert::From<$from> for $this {
93 fn from(from: $from) -> Self {
94 Self { $field: from }
95 }
96 }
97 };
98
99 (@impl [$($generic:tt)*] $from:ty => $($target:tt)+) => {
101 $crate::impl_from!(@tuple [$($generic)*] [$from] [] $($target)+);
102 };
103
104 (@tuple [$($generic:tt)*] [$from:ty] [$($this:tt)+] in $($wrapper:tt)+) => {
105 $crate::impl_from!(@wrap [$($generic)*] $from => $($this)+ : 0 in $($wrapper)+);
106 };
107
108 (@tuple [$($generic:tt)*] [$from:ty] [$($this:tt)*] $next:tt $($rest:tt)+) => {
109 $crate::impl_from!(@tuple [$($generic)*] [$from] [$($this)* $next] $($rest)+);
110 };
111
112 (@wrap [$($generic:tt)*] $from:ty => $this:ty : $field:tt in $($wrapper:tt)+) => {
113 impl <$($generic)*> ::core::convert::From<$from> for $this {
114 fn from(value: $from) -> Self {
115 Self { $field: $crate::impl_from!(@new value; $($wrapper)+) }
116 }
117 }
118 };
119
120 (@new $value:ident; $($segment:ident)::+ $(,)?) => { $($segment)::+::new($value) };
122 (@new $value:ident; ::$($segment:ident)::+ $(,)?) => { ::$($segment)::+::new($value) };
123 (@new $value:ident; $wrapper:ty $(,)?) => { <$wrapper>::new($value) };
124
125 ($from:ty => $($target:tt)+) => {
126 $crate::impl_from!(@impl [] $from => $($target)+);
127 };
128}
129
130#[macro_export]
143macro_rules! impl_from_for_primitive {
144 ($from:ty => $this:ty $(,)?) => {
145 impl ::core::convert::From<$from> for $this {
146 fn from(from: $from) -> $this {
147 <$this as ::core::convert::From<_>>::from(from.0)
148 }
149 }
150 };
151}
152
153#[macro_export]
170macro_rules! impl_newtype_from_into {
171 ($newtype:ty [<=>] $inner:ty $(,)?) => {
172 impl ::core::convert::From<$inner> for $newtype {
173 fn from(from: $inner) -> $newtype {
174 Self(from)
175 }
176 }
177
178 impl ::core::convert::From<$newtype> for $inner {
179 fn from(from: $newtype) -> $inner {
180 from.0
181 }
182 }
183 };
184}
185
186#[macro_export]
215macro_rules! impl_into {
216 (<$($generic:ident),+> in $($rest:tt)+) => {
217 $crate::impl_into!(@impl [$($generic),+] $($rest)+);
218 };
219
220 (<const $($rest:tt)+) => {
221 $crate::__impl_more_parse_generics!(@parse impl_into [] [] const $($rest)+);
222 };
223
224 (<$generic:ident, $($rest:tt)+) => {
225 $crate::__impl_more_parse_generics!(@parse impl_into [] [] $generic, $($rest)+);
226 };
227
228 (@impl [$($generic:tt)+] $this:ty => $inner:ty : $field:ident) => {
229 impl <$($generic)+> ::core::convert::Into<$inner> for $this {
230 fn into(self) -> $inner {
231 self.$field
232 }
233 }
234 };
235
236 (@impl [$($generic:tt)+] $this:ty => $inner:ty) => {
237 impl <$($generic)+> ::core::convert::Into<$inner> for $this {
238 fn into(self) -> $inner {
239 self.0
240 }
241 }
242 };
243
244 ($this:ty => $inner:ty) => {
245 impl ::core::convert::Into<$inner> for $this {
246 fn into(self) -> $inner {
247 self.0
248 }
249 }
250 };
251
252 ($this:ty => $inner:ty : $field:ident) => {
253 impl ::core::convert::Into<$inner> for $this {
254 fn into(self) -> $inner {
255 self.$field
256 }
257 }
258 };
259}
260
261#[cfg(test)]
262mod tests {
263 #![allow(clippy::from_over_into)]
264
265 use alloc::rc::Rc;
266
267 #[test]
268 fn reference_counted_fields() {
269 struct Local {
270 rc: Rc<u32>,
271 }
272 impl_from!(u32 => Local : rc in Rc);
273 impl_from!(Rc<u32> => Local : rc);
274
275 struct Shared(alloc::sync::Arc<u32>);
276 impl_from!(u32 => Shared in alloc::sync::Arc);
277
278 struct Named {
279 arc: u32,
280 }
281 impl_from!(u32 => Named : arc);
282
283 let local = Local::from(42).rc;
284 assert_eq!(*local, 42);
285 assert!(Rc::ptr_eq(&local, &Local::from(local.clone()).rc));
286 assert_eq!(*Shared::from(7).0, 7);
287 assert_eq!(Named::from(9).arc, 9);
288 }
289
290 #[test]
291 fn newtype() {
292 struct Foo(usize);
293 impl_from!(usize => Foo);
294 impl_into!(Foo => usize);
295
296 static_assertions::assert_impl_all!(Foo: From<usize>, Into<usize>);
297
298 let foo = Foo::from(42);
299 assert_eq!(foo.0, 42);
300 }
301
302 #[test]
303 fn newtype_primitive() {
304 struct Foo(usize);
305 impl_from!(usize => Foo);
306 impl_from_for_primitive!(Foo => usize);
307
308 static_assertions::assert_impl_all!(Foo: From<usize>);
309 static_assertions::assert_impl_all!(usize: From<Foo>);
310
311 let foo = Foo::from(42);
312 assert_eq!(foo.0, 42);
313 }
314
315 #[test]
316 fn newtype_generic() {
317 struct Foo<T>(Rc<T>);
318 impl_from!(<T> in Rc<T> => Foo<T>);
319 impl_into!(<T> in Foo<T> => Rc<T>);
320
321 let foo = Foo::from(Rc::new(42_usize));
322 assert_eq!(*foo.0, 42);
323 }
324
325 #[test]
326 fn named_field() {
327 struct Foo {
328 inner: usize,
329 }
330 impl_from!(usize => Foo : inner);
331 impl_into!(Foo => usize : inner);
332
333 static_assertions::assert_impl_all!(Foo: From<usize>, Into<usize>);
334
335 let foo = Foo::from(42);
336 assert_eq!(foo.inner, 42);
337
338 struct MultiFoo {
339 small: u8,
340 big: u64,
341 }
342 impl_into!(MultiFoo => u8 : small);
343 impl_into!(MultiFoo => u64 : big);
344
345 static_assertions::assert_impl_all!(MultiFoo: Into<u8>, Into<u64>);
346
347 let foo = MultiFoo { small: 4, big: 42 };
348 assert_eq!(foo.small, 4);
349 assert_eq!(foo.big, 42);
350 }
351
352 #[test]
353 fn named_field_generic() {
354 struct Foo<T> {
355 inner: Rc<T>,
356 }
357 impl_from!(<T> in Rc<T> => Foo<T> : inner);
358 impl_into!(<T> in Foo<T> => Rc<T> : inner);
359
360 let foo = Foo::from(Rc::new(42_usize));
361 assert_eq!(*foo.inner, 42);
362 }
363}