]>
Commit | Line | Data |
---|---|---|
60c5eb7d | 1 | An attempt was made to implement `Drop` on a specialization of a generic type. |
74b04a01 XL |
2 | |
3 | Erroneous code example: | |
60c5eb7d XL |
4 | |
5 | ```compile_fail,E0367 | |
74b04a01 | 6 | trait Foo {} |
60c5eb7d XL |
7 | |
8 | struct MyStruct<T> { | |
9 | t: T | |
10 | } | |
11 | ||
12 | impl<T: Foo> Drop for MyStruct<T> { | |
13 | fn drop(&mut self) {} | |
14 | } | |
15 | ``` | |
16 | ||
17 | This code is not legal: it is not possible to specialize `Drop` to a subset of | |
18 | implementations of a generic type. In order for this code to work, `MyStruct` | |
19 | must also require that `T` implements `Foo`. Alternatively, another option is | |
20 | to wrap the generic type in another that specializes appropriately: | |
21 | ||
22 | ``` | |
23 | trait Foo{} | |
24 | ||
25 | struct MyStruct<T> { | |
26 | t: T | |
27 | } | |
28 | ||
29 | struct MyStructWrapper<T: Foo> { | |
30 | t: MyStruct<T> | |
31 | } | |
32 | ||
33 | impl <T: Foo> Drop for MyStructWrapper<T> { | |
34 | fn drop(&mut self) {} | |
35 | } | |
36 | ``` |