@@ -52,7 +52,7 @@ Trait functions are not allowed to be [`const`].
52
52
53
53
Generic items may use traits as [ bounds] on their type parameters.
54
54
55
- ## Generic Traits
55
+ ## Generic traits
56
56
57
57
Type parameters can be specified for a trait to make it generic. These appear
58
58
after the trait name, using the same syntax used in [ generic functions] .
@@ -65,12 +65,13 @@ trait Seq<T> {
65
65
}
66
66
```
67
67
68
- ## Object Safety
68
+ <a id =" object-safety " ></a >
69
+ ## Dyn compatibility
69
70
70
- Object safe traits can be the base trait of a [ trait object] . A trait is
71
- * object safe * if it has the following qualities (defined in [ RFC 255 ] ) :
71
+ A dyn-compatible trait can be the base trait of a [ trait object] . A trait is
72
+ * dyn compatible * if it has the following qualities:
72
73
73
- * All [ supertraits] must also be object safe .
74
+ * All [ supertraits] must also be dyn compatible .
74
75
* ` Sized ` must not be a [ supertrait] [ supertraits ] . In other words, it must not require ` Self: Sized ` .
75
76
* It must not have any associated constants.
76
77
* It must not have any associated types with generics.
@@ -92,11 +93,13 @@ Object safe traits can be the base trait of a [trait object]. A trait is
92
93
* Explicitly non-dispatchable functions require:
93
94
* Have a ` where Self: Sized ` bound (receiver type of ` Self ` (i.e. ` self ` ) implies this).
94
95
96
+ > ** Note** : This concept was formerly known as * object safety* .
97
+
95
98
``` rust
96
99
# use std :: rc :: Rc ;
97
100
# use std :: sync :: Arc ;
98
101
# use std :: pin :: Pin ;
99
- // Examples of object safe methods.
102
+ // Examples of dyn compatible methods.
100
103
trait TraitMethods {
101
104
fn by_ref (self : & Self ) {}
102
105
fn by_ref_mut (self : & mut Self ) {}
@@ -113,7 +116,7 @@ trait TraitMethods {
113
116
```
114
117
115
118
``` rust,compile_fail
116
- // This trait is object-safe , but these methods cannot be dispatched on a trait object.
119
+ // This trait is dyn compatible , but these methods cannot be dispatched on a trait object.
117
120
trait NonDispatchable {
118
121
// Non-methods cannot be dispatched.
119
122
fn foo() where Self: Sized {}
@@ -137,8 +140,8 @@ obj.typed(1); // ERROR: cannot call with generic type
137
140
138
141
``` rust,compile_fail
139
142
# use std::rc::Rc;
140
- // Examples of non-object safe traits.
141
- trait NotObjectSafe {
143
+ // Examples of dyn-incompatible traits.
144
+ trait DynIncompatible {
142
145
const CONST: i32 = 1; // ERROR: cannot have associated const
143
146
144
147
fn foo() {} // ERROR: associated function without Sized
@@ -148,14 +151,14 @@ trait NotObjectSafe {
148
151
}
149
152
150
153
struct S;
151
- impl NotObjectSafe for S {
154
+ impl DynIncompatible for S {
152
155
fn returns(&self) -> Self { S }
153
156
}
154
- let obj: Box<dyn NotObjectSafe > = Box::new(S); // ERROR
157
+ let obj: Box<dyn DynIncompatible > = Box::new(S); // ERROR
155
158
```
156
159
157
160
``` rust,compile_fail
158
- // Self: Sized traits are not object-safe .
161
+ // ` Self: Sized` traits are dyn-incompatible .
159
162
trait TraitWithSize where Self: Sized {}
160
163
161
164
struct S;
@@ -164,7 +167,7 @@ let obj: Box<dyn TraitWithSize> = Box::new(S); // ERROR
164
167
```
165
168
166
169
``` rust,compile_fail
167
- // Not object safe if `Self` is a type argument.
170
+ // Dyn-incompatible if `Self` is a type argument.
168
171
trait Super<A> {}
169
172
trait WithSelf: Super<Self> where Self: Sized {}
170
173
@@ -330,7 +333,6 @@ fn main() {
330
333
[ _WhereClause_ ] : generics.md#where-clauses
331
334
[ bounds ] : ../trait-bounds.md
332
335
[ trait object ] : ../types/trait-object.md
333
- [ RFC 255 ] : https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
334
336
[ associated items ] : associated-items.md
335
337
[ method ] : associated-items.md#methods
336
338
[ supertraits ] : #supertraits
@@ -349,3 +351,17 @@ fn main() {
349
351
[ `async` ] : functions.md#async-functions
350
352
[ `const` ] : functions.md#const-functions
351
353
[ type namespace ] : ../names/namespaces.md
354
+
355
+ <script >
356
+ (function () {
357
+ var fragments = {
358
+ " #object-safety" : " traits.html#dyn-compatibility" ,
359
+ };
360
+ var target = fragments[window .location .hash ];
361
+ if (target) {
362
+ var url = window .location .toString ();
363
+ var base = url .substring (0 , url .lastIndexOf (' /' ));
364
+ window .location .replace (base + " /" + target);
365
+ }
366
+ })();
367
+ </script >
0 commit comments