@@ -30,17 +30,16 @@ Unlike [alternatives](#alternatives), `#[autoimpl]` has minimal and intuitive sy
30
30
use impl_tools :: autoimpl;
31
31
use std :: fmt :: Debug ;
32
32
33
- #[autoimpl( for <' a , T : trait + ? Sized > Box < T >)]
34
- // Generates: impl<'a, T: Animal + ?Sized> Animal for Box<T> { .. }
33
+ // Impl Animal for Box<T> where T: Animal + ?Sized
34
+ #[autoimpl( for < T : trait + ? Sized > Box <T >)]
35
35
trait Animal {
36
36
fn number_of_legs (& self ) -> u32 ;
37
37
}
38
38
39
+ // Impl Debug for Named<T, A: Animal> omitting field animal from output
39
40
#[autoimpl(Debug ignore self. animal where T : Debug )]
40
- // Generates: impl<T, A: Animal> std::fmt::Debug for Named<A> where T: Debug { .. }
41
+ // Impl Deref and DerefMut to field animal for Named<T, A: Animal>
41
42
#[autoimpl(Deref , DerefMut using self. animal)]
42
- // Generates: impl<T, A: Animal> std::ops::Deref for Named<A> { .. }
43
- // Generates: impl<T, A: Animal> std::ops::DerefMut for Named<A> { .. }
44
43
struct Named <T , A : Animal > {
45
44
name : T ,
46
45
animal : A ,
@@ -73,20 +72,39 @@ trait allows succinct new-type patterns:
73
72
74
73
``` rust
75
74
use impl_tools :: autoimpl;
75
+ use std :: sync :: Arc ;
76
76
77
- #[autoimpl(for <T : trait > & T , & mut T )]
78
- trait Foo {
77
+ // Impl Foo for &T, &mut T and Arc<T>
78
+ #[autoimpl(for <T : trait + ? Sized > & T , & mut T , Arc <T >)]
79
+ // Optional: impl Foo for NewFoo (requires NewFoo: Deref<Target = T>)
80
+ #[autoimpl(for <T : trait > NewFoo <T >)]
81
+ pub trait Foo {
79
82
fn success (& self ) -> bool ;
80
83
}
81
84
82
- #[autoimpl(Deref , DerefMut using self. 0)]
83
- struct NewFoo <T : Foo >(T );
85
+ // Impl Deref and DerefMut to a Target which itself supports Foo
86
+ #[autoimpl(Deref <Target = T >, DerefMut using self. 0)]
87
+ pub struct NewFoo <T : Foo >(T );
88
+
89
+ // Impl Deref and DerefMut to a Target which itself supports Foo
90
+ #[autoimpl(Deref <Target = dyn Foo >, DerefMut using self. 0)]
91
+ pub struct ArcDynFoo (Arc <dyn Foo >);
92
+
93
+ #[test]
94
+ fn test_foo_newtypes () {
95
+ struct Success ;
96
+ impl Foo for Success {
97
+ fn success (& self ) -> bool { true }
98
+ }
84
99
85
- // NewFoo now supports `Deref<Target = T>` and `&T` supports `Foo`.
86
- // Effectively, `NewFoo` supports `Foo`.
87
- // Works for FooRef<'a>(&'a dyn Foo) too; see tests/newtype.rs
100
+ // We can now directly call Foo's methods on the wrapper:
101
+ assert! (NewFoo (Success ). success ());
102
+ assert! (ArcDynFoo (Arc :: new (Success )). success ());
103
+ }
88
104
```
89
105
106
+ See [ ` tests/newtype.rs ` ] ( https://github.com/kas-gui/impl-tools/blob/master/tests/newtype.rs ) for more variants of this pattern.
107
+
90
108
91
109
### Impl Default
92
110
0 commit comments