Skip to content

Commit 07e844f

Browse files
committed
Add more docs for CoerceUnsized and Unsize
1 parent 75f5981 commit 07e844f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/doc/nomicon/coercions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Coercion is allowed between the following types:
1717
* `&T` to `*const T`
1818
* `&mut T` to `*mut T`
1919
* Unsizing: `T` to `U` if `T` implements `CoerceUnsized<U>`
20+
* Deref coercion: Expression `&x` of type `&T` to `&*x` of type `&U` if `T` derefs to `U` (i.e. `T: Deref<Target=U>`)
2021

2122
`CoerceUnsized<Pointer<U>> for Pointer<T> where T: Unsize<U>` is implemented
2223
for all pointer types (including smart pointers like Box and Rc). Unsize is
@@ -27,8 +28,9 @@ only implemented automatically, and enables the following transformations:
2728
* `Foo<..., T, ...>` => `Foo<..., U, ...>` where:
2829
* `T: Unsize<U>`
2930
* `Foo` is a struct
30-
* Only the last field of `Foo` has type `T`
31+
* Only the last field of `Foo` has type involving `T`
3132
* `T` is not part of the type of any other fields
33+
* `Bar<T>: Unsize<Bar<U>>`, if the last field of `Foo` has type `Bar<T>`
3234

3335
Coercions occur at a *coercion site*. Any location that is explicitly typed
3436
will cause a coercion to its type. If inference is necessary, the coercion will

src/libcore/marker.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,26 @@ pub trait Sized {
100100
///
101101
/// All implementations of `Unsize` are provided automatically by the compiler.
102102
///
103+
/// `Unsize` is implemented for:
104+
///
105+
/// - `[T; N]` is `Unsize<[T]>`
106+
/// - `T` is `Unsize<Trait>` when `T: Trait`
107+
/// - `Foo<..., T, ...>` is `Unsize<Foo<..., U, ...>>` if:
108+
/// - `T: Unsize<U>`
109+
/// - Foo is a struct
110+
/// - Only the last field of `Foo` has a type involving `T`
111+
/// - `T` is not part of the type of any other fields
112+
/// - `Bar<T>: Unsize<Bar<U>>`, if the last field of `Foo` has type `Bar<T>`
113+
///
103114
/// `Unsize` is used along with [`ops::CoerceUnsized`][coerceunsized] to allow
104115
/// "user-defined" containers such as [`rc::Rc`][rc] to contain dynamically-sized
105-
/// types. See the [DST coercion RFC][RFC982] for more details.
116+
/// types. See the [DST coercion RFC][RFC982] and [the nomicon entry on coercion][nomicon-coerce]
117+
/// for more details.
106118
///
107119
/// [coerceunsized]: ../ops/trait.CoerceUnsized.html
108120
/// [rc]: ../../std/rc/struct.Rc.html
109121
/// [RFC982]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md
122+
110123
#[unstable(feature = "unsize", issue = "27732")]
111124
#[lang="unsize"]
112125
pub trait Unsize<T: ?Sized> {

src/libcore/ops.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,6 +2649,35 @@ mod impls {
26492649

26502650
/// Trait that indicates that this is a pointer or a wrapper for one,
26512651
/// 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
26522681
#[unstable(feature = "coerce_unsized", issue = "27732")]
26532682
#[lang="coerce_unsized"]
26542683
pub trait CoerceUnsized<T> {

0 commit comments

Comments
 (0)