Skip to content

Commit d83ea1f

Browse files
committed
refactor unsizing coercion documentation
The old one was quite confusing and also incorrect in a few places.
1 parent 2837b28 commit d83ea1f

File tree

1 file changed

+161
-37
lines changed

1 file changed

+161
-37
lines changed

src/type-coercions.md

Lines changed: 161 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ r[coerce.types.ref-to-pointer]
134134
r[coerce.types.mut-to-pointer]
135135
* `&mut T` to `*mut T`
136136

137+
r[coerce.types.unsize]
138+
* `T` to `U` if `T: CoerceUnsized<U>`. For example:
139+
```rust
140+
const _: &dyn std::fmt::Display = &0u8; // &u8 -> &dyn Display
141+
const _: &[u32] = &[0, 1, 2, 3, 4, 5]; // &[u32; 4] -> &[u32]
142+
```
143+
144+
See [unsizing coercion](#unsizing-coercions) for more details.
145+
137146
r[coerce.types.deref]
138147
* `&T` or `&mut T` to `&U` if `T` implements `Deref<Target = U>`. For example:
139148

@@ -163,20 +172,6 @@ r[coerce.types.deref]
163172
r[coerce.types.deref-mut]
164173
* `&mut T` to `&mut U` if `T` implements `DerefMut<Target = U>`.
165174

166-
r[coerce.types.unsize]
167-
* TyCtor(`T`) to TyCtor(`U`), where TyCtor(`T`) is one of
168-
- `&T`
169-
- `&mut T`
170-
- `*const T`
171-
- `*mut T`
172-
- `Box<T>`
173-
174-
and where `U` can be obtained from `T` by [unsized coercion](#unsized-coercions).
175-
176-
<!--In the future, coerce_inner will be recursively extended to tuples and
177-
structs. In addition, coercions from subtraits to supertraits will be
178-
added. See [RFC 401] for more details.-->
179-
180175
r[coerce.types.fn]
181176
* Function item types to `fn` pointers
182177

@@ -187,45 +182,172 @@ r[coerce.types.never]
187182
* `!` to any `T`
188183

189184
r[coerce.unsize]
190-
### Unsized Coercions
185+
### Unsizing Coercions
191186

192187
r[coerce.unsize.intro]
193-
The following coercions are called `unsized coercions`, since they
194-
relate to converting types to unsized types, and are permitted in a few
195-
cases where other coercions are not, as described above. They can still happen
196-
anywhere else a coercion can occur.
188+
The following coercions are called "Unsizing coercions", since their targets contain an unsized type.
189+
Unsizing coercions apply to pointer-like types which point to types which can lose some of their compile-time known information (such as size or implemented traits). For example:
190+
191+
```rust
192+
fn main() {
193+
// `&[u8; 0]` can be coerced to `&[u8]`.
194+
//
195+
// here `&_` is the pointer-like type,
196+
// `[u8; 0]` is the original pointee,
197+
// and `[u8]` is more erased pointee (it lost the length information).
198+
let _: &[u8] = &[];
199+
200+
trait A: Super {}
201+
impl A for () {}
202+
203+
trait Super {}
204+
impl Super for () {}
205+
206+
// `&()` can be coerced to `&dyn A`, losing the type information.
207+
let a: &dyn A = &();
208+
209+
// `&dyn A` can be coerced to `&dyn Super`,
210+
// loosing the fact that the underlying type (unit) implements `A` too.
211+
let _: &dyn Super = a;
212+
213+
// The same coercions work with other pointer-like types and wrappers over them:
214+
let _: Box<[u8]> = Box::<[u8; 0]>::new([]);
215+
let _: Cell<Box<[u8]>> = Cell::new(Box::<[u8; 0]>::new([]));
216+
217+
// The result of the coercion doesn't *have* to be the same pointer-like type,
218+
// alhtough this is only allowed for certain pairs of pointer-like types.
219+
let _: *const dyn A = &mut ();
220+
}
221+
```
197222

198-
r[coerce.unsize.trait]
199-
Two traits, [`Unsize`] and [`CoerceUnsized`], are used
200-
to assist in this process and expose it for library use. The following
201-
coercions are built-ins and, if `T` can be coerced to `U` with one of them, then
202-
an implementation of `Unsize<U>` for `T` will be provided:
223+
r[coerce.unsize.confusion]
224+
> [!NOTE]
225+
> The term "unsizing" might be quite confusing,
226+
> since the coercion works on sized types (pointers) and
227+
> the source pointer might point to an unsized type in the first place
228+
> (`&dyn A -> &dyn Super` in the example above).
229+
>
230+
> "Unsizing" refers to the main purpose of these coercions —
231+
> converting (pointers to) sized types to (pointers to) unsized types.
232+
> The pointers being not the focus, since unsized types can't exist without them.
233+
234+
r[coerce.unsize.metadata]
235+
When performing unsizing coercion, the pointer metadata type changes. For example, when unsizing `&u32` to `&dyn Debug` metadate type changes from `()` to `DynMetadata<dyn Debug>` (note that exact metadata types are not yet stable). This can also lead to a change in the pointer size -- `&u32` is half the size of `&dyn Debug`.
236+
237+
r[coerce.unsize.traits]
238+
Three traits, [`Unsize`], [`CoerceUnsized`], and [`PinCoerceUnsized`] are used to assist in this process and expose it for library use.
239+
240+
r[coerce.unsize.traits.unsize]
241+
[`Unsize`] represents the fact that the target type is layout compatible with the source type and the pointer metadata of the target type can be derived from the metadata of the source, meaning that a pointer to the source type can be converted to a pointer to the target type. For example `[T; N]` implements `Unsize<[T]>` meaning that you can *unsize* former into the later, allowing coercions such as `&[T; N] -> &[T]`.
242+
243+
r[coerce.unsize.traits.coerce-unsized]
244+
[`CoerceUnsized`] represents the fact that a pointer-like type can be coerced to another pointer-like type, due to `Unsize` being implemented for their pointees. For example, `&T` implements `CoerceUnsized<&U>` when `T: Unsize<U>`.
245+
246+
r[coerce.unsize.traits.pin-coerce-unsized]
247+
[`PinCoerceUnsized`] is an unsafe marker trait for pointer-like types unsizing coercion of which does not break [`Pin`] guarantees.
248+
It is a requirement of the [`CoerceUnsized` implementation for `Pin`][coerce.unsize.coerce-unsized-impls.pin-pin]. That is, `&D: PinCoerceUnsized` implies `Pin<&T>: CoerceUnsized<Pin<&U>>`.
249+
250+
The following implementations of [`Unsize`] are built-in:
203251

204252
r[coerce.unsize.slice]
205-
* `[T; n]` to `[T]`.
253+
* `[T; n]: Unsize<[T]>`.
206254

207255
r[coerce.unsize.trait-object]
208-
* `T` to `dyn U`, when `T` implements `U + Sized`, and `U` is [dyn compatible].
256+
* `T: Unsize<dyn U>`, when `T` implements `U + Sized`, and `U` is [dyn compatible].
209257

210258
r[coerce.unsize.trait-upcast]
211-
* `dyn T` to `dyn U`, when `U` is one of `T`'s [supertraits].
259+
* `dyn T: Unsize<dyn U>`, when `U` is one of `T`'s [supertraits].
212260
* This allows dropping auto traits, i.e. `dyn T + Auto` to `dyn U` is allowed.
213261
* This allows adding auto traits if the principal trait has the auto trait as a super trait, i.e. given `trait T: U + Send {}`, `dyn T` to `dyn T + Send` or to `dyn U + Send` coercions are allowed.
214262

215-
r[coerce.unsized.composite]
216-
* `Foo<..., T, ...>` to `Foo<..., U, ...>`, when:
217-
* `Foo` is a struct.
263+
r[coerce.unsize.composite]
264+
* `S<..., T, ...>: Unsize<S<..., U, ...>>`, when:
265+
* `S` is a struct.
218266
* `T` implements `Unsize<U>`.
219-
* The last field of `Foo` has a type involving `T`.
220-
* If that field has type `Bar<T>`, then `Bar<T>` implements `Unsize<Bar<U>>`.
221-
* T is not part of the type of any other fields.
267+
* The last field of `S` has a type involving `T`. i.e. it's either of `T` or `C<..., T, ...>` where `C` is a type constructor and `T` is only present in it once (`C<T, T>` is disallowed).
268+
* The last field is the *only* field which type involves `T`.
269+
* The type of the last field implements `Unsize<F>` where `F` is the same type with `T` replaced by `U`. i.e. if the field has type `Bar<T>`, then `Bar<T>` implements `Unsize<Bar<U>>`.
222270

223-
r[coerce.unsized.pointer]
271+
r[coerce.unsize.pointer]
224272
Additionally, a type `Foo<T>` can implement `CoerceUnsized<Foo<U>>` when `T`
225-
implements `Unsize<U>` or `CoerceUnsized<Foo<U>>`. This allows it to provide an
226-
unsized coercion to `Foo<U>`.
273+
implements `Unsize<U>` or `CoerceUnsized<U>`. This allows it to provide an
274+
unsized coercion to `Foo<U>`. FIXME: are there requirements for `CoerceUnsized`?
275+
276+
r[coerce.unsize.coerce-unsized-impls]
277+
Types which currently implement `CoerceUnsized<_>` (assuming `T: Unsize<U>`, `'a: 'b`, `A: CoerceUnsized<B>`, `Al: Allocator`):
278+
279+
r[coerce.unsize.coerce-unsized-impls.ref-ref]
280+
* `&'a T: CoerceUnsized<&'b U>`
281+
282+
r[coerce.unsize.coerce-unsized-impls.refmut-ref]
283+
* `&'a mut T: CoerceUnsized<&'b U>`
284+
285+
r[coerce.unsize.coerce-unsized-impls.ref-constptr]
286+
* `&T: CoerceUnsized<*const U>`
287+
288+
r[coerce.unsize.coerce-unsized-impls.refmut-constptr]
289+
* `&mut T: CoerceUnsized<*const U>`
290+
291+
r[coerce.unsize.coerce-unsized-impls.refmut-mutptr]
292+
* `&mut T: CoerceUnsized<*mut U>`
293+
294+
r[coerce.unsize.coerce-unsized-impls.refmut-refmut]
295+
* `&'a mut T: CoerceUnsized<&'a mut U>`
296+
* Note: the lifetimes of source and target must match, which is different from the impls where the target is a [shared reference][type.pointer.reference.shared].
297+
298+
r[coerce.unsize.coerce-unsized-impls.cellref-cellref]
299+
* `core::cell::Ref<'a, A>: CoerceUnsized<core::cell::Ref<'a, B>>`
300+
301+
r[coerce.unsize.coerce-unsized-impls.cellrefmut-cellrefmut]
302+
* `core::cell::RefMut<'a, A>: CoerceUnsized<core::cell::RefMut<'a, B>>`
303+
304+
r[coerce.unsize.coerce-unsized-impls.pin-pin]
305+
* `Pin<A>: CoerceUnsized<Pin<B>>` where `A: PinCoerceUnsized, B: PinCoerceUnsized`
306+
307+
r[coerce.unsize.coerce-unsized-impls.constptr-constptr]
308+
* `*const T: CoerceUnsized<*const U>`
309+
310+
r[coerce.unsize.coerce-unsized-impls.mutptr-constptr]
311+
* `*mut T: CoerceUnsized<*const U>`
312+
313+
r[coerce.unsize.coerce-unsized-impls.mutptr-mutptr]
314+
* `*mut T: CoerceUnsized<*mut U>`
315+
316+
r[coerce.unsize.coerce-unsized-impls.cell-cell]
317+
* `core::cell::Cell<A>: CoerceUnsized<core::cell::Cell<B>>`
318+
319+
r[coerce.unsize.coerce-unsized-impls.refcell-refcell]
320+
* `RefCell<A>: CoerceUnsized<RefCell<B>>`
321+
322+
r[coerce.unsize.coerce-unsized-impls.syncunsafecell-syncunsafecell]
323+
* `SyncUnsafeCell<A>: CoerceUnsized<SyncUnsafeCell<B>>`
324+
325+
r[coerce.unsize.coerce-unsized-impls.unsafecell-unsafecell]
326+
* `UnsafeCell<A>: CoerceUnsized<UnsafeCell<B>>`
327+
328+
r[coerce.unsize.coerce-unsized-impls.nonnull-nonnull]
329+
* `NonNull<T>: CoerceUnsized<NonNull<U>>`
330+
331+
r[coerce.unsize.coerce-unsized-impls.box-box]
332+
* `Box<T, Al>: CoerceUnsized<Box<U, Al>>`
333+
334+
r[coerce.unsize.coerce-unsized-impls.rc-rc]
335+
* `Rc<T, Al>: CoerceUnsized<Rc<U, Al>>`
336+
337+
r[coerce.unsize.coerce-unsized-impls.uniquerc-unieuqrc]
338+
* `UniqueRc<T, Al>: CoerceUnsized<UniqueRc<U, Al>>`
339+
340+
r[coerce.unsize.coerce-unsized-impls.rcweak-rcweak]
341+
* `alloc::rc::Weak<T, Al>: CoerceUnsized<alloc::rc::Weak<U, Al>>`
342+
343+
r[coerce.unsize.coerce-unsized-impls.arc-arc]
344+
* `Arc<T, Al>: CoerceUnsized<Arc<U, Al>>`
345+
346+
r[coerce.unsize.coerce-unsized-impls.arcweak-arcweak]
347+
* `alloc::sync::Weak<T, Al>: CoerceUnsized<alloc::sync::Weak<U, Al>>`
227348

228-
> Note: While the definition of the unsized coercions and their implementation
349+
> [!NOTE]
350+
> While the definition of the unsized coercions and their implementation
229351
> has been stabilized, the traits themselves are not yet stable and therefore
230352
> can't be used directly in stable Rust.
231353
@@ -324,6 +446,8 @@ precisely.
324446
[subtype]: subtyping.md
325447
[dyn compatible]: items/traits.md#dyn-compatibility
326448
[type cast operator]: expressions/operator-expr.md#type-cast-expressions
449+
[`Pin`]: std::pin::Pin
450+
[`PinCoerceUnsized`]: std::pin::PinCoerceUnsized
327451
[`Unsize`]: std::marker::Unsize
328452
[`CoerceUnsized`]: std::ops::CoerceUnsized
329453
[method-call expressions]: expressions/method-call-expr.md

0 commit comments

Comments
 (0)