|
69 | 69 | //! }
|
70 | 70 | //! }
|
71 | 71 | //! }
|
| 72 | +//! |
| 73 | +//! impl From<Cat> for Animal { |
| 74 | +//! fn from(from: Cat) -> Self { |
| 75 | +//! Animal::Cat(from) |
| 76 | +//! } |
| 77 | +//! } |
| 78 | +//! |
| 79 | +//! impl From<Lion> for Animal { |
| 80 | +//! fn from(from: Lion) -> Self { |
| 81 | +//! Animal::Lion(from) |
| 82 | +//! } |
| 83 | +//! } |
| 84 | +//! |
| 85 | +//! impl From<Mouse> for Animal { |
| 86 | +//! fn from(from: Mouse) -> Self { |
| 87 | +//! Animal::Mouse(from) |
| 88 | +//! } |
| 89 | +//! } |
72 | 90 | //! }
|
73 | 91 | //! ```
|
74 | 92 | //! This, however, will only compile if `Cat`, `Lion` and `Mouse` all have a method called `feed`.
|
@@ -314,6 +332,22 @@ impl GenerateProxyImpl {
|
314 | 332 | .get(&path)
|
315 | 333 | .unwrap_or_else(|| panic!("missing declaration of trait `{}`", path))
|
316 | 334 | }
|
| 335 | + |
| 336 | + fn impl_from_variants(&self, module: &mut ItemMod) { |
| 337 | + let proxy_enum = &self.proxy_enum; |
| 338 | + for WrapperVariant { variant, wrapped, .. } in self.get_variants() { |
| 339 | + let variant = &variant.ident; |
| 340 | + let tokens = quote! { |
| 341 | + impl From<#wrapped> for #proxy_enum { |
| 342 | + fn from(from: #wrapped) -> Self { |
| 343 | + #proxy_enum :: #variant(from) |
| 344 | + } |
| 345 | + } |
| 346 | + }; |
| 347 | + let from_impl: ItemImpl = syn::parse2(tokens).unwrap(); |
| 348 | + module.content.as_mut().unwrap().1.push(from_impl.into()); |
| 349 | + } |
| 350 | + } |
317 | 351 | }
|
318 | 352 |
|
319 | 353 | impl VisitMut for GenerateProxyImpl {
|
@@ -360,6 +394,7 @@ impl VisitMut for GenerateProxyImpl {
|
360 | 394 | true
|
361 | 395 | }
|
362 | 396 | });
|
| 397 | + self.impl_from_variants(module); |
363 | 398 | }
|
364 | 399 |
|
365 | 400 | // scan for trait declarations and store them
|
|
0 commit comments