@@ -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,12 @@ trait Seq<T> {
65
65
}
66
66
```
67
67
68
- ## Object Safety
68
+ ## Dyn compatibility
69
69
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 ] ) :
70
+ A dyn-compatible trait can be the base trait of a [ trait object] . A trait is
71
+ * dyn compatible * if it has the following qualities:
72
72
73
- * All [ supertraits] must also be object safe .
73
+ * All [ supertraits] must also be dyn compatible .
74
74
* ` Sized ` must not be a [ supertrait] [ supertraits ] . In other words, it must not require ` Self: Sized ` .
75
75
* It must not have any associated constants.
76
76
* It must not have any associated types with generics.
@@ -92,11 +92,14 @@ Object safe traits can be the base trait of a [trait object]. A trait is
92
92
* Explicitly non-dispatchable functions require:
93
93
* Have a ` where Self: Sized ` bound (receiver type of ` Self ` (i.e. ` self ` ) implies this).
94
94
95
+ This concept was formerly known as * object safety* .
96
+ The original set of rules was defined in [ RFC 255] and has since been extended.
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
@@ -349,3 +352,17 @@ fn main() {
349
352
[ `async` ] : functions.md#async-functions
350
353
[ `const` ] : functions.md#const-functions
351
354
[ type namespace ] : ../names/namespaces.md
355
+
356
+ <script >
357
+ (function () {
358
+ var fragments = {
359
+ " #object-safety" : " items/traits.html#dyn-compatibility" ,
360
+ };
361
+ var target = fragments[window .location .hash ];
362
+ if (target) {
363
+ var url = window .location .toString ();
364
+ var base = url .substring (0 , url .lastIndexOf (' /' ));
365
+ window .location .replace (base + " /" + target);
366
+ }
367
+ })();
368
+ </script >
0 commit comments