@@ -5,9 +5,9 @@ trait, written in type positions) but this was a bit too confusing, so we now
5
5
write ` dyn Trait ` .
6
6
7
7
Some traits are not allowed to be used as trait object types. The traits that
8
- are allowed to be used as trait object types are called "object-safe" traits.
9
- Attempting to use a trait object type for a trait that is not object-safe will
10
- trigger error E0038.
8
+ are allowed to be used as trait object types are called "dyn-compatible" [ ^ 1 ]
9
+ traits. Attempting to use a trait object type for a trait that is not
10
+ dyn-compatible will trigger error E0038.
11
11
12
12
Two general aspects of trait object types give rise to the restrictions:
13
13
@@ -25,13 +25,16 @@ Two general aspects of trait object types give rise to the restrictions:
25
25
objects with the same trait object type may point to vtables from different
26
26
implementations.
27
27
28
- The specific conditions that violate object-safety follow, most of which relate
29
- to missing size information and vtable polymorphism arising from these aspects.
28
+ The specific conditions that violate dyn-compatibility follow, most of which
29
+ relate to missing size information and vtable polymorphism arising from these
30
+ aspects.
31
+
32
+ [ ^ 1 ] : Formerly known as "object-safe".
30
33
31
34
### The trait requires ` Self: Sized `
32
35
33
36
Traits that are declared as ` Trait: Sized ` or which otherwise inherit a
34
- constraint of ` Self:Sized ` are not object-safe .
37
+ constraint of ` Self:Sized ` are not dyn-compatible .
35
38
36
39
The reasoning behind this is somewhat subtle. It derives from the fact that Rust
37
40
requires (and defines) that every trait object type ` dyn Trait ` automatically
@@ -58,7 +61,7 @@ implement a sized trait like `Trait:Sized`. So, rather than allow an exception
58
61
to the rule that ` dyn Trait ` always implements ` Trait ` , Rust chooses to prohibit
59
62
such a ` dyn Trait ` from existing at all.
60
63
61
- Only unsized traits are considered object-safe .
64
+ Only unsized traits are considered dyn-compatible .
62
65
63
66
Generally, ` Self: Sized ` is used to indicate that the trait should not be used
64
67
as a trait object. If the trait comes from your own crate, consider removing
@@ -103,8 +106,8 @@ fn call_foo(x: Box<dyn Trait>) {
103
106
}
104
107
```
105
108
106
- If only some methods aren't object-safe , you can add a ` where Self: Sized ` bound
107
- on them to mark them as explicitly unavailable to trait objects. The
109
+ If only some methods aren't dyn-compatible , you can add a ` where Self: Sized `
110
+ bound on them to mark them as explicitly unavailable to trait objects. The
108
111
functionality will still be available to all other implementers, including
109
112
` Box<dyn Trait> ` which is itself sized (assuming you `impl Trait for Box<dyn
110
113
Trait>`).
@@ -117,7 +120,7 @@ trait Trait {
117
120
```
118
121
119
122
Now, ` foo() ` can no longer be called on a trait object, but you will now be
120
- allowed to make a trait object, and that will be able to call any object-safe
123
+ allowed to make a trait object, and that will be able to call any dyn-compatible
121
124
methods. With such a bound, one can still call ` foo() ` on types implementing
122
125
that trait that aren't behind trait objects.
123
126
@@ -306,18 +309,18 @@ Here, the supertrait might have methods as follows:
306
309
307
310
```
308
311
trait Super<A: ?Sized> {
309
- fn get_a(&self) -> &A; // note that this is object safe !
312
+ fn get_a(&self) -> &A; // note that this is dyn-compatible !
310
313
}
311
314
```
312
315
313
316
If the trait ` Trait ` was deriving from something like ` Super<String> ` or
314
317
` Super<T> ` (where ` Foo ` itself is ` Foo<T> ` ), this is okay, because given a type
315
318
` get_a() ` will definitely return an object of that type.
316
319
317
- However, if it derives from ` Super<Self> ` , even though ` Super ` is object safe,
318
- the method ` get_a() ` would return an object of unknown type when called on the
319
- function. ` Self ` type parameters let us make object safe traits no longer safe,
320
- so they are forbidden when specifying supertraits.
320
+ However, if it derives from ` Super<Self> ` , even though ` Super ` is
321
+ dyn-compatible, the method ` get_a() ` would return an object of unknown type when
322
+ called on the function. ` Self ` type parameters let us make dyn-compatible traits
323
+ no longer compatible, so they are forbidden when specifying supertraits.
321
324
322
325
There's no easy fix for this. Generally, code will need to be refactored so that
323
326
you no longer need to derive from ` Super<Self> ` .
0 commit comments