You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/type-coercions.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -186,7 +186,7 @@ r[coerce.unsize]
186
186
187
187
r[coerce.unsize.intro]
188
188
The following coercions are called "unsized coercions", since their targets contain an unsized type.
189
-
unsized 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:
189
+
Unsized 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
190
191
191
```rust
192
192
usestd::cell::Cell;
@@ -222,14 +222,13 @@ fn main() {
222
222
}
223
223
```
224
224
225
-
r[coerce.unsize.confusion]
226
225
> [!NOTE]
227
226
> The term "unsized" might be quite confusing, since the coercion works on sized types (pointers) and the source pointer might point to an unsized type in the first place (`&dyn A -> &dyn Super` in the example above).
228
227
>
229
228
> "unsized" refers to the main purpose of these coercions --- converting (pointers to) sized types to (pointers to) unsized types. The pointers being not the focus, since unsized types can't exist without them.
230
229
231
230
r[coerce.unsize.metadata]
232
-
When performing unsized coercion, the pointer metadata type changes. For example, when unsized `&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`.
231
+
When performing unsized coercion, the pointer metadata type changes. For example, when unsized `&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`.
233
232
234
233
r[coerce.unsize.traits]
235
234
Three internal traits, [`Unsize`], [`CoerceUnsized`], and [`PinCoerceUnsized`] are used to assist in this process and expose it for library use.
[`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>`.
242
241
243
242
r[coerce.unsize.traits.pin-coerce-unsized]
244
-
[`PinCoerceUnsized`] is an unsafe marker trait for pointer-like types unsized coercion of which does not break [`Pin`] guarantees. It is a requirement of the `CoerceUnsized` implementation for `Pin`. That is, `&D: PinCoerceUnsized` implies `Pin<&T>: CoerceUnsized<Pin<&U>>`.
243
+
[`PinCoerceUnsized`] is an unsafe marker trait for pointer-like types, unsized coercion of which does not break [`Pin`] guarantees. [`PinCoerceUnsized`] being implemented for the pointer is a requirement of the `CoerceUnsized` implementation for `Pin`. That is, `&D: PinCoerceUnsized` implies `Pin<&T>: CoerceUnsized<Pin<&U>>`.
245
244
245
+
r[coerce.unsize.built-in]
246
246
The following implementations of [`Unsize`] are built-in:
247
247
248
-
r[coerce.unsize.slice]
248
+
r[coerce.unsize.built-in.slice]
249
249
*`[T; n]: Unsize<[T]>`.
250
250
251
-
r[coerce.unsize.trait-object]
251
+
r[coerce.unsize.built-in.trait-object]
252
252
*`T: Unsize<dyn U>`, when `T` implements `U + Sized`, and `U` is [dyn compatible].
253
253
254
-
r[coerce.unsize.trait-upcast]
254
+
r[coerce.unsize.built-in.trait-upcast]
255
255
*`dyn Trait: Unsize<dyn Super>`, when `Super` is one of `Trait`'s [supertraits].
256
256
* This allows dropping auto traits, i.e. `dyn Trait + Auto` to `dyn Super` is allowed.
257
257
* This allows adding auto traits if the principal trait has the auto trait as a super trait, i.e. given `trait Trait: Super + Auto {}`, `dyn Trait` to `dyn Trait + Auto` or to `dyn Super + Auto` coercions are allowed.
258
258
259
-
r[coerce.unsize.composite]
259
+
r[coerce.unsize.built-in.composite]
260
260
*`S<T...>: Unsize<S<U...>>`, when:
261
261
*`S` is a struct.
262
262
* The type of the last field of `S<T...>` implements `Unsize<X>` where `X` is the type of the last field of `S<U...>`.
@@ -265,7 +265,7 @@ r[coerce.unsize.composite]
265
265
266
266
r[coerce.unsize.pointer]
267
267
A type `S<T...>`*can* implement `CoerceUnsized<S<U...>>` if
268
-
1. Only one field of `S<T...>` has different type than the same field of `S<U...>` (ignoring fields of type `PhantomData<_>`).
268
+
1. Only one field of `S<T...>` has a different type than the same field of `S<U...>` (ignoring fields of type `PhantomData<_>`).
269
269
2. ... and that field's type implements `CoerceUnsized<X>` where `X` is the type of the same field in `S<U,...>`.
270
270
271
271
This allows it to provide an unsized coercion to `S<U...>`.
0 commit comments