]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0120.md
Rollup merge of #62514 - stephaneyfx:box-ffi, r=nikomatsakis
[rust.git] / src / librustc_error_codes / error_codes / E0120.md
1 An attempt was made to implement Drop on a trait, which is not allowed: only
2 structs and enums can implement Drop. An example causing this error:
3
4 ```compile_fail,E0120
5 trait MyTrait {}
6
7 impl Drop for MyTrait {
8     fn drop(&mut self) {}
9 }
10 ```
11
12 A workaround for this problem is to wrap the trait up in a struct, and implement
13 Drop on that. An example is shown below:
14
15 ```
16 trait MyTrait {}
17 struct MyWrapper<T: MyTrait> { foo: T }
18
19 impl <T: MyTrait> Drop for MyWrapper<T> {
20     fn drop(&mut self) {}
21 }
22
23 ```
24
25 Alternatively, wrapping trait objects requires something like the following:
26
27 ```
28 trait MyTrait {}
29
30 //or Box<MyTrait>, if you wanted an owned trait object
31 struct MyWrapper<'a> { foo: &'a MyTrait }
32
33 impl <'a> Drop for MyWrapper<'a> {
34     fn drop(&mut self) {}
35 }
36 ```