]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0376.md
Auto merge of #68154 - ssomers:btreemap_navigation_benches, r=Mark-Simulacrum
[rust.git] / src / librustc_error_codes / error_codes / E0376.md
1 The type you are trying to impl `CoerceUnsized` for is not a struct.
2 `CoerceUnsized` can only be implemented for a struct. Unsized types are
3 already able to be coerced without an implementation of `CoerceUnsized`
4 whereas a struct containing an unsized type needs to know the unsized type
5 field it's containing is able to be coerced. An [unsized type][1]
6 is any type that the compiler doesn't know the length or alignment of at
7 compile time. Any struct containing an unsized type is also unsized.
8
9 [1]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait
10
11 Example of erroneous code:
12
13 ```compile_fail,E0376
14 #![feature(coerce_unsized)]
15 use std::ops::CoerceUnsized;
16
17 struct Foo<T: ?Sized> {
18     a: T,
19 }
20
21 // error: The type `U` is not a struct
22 impl<T, U> CoerceUnsized<U> for Foo<T> {}
23 ```
24
25 The `CoerceUnsized` trait takes a struct type. Make sure the type you are
26 providing to `CoerceUnsized` is a struct with only the last field containing an
27 unsized type.
28
29 Example:
30
31 ```
32 #![feature(coerce_unsized)]
33 use std::ops::CoerceUnsized;
34
35 struct Foo<T> {
36     a: T,
37 }
38
39 // The `Foo<U>` is a struct so `CoerceUnsized` can be implemented
40 impl<T, U> CoerceUnsized<Foo<U>> for Foo<T> where T: CoerceUnsized<U> {}
41 ```
42
43 Note that in Rust, structs can only contain an unsized type if the field
44 containing the unsized type is the last and only unsized type field in the
45 struct.