@@ -62,12 +62,12 @@ trait Seq<T> {
62
62
}
63
63
```
64
64
65
- ## Object Safety
65
+ ## Trait- Object Safety {#object-safety}
66
66
67
- Object safe traits can be the base trait of a [ trait object] . A trait is
68
- * object safe* if it has the following qualities (defined in [ RFC 255] ):
67
+ An object- safe trait can be used in a ` dyn Trait ` type, and be the base trait of a [ trait object] .
68
+ A trait is * object safe* if it has the following qualities (defined in [ RFC 255] ):
69
69
70
- * All [ supertraits] must also be object safe.
70
+ * All [ supertraits] must also be trait- object safe.
71
71
* ` Sized ` must not be a [ supertrait] [ supertraits ] . In other words, it must not require ` Self: Sized ` .
72
72
* It must not have any associated constants.
73
73
* It must not have any associated types with generics.
@@ -93,7 +93,7 @@ Object safe traits can be the base trait of a [trait object]. A trait is
93
93
# use std :: rc :: Rc ;
94
94
# use std :: sync :: Arc ;
95
95
# use std :: pin :: Pin ;
96
- // Examples of object safe methods.
96
+ // Examples of trait- object safe methods.
97
97
trait TraitMethods {
98
98
fn by_ref (self : & Self ) {}
99
99
fn by_ref_mut (self : & mut Self ) {}
@@ -110,7 +110,7 @@ trait TraitMethods {
110
110
```
111
111
112
112
``` rust,compile_fail
113
- // This trait is object-safe, but these methods cannot be dispatched on a trait object.
113
+ // This trait is object-safe, but these methods cannot be dispatched on a trait object (such as `&dyn NonDispatchable`) .
114
114
trait NonDispatchable {
115
115
// Non-methods cannot be dispatched.
116
116
fn foo() where Self: Sized {}
@@ -135,7 +135,7 @@ obj.typed(1); // ERROR: cannot call with generic type
135
135
``` rust,compile_fail
136
136
# use std::rc::Rc;
137
137
// Examples of non-object safe traits.
138
- trait NotObjectSafe {
138
+ trait NotDynCompatible {
139
139
const CONST: i32 = 1; // ERROR: cannot have associated const
140
140
141
141
fn foo() {} // ERROR: associated function without Sized
@@ -145,14 +145,14 @@ trait NotObjectSafe {
145
145
}
146
146
147
147
struct S;
148
- impl NotObjectSafe for S {
148
+ impl NotDynCompatible for S {
149
149
fn returns(&self) -> Self { S }
150
150
}
151
- let obj: Box<dyn NotObjectSafe > = Box::new(S); // ERROR
151
+ let obj: Box<dyn NotDynCompatible > = Box::new(S); // ERROR
152
152
```
153
153
154
154
``` rust,compile_fail
155
- // Self: Sized traits are not object-safe.
155
+ // Self: Sized traits are not trait- object-safe.
156
156
trait TraitWithSize where Self: Sized {}
157
157
158
158
struct S;
@@ -161,7 +161,7 @@ let obj: Box<dyn TraitWithSize> = Box::new(S); // ERROR
161
161
```
162
162
163
163
``` rust,compile_fail
164
- // Not object safe if `Self` is a type argument.
164
+ // Not trait- object safe if `Self` is a type argument.
165
165
trait Super<A> {}
166
166
trait WithSelf: Super<Self> where Self: Sized {}
167
167
@@ -326,7 +326,7 @@ fn main() {
326
326
[ _Visibility_ ] : ../visibility-and-privacy.md
327
327
[ _WhereClause_ ] : generics.md#where-clauses
328
328
[ bounds ] : ../trait-bounds.md
329
- [ trait object ] : ../types/trait-object.md
329
+ [ trait objects ] : ../types/trait-object.md
330
330
[ RFC 255 ] : https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
331
331
[ associated items ] : associated-items.md
332
332
[ method ] : associated-items.md#methods
0 commit comments