@@ -2649,6 +2649,35 @@ mod impls {
2649
2649
2650
2650
/// Trait that indicates that this is a pointer or a wrapper for one,
2651
2651
/// where unsizing can be performed on the pointee.
2652
+ ///
2653
+ /// See the [DST coercion RfC][dst-coerce] and [the nomicon entry on coercion][nomicon-coerce]
2654
+ /// for more details.
2655
+ ///
2656
+ /// For builtin pointer types, pointers to `T` will coerce to pointers to `U` if `T: Unsize<U>`
2657
+ /// by converting from a thin pointer to a fat pointer.
2658
+ ///
2659
+ /// For custom types, the coercion here works by coercing `Foo<T>` to `Foo<U>`
2660
+ /// provided an impl of `CoerceUnsized<Foo<U>> for Foo<T>` exists.
2661
+ /// Such an impl can only be written if `Foo<T>` has only a single non-phantomdata
2662
+ /// field involving `T`. If the type of that field is `Bar<T>`, an implementation
2663
+ /// of `CoerceUnsized<Bar<U>> for Bar<T>` must exist. The coercion will work by
2664
+ /// by coercing the `Bar<T>` field into `Bar<U>` and filling in the rest of the fields
2665
+ /// from `Foo<T>` to create a `Foo<U>`. This will effectively drill down to a pointer
2666
+ /// field and coerce that.
2667
+ ///
2668
+ /// Generally, for smart pointers you will implement
2669
+ /// `CoerceUnsized<Ptr<U>> for Ptr<T> where T: Unsize<U>, U: ?Sized`, with an
2670
+ /// optional `?Sized` bound on `T` itself. For wrapper types that directly embed `T`
2671
+ /// like `Cell<T>` and `RefCell<T>`, you
2672
+ /// can directly implement `CoerceUnsized<Wrap<U>> for Wrap<T> where T: CoerceUnsized<U>`.
2673
+ /// This will let coercions of types like `Cell<Box<T>>` work.
2674
+ ///
2675
+ /// [`Unsize`][unsize] is used to mark types which can be coerced to DSTs if behind
2676
+ /// pointers. It is implemented automatically by the compiler.
2677
+ ///
2678
+ /// [dst-coerce]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md
2679
+ /// [unsize]: ../marker/trait.Unsize.html
2680
+ /// [nomicon-coerce]: ../../nomicon/coercions.html
2652
2681
#[ unstable( feature = "coerce_unsized" , issue = "27732" ) ]
2653
2682
#[ lang="coerce_unsized" ]
2654
2683
pub trait CoerceUnsized < T > {
0 commit comments