@@ -22,23 +22,39 @@ extern crate auto_impl;
22
22
use auto_impl :: auto_impl;
23
23
```
24
24
25
- The following types are supported:
25
+ Add an ` auto_impl ` attribute to traits you want to automatically implement for wrapper types:
26
+
27
+ ``` rust
28
+ #[auto_impl(& , Arc )]
29
+ pub trait OrderStoreFilter {
30
+ fn filter <F >(& self , predicate : F ) -> Result <Iter , Error >
31
+ where
32
+ F : Fn (& OrderData ) -> bool ;
33
+ }
34
+ ```
35
+
36
+ Now anywhere that requires a ` T: OrderStoreFilter ` will also accept an ` &T ` or ` Arc<T> ` where ` T ` implements ` OrderStoreFilter ` .
37
+
38
+ ## Specifics
39
+
40
+ The following types are supported and can be freely combined:
26
41
27
42
- [ ` Arc ` ] ( https://doc.rust-lang.org/std/sync/struct.Arc.html )
28
43
- [ ` Rc ` ] ( https://doc.rust-lang.org/std/rc/struct.Rc.html )
29
44
- [ ` Box ` ] ( https://doc.rust-lang.org/std/boxed/struct.Box.html )
45
+
46
+ The following types are also supported, but require a blanket implementation (you can only have one of these per ` trait ` ):
47
+
30
48
- [ ` Fn ` ] ( https://doc.rust-lang.org/std/ops/trait.Fn.html )
31
49
- [ ` FnMut ` ] ( https://doc.rust-lang.org/std/ops/trait.FnMut.html )
32
50
- [ ` FnOnce ` ] ( https://doc.rust-lang.org/std/ops/trait.FnOnce.html )
33
51
- ` & `
34
52
- ` &mut `
35
53
36
- ## Implement a trait for a smart pointer
37
-
38
- Add the ` #[auto_impl] ` attribute to traits to automatically implement them for wrapper types:
54
+ ## Implement a trait for ` Box `
39
55
40
56
``` rust
41
- #[auto_impl(Arc , Box , Rc )]
57
+ #[auto_impl(Box )]
42
58
trait MyTrait <'a , T >
43
59
where T : AsRef <str >
44
60
{
@@ -47,41 +63,74 @@ trait MyTrait<'a, T>
47
63
48
64
fn execute1 <'b >(& 'a self , arg1 : & 'b T ) -> Result <Self :: Type1 , String >;
49
65
fn execute2 (& self ) -> Self :: Type2 ;
66
+ fn execute3 (self ) -> Self :: Type1 ;
67
+ fn execute4 () -> & 'static str ;
50
68
}
51
69
```
52
70
53
71
Will expand to:
54
72
55
73
``` rust
56
- impl <'a , T , TAutoImpl > MyTrait <'a , T > for :: std :: sync :: Arc <TAutoImpl >
74
+ impl <'a , T , TAutoImpl > MyTrait <'a , T > for :: std :: boxed :: Box <TAutoImpl >
57
75
where TAutoImpl : MyTrait <'a , T >,
58
76
T : AsRef <str >
59
77
{
60
78
type Type1 = TAutoImpl :: Type1 ;
61
79
type Type2 = TAutoImpl :: Type2 ;
62
80
63
81
fn execute1 <'b >(& 'a self , arg1 : & 'b T ) -> Result <Self :: Type1 , String > {
64
- self . as_ref ( ). execute1 (arg1 )
82
+ ( * * self ). execute1 (arg1 )
65
83
}
66
84
67
85
fn execute2 (& self ) -> Self :: Type2 {
68
- self . as_ref (). execute2 ()
86
+ (* * self ). execute2 ()
87
+ }
88
+
89
+ fn execute3 (self ) -> Self :: Type1 {
90
+ (* self ). execute3 ()
69
91
}
92
+
93
+ fn execute4 () -> & 'static str {
94
+ TAutoImpl :: execute4 ()
95
+ }
96
+ }
97
+ ```
98
+
99
+ There are no restrictions on ` auto_impl ` for ` Box ` .
100
+
101
+ ## Implement a trait for a smart pointer
102
+
103
+ Add the ` #[auto_impl] ` attribute to traits to automatically implement them for wrapper types:
104
+
105
+ ``` rust
106
+ #[auto_impl(Arc , Rc )]
107
+ trait MyTrait <'a , T >
108
+ where T : AsRef <str >
109
+ {
110
+ type Type1 ;
111
+ type Type2 ;
112
+
113
+ fn execute1 <'b >(& 'a self , arg1 : & 'b T ) -> Result <Self :: Type1 , String >;
114
+ fn execute2 (& self ) -> Self :: Type2 ;
70
115
}
116
+ ```
71
117
72
- impl <'a , T , TAutoImpl > MyTrait <'a , T > for Box <TAutoImpl >
118
+ Will expand to:
119
+
120
+ ``` rust
121
+ impl <'a , T , TAutoImpl > MyTrait <'a , T > for :: std :: sync :: Arc <TAutoImpl >
73
122
where TAutoImpl : MyTrait <'a , T >,
74
123
T : AsRef <str >
75
124
{
76
125
type Type1 = TAutoImpl :: Type1 ;
77
126
type Type2 = TAutoImpl :: Type2 ;
78
127
79
128
fn execute1 <'b >(& 'a self , arg1 : & 'b T ) -> Result <Self :: Type1 , String > {
80
- self . as_ref ( ). execute1 (arg1 )
129
+ ( * * self ). execute1 (arg1 )
81
130
}
82
131
83
132
fn execute2 (& self ) -> Self :: Type2 {
84
- self . as_ref ( ). execute2 ()
133
+ ( * * self ). execute2 ()
85
134
}
86
135
}
87
136
@@ -93,18 +142,18 @@ impl<'a, T, TAutoImpl> MyTrait<'a, T> for ::std::rc::Rc<TAutoImpl>
93
142
type Type2 = TAutoImpl :: Type2 ;
94
143
95
144
fn execute1 <'b >(& 'a self , arg1 : & 'b T ) -> Result <Self :: Type1 , String > {
96
- self . as_ref ( ). execute1 (arg1 )
145
+ ( * * self ). execute1 (arg1 )
97
146
}
98
147
99
148
fn execute2 (& self ) -> Self :: Type2 {
100
- self . as_ref ( ). execute2 ()
149
+ ( * * self ). execute2 ()
101
150
}
102
151
}
103
152
```
104
153
105
154
There are a few restrictions on ` #[auto_impl] ` for smart pointers. The trait must:
106
155
107
- - Only have methods that take ` &self `
156
+ - Only have methods that take ` &self ` or have no receiver (static)
108
157
109
158
## Implement a trait for a closure
110
159
@@ -119,7 +168,7 @@ Will expand to:
119
168
120
169
``` rust
121
170
impl <'a , T , TAutoImpl > MyTrait <'a , T > for TAutoImpl
122
- where TAutoImpl : Fn (& T , & 'static str ) -> Result <(), String >
171
+ where TAutoImpl : :: std :: ops :: Fn (& T , & 'static str ) -> Result <(), String >
123
172
{
124
173
fn execute <'b >(& 'a self , arg1 : & 'b T , arg1 : & 'static str ) -> Result <(), String > {
125
174
self (arg1 , arg2 )
@@ -160,6 +209,8 @@ impl<'auto, 'a, T, TAutoImpl> MyTrait<'a, T> for &'auto mut TAutoImpl {
160
209
161
210
There are a few restrictions on ` #[auto_impl] ` for immutably borrowed references. The trait must:
162
211
163
- - Only have methods that take ` &self `
212
+ - Only have methods that take ` &self ` or have no receiver (static)
213
+
214
+ There are a few restrictions on ` #[auto_impl] ` for mutably borrowed references. The trait must:
164
215
165
- There are no restrictions on ` #[auto_impl] ` for mutably borrowed references.
216
+ - Only have methods that take ` &self ` , ` &mut self ` or have no receiver (static)
0 commit comments