@@ -5,9 +5,9 @@ trait, written in type positions) but this was a bit too confusing, so we now
55write ` dyn Trait ` .
66
77Some 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.
1111
1212Two general aspects of trait object types give rise to the restrictions:
1313
@@ -25,13 +25,16 @@ Two general aspects of trait object types give rise to the restrictions:
2525 objects with the same trait object type may point to vtables from different
2626 implementations.
2727
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".
3033
3134### The trait requires ` Self: Sized `
3235
3336Traits 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 .
3538
3639The reasoning behind this is somewhat subtle. It derives from the fact that Rust
3740requires (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
5861to the rule that ` dyn Trait ` always implements ` Trait ` , Rust chooses to prohibit
5962such a ` dyn Trait ` from existing at all.
6063
61- Only unsized traits are considered object-safe .
64+ Only unsized traits are considered dyn-compatible .
6265
6366Generally, ` Self: Sized ` is used to indicate that the trait should not be used
6467as a trait object. If the trait comes from your own crate, consider removing
@@ -103,8 +106,8 @@ fn call_foo(x: Box<dyn Trait>) {
103106}
104107```
105108
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
108111functionality will still be available to all other implementers, including
109112` Box<dyn Trait> ` which is itself sized (assuming you `impl Trait for Box<dyn
110113Trait>`).
@@ -117,7 +120,7 @@ trait Trait {
117120```
118121
119122Now, ` 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
121124methods. With such a bound, one can still call ` foo() ` on types implementing
122125that trait that aren't behind trait objects.
123126
@@ -306,18 +309,18 @@ Here, the supertrait might have methods as follows:
306309
307310```
308311trait 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 !
310313}
311314```
312315
313316If the trait ` Trait ` was deriving from something like ` Super<String> ` or
314317` Super<T> ` (where ` Foo ` itself is ` Foo<T> ` ), this is okay, because given a type
315318` get_a() ` will definitely return an object of that type.
316319
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.
321324
322325There's no easy fix for this. Generally, code will need to be refactored so that
323326you no longer need to derive from ` Super<Self> ` .
0 commit comments