@@ -146,12 +146,9 @@ let slice: &[i32] = &boxed_array[..];
146
146
All elements of arrays and slices are always initialized, and access to an
147
147
array or slice is always bounds-checked in safe methods and operators.
148
148
149
- > Note: The [ ` Vec<T> ` ] standard library type provides a heap allocated resizable
149
+ > Note: The [ ` Vec<T> ` ] standard library type provides a heap- allocated resizable
150
150
> array type.
151
151
152
- [ dynamically sized type ] : dynamically-sized-types.html
153
- [ `Vec<T>` ] : ../std/vec/struct.Vec.html
154
-
155
152
## Struct types
156
153
157
154
A ` struct ` * type* is a heterogeneous product of other types, called the
@@ -174,9 +171,8 @@ A _tuple struct_ type is just like a struct type, except that the fields are
174
171
anonymous.
175
172
176
173
A _ unit-like struct_ type is like a struct type, except that it has no fields.
177
- The one value constructed by the associated [ struct
178
- expression] ( expressions/struct-expr.html ) is the only value that
179
- inhabits such a type.
174
+ The one value constructed by the associated [ struct expression] is the only
175
+ value that inhabits such a type.
180
176
181
177
[ ^ structtype ] : ` struct ` types are analogous to ` struct ` types in C, the
182
178
* record* types of the ML family, or the * struct* types of the Lisp family.
@@ -365,8 +361,8 @@ x = bo(5,7);
365
361
366
362
## Closure types
367
363
368
- A [ closure expression] ( expressions/closure-expr.html ) produces a closure
369
- value with a unique, anonymous type that cannot be written out.
364
+ A [ closure expression] produces a closure value with a unique, anonymous type
365
+ that cannot be written out.
370
366
371
367
Depending on the requirements of the closure, its type implements one or
372
368
more of the closure traits:
@@ -387,8 +383,8 @@ more of the closure traits:
387
383
moved in the body of the closure. ` Fn ` inherits from ` FnMut ` , which itself
388
384
inherits from ` FnOnce ` .
389
385
390
- Closures that don't use anything from their environment ("non capturing
391
- closures") can be coerced to function pointers (` fn ` ) with the matching
386
+ Closures that don't use anything from their environment, called * non- capturing
387
+ closures* , can be coerced to function pointers (` fn ` ) with the matching
392
388
signature. To adopt the example from the section above:
393
389
394
390
``` rust
@@ -403,10 +399,31 @@ x = bo(5,7);
403
399
404
400
## Trait objects
405
401
406
- In Rust, trait names also refer to [ dynamically sized types] called _ trait
407
- objects_ . Like all <abbr title =" dynamically sized types " >DSTs</abbr >, trait
408
- objects are used behind some type of pointer; for example ` &SomeTrait ` or
409
- ` Box<SomeTrait> ` . Each instance of a pointer to a trait object includes:
402
+ A * trait object* is an opaque value of another type that implements a set of
403
+ traits. The set of traits is made up of an [ object safe] * base trait* plus any
404
+ number of [ auto traits] .
405
+
406
+ Trait objects are written as the path to the base trait followed by the list
407
+ of auto traits all separated by ` + ` . For example, given a trait ` Trait ` , the
408
+ following are all trait objects: ` Trait ` , ` Trait + Send ` , ` Trait + Send + Sync ` .
409
+
410
+ Two trait object types alias each other if the base traits alias each other and
411
+ if the sets of auto traits are the same. For example,
412
+ ` Trait + Send + UnwindSafe ` is the same as ` Trait + Unwindsafe + Send ` .
413
+
414
+ > Warning: With two trait object types, even when the complete set of traits is
415
+ > the same, if the base traits differ, the type is different. For example,
416
+ > ` Send + Sync ` is a different type from ` Sync + Send ` . See [ issue 33140] .
417
+
418
+ > Warning: Including the same auto trait multiple times is allowed, and each
419
+ > instance is considered a unique type. As such, ` Trait + Send ` is a distinct
420
+ > type than ` Trait + Send + Send ` . See [ issue 47010] .
421
+
422
+ Due to the opaqueness of which concrete type the value is of, trait objects are
423
+ [ dynamically sized types] . Like all
424
+ <abbr title =" dynamically sized types " >DSTs</abbr >, trait objects are used
425
+ behind some type of pointer; for example ` &SomeTrait ` or ` Box<SomeTrait> ` . Each
426
+ instance of a pointer to a trait object includes:
410
427
411
428
- a pointer to an instance of a type ` T ` that implements ` SomeTrait `
412
429
- a _ virtual method table_ , often just called a _ vtable_ , which contains, for
@@ -419,23 +436,6 @@ function pointer is loaded from the trait object vtable and invoked indirectly.
419
436
The actual implementation for each vtable entry can vary on an object-by-object
420
437
basis.
421
438
422
- Note that trait object types only exist for
423
- <span id =" object-safety " >* object-safe* </span > traits ([ RFC 255] ):
424
-
425
- * It must not require ` Self: Sized `
426
- * All associated functions must either have a ` where Self: Sized ` bound or
427
- * Not have any type parameters (lifetime parameters are allowed)
428
- * Must be a method: its first parameter must be called self, with type
429
- ` Self ` , ` &Self ` , ` &mut Self ` , ` Box<Self> ` .
430
- * ` Self ` may only be used in the type of the receiver.
431
- * It must not have any associated constants.
432
-
433
- Given a pointer-typed expression ` E ` of type ` &T ` or ` Box<T> ` , where ` T `
434
- implements trait ` R ` , casting ` E ` to the corresponding pointer type ` &R ` or
435
- ` Box<R> ` results in a value of the _ trait object_ ` R ` . This result is
436
- represented as a pair of pointers: the vtable pointer for the ` T `
437
- implementation of ` R ` , and the pointer value of ` E ` .
438
-
439
439
An example of a trait object:
440
440
441
441
``` rust
@@ -459,16 +459,18 @@ fn main() {
459
459
In this example, the trait ` Printable ` occurs as a trait object in both the
460
460
type signature of ` print ` , and the cast expression in ` main ` .
461
461
462
+ ### Trait Object Lifetime Bounds
463
+
462
464
Since a trait object can contain references, the lifetimes of those references
463
465
need to be expressed as part of the trait object. The assumed lifetime of
464
- references held by a trait object is called its _ default object lifetime bound _ .
466
+ references held by a trait object is called its * default object lifetime bound * .
465
467
These were defined in [ RFC 599] and amended in [ RFC 1156] .
466
468
467
469
For traits that themselves have no lifetime parameters:
468
- * If there is a unique bound from the containing type then that is the default
469
- * If there is more than one bound from the containing type then an explicit bound must
470
- be specified
471
- * Otherwise the default bound is ` 'static `
470
+ * If there is a unique bound from the containing type then that is the default.
471
+ * If there is more than one bound from the containing type then an explicit
472
+ bound must be specified.
473
+ * Otherwise the default bound is ` 'static ` .
472
474
473
475
``` rust,ignore
474
476
// For the following trait...
@@ -575,7 +577,14 @@ impl Printable for String {
575
577
576
578
The notation ` &self ` is a shorthand for ` self: &Self ` .
577
579
580
+ [ `Vec<T>` ] : ../std/vec/struct.Vec.html
581
+ [ dynamically sized type ] : dynamically-sized-types.html
578
582
[ dynamically sized types ] : dynamically-sized-types.html
579
583
[ RFC 599 ] : https://github.com/rust-lang/rfcs/blob/master/text/0599-default-object-bound.md
580
584
[ RFC 1156 ] : https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
581
- [ RFC 255 ] : https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
585
+ [ struct expression ] : expressions/struct-expr.html
586
+ [ closure expression ] : expressions/closure-expr.html
587
+ [ auto traits ] : special-types-and-traits.html#auto-traits
588
+ [ object safe ] : items/traits.html#object-safety
589
+ [ issue 47010 ] : https://github.com/rust-lang/rust/issues/47010
590
+ [ issue 33140 ] : https://github.com/rust-lang/rust/issues/33140
0 commit comments