]> git.proxmox.com Git - rustc.git/blob - src/librustc_error_codes/error_codes/E0120.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0120.md
1 Drop was implemented on a trait, which is not allowed: only structs and
2 enums can implement Drop.
3
4 Erroneous code example:
5
6 ```compile_fail,E0120
7 trait MyTrait {}
8
9 impl Drop for MyTrait {
10 fn drop(&mut self) {}
11 }
12 ```
13
14 A workaround for this problem is to wrap the trait up in a struct, and implement
15 Drop on that:
16
17 ```
18 trait MyTrait {}
19 struct MyWrapper<T: MyTrait> { foo: T }
20
21 impl <T: MyTrait> Drop for MyWrapper<T> {
22 fn drop(&mut self) {}
23 }
24
25 ```
26
27 Alternatively, wrapping trait objects requires something:
28
29 ```
30 trait MyTrait {}
31
32 //or Box<MyTrait>, if you wanted an owned trait object
33 struct MyWrapper<'a> { foo: &'a MyTrait }
34
35 impl <'a> Drop for MyWrapper<'a> {
36 fn drop(&mut self) {}
37 }
38 ```