]>
Commit | Line | Data |
---|---|---|
1b1a35ee XL |
1 | An inherent implementation was defined for something which isn't a struct, |
2 | enum, union, or trait object. | |
60c5eb7d XL |
3 | |
4 | Erroneous code example: | |
5 | ||
6 | ```compile_fail,E0118 | |
064997fb | 7 | impl<T> T { // error: no nominal type found for inherent implementation |
60c5eb7d XL |
8 | fn get_state(&self) -> String { |
9 | // ... | |
10 | } | |
11 | } | |
12 | ``` | |
13 | ||
14 | To fix this error, please implement a trait on the type or wrap it in a struct. | |
15 | Example: | |
16 | ||
17 | ``` | |
18 | // we create a trait here | |
19 | trait LiveLongAndProsper { | |
20 | fn get_state(&self) -> String; | |
21 | } | |
22 | ||
064997fb FG |
23 | // and now you can implement it on T |
24 | impl<T> LiveLongAndProsper for T { | |
60c5eb7d XL |
25 | fn get_state(&self) -> String { |
26 | "He's dead, Jim!".to_owned() | |
27 | } | |
28 | } | |
29 | ``` | |
30 | ||
31 | Alternatively, you can create a newtype. A newtype is a wrapping tuple-struct. | |
32 | For example, `NewType` is a newtype over `Foo` in `struct NewType(Foo)`. | |
33 | Example: | |
34 | ||
35 | ``` | |
064997fb | 36 | struct TypeWrapper<T>(T); |
60c5eb7d | 37 | |
064997fb | 38 | impl<T> TypeWrapper<T> { |
60c5eb7d XL |
39 | fn get_state(&self) -> String { |
40 | "Fascinating!".to_owned() | |
41 | } | |
42 | } | |
43 | ``` |