Skip to content

Commit d49c692

Browse files
authored
Rollup merge of rust-lang#91587 - nrc:dispatchfromdyn-docs, r=yaahc
core::ops::unsize: improve docs for DispatchFromDyn Docs-only PR, improves documentation for DispatchFromDyn.
2 parents 2b681ac + b3573c5 commit d49c692

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

library/core/src/ops/unsize.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,38 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
6868
#[unstable(feature = "coerce_unsized", issue = "27732")]
6969
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
7070

71-
/// This is used for object safety, to check that a method's receiver type can be dispatched on.
71+
/// `DispatchFromDyn` is used in the implementation of object safety checks (specifically allowing
72+
/// arbitrary self types), to guarantee that a method's receiver type can be dispatched on.
73+
///
74+
/// Note: `DispatchFromDyn` was briefly named `CoerceSized` (and had a slightly different
75+
/// interpretation).
76+
///
77+
/// Imagine we have a trait object `t` with type `&dyn Tr`, where `Tr` is some trait with a method
78+
/// `m` defined as `fn m(&self);`. When calling `t.m()`, the receiver `t` is a wide pointer, but an
79+
/// implementation of `m` will expect a narrow pointer as `&self` (a reference to the concrete
80+
/// type). The compiler must generate an implicit conversion from the trait object/wide pointer to
81+
/// the concrete reference/narrow pointer. Implementing `DispatchFromDyn` indicates that that
82+
/// conversion is allowed and thus that the type implementing `DispatchFromDyn` is safe to use as
83+
/// the self type in an object-safe method. (in the above example, the compiler will require
84+
/// `DispatchFromDyn` is implemented for `&'a U`).
85+
///
86+
/// `DispatchFromDyn` does not specify the conversion from wide pointer to narrow pointer; the
87+
/// conversion is hard-wired into the compiler. For the conversion to work, the following
88+
/// properties must hold (i.e., it is only safe to implement `DispatchFromDyn` for types which have
89+
/// these properties, these are also checked by the compiler):
90+
///
91+
/// * EITHER `Self` and `T` are either both references or both raw pointers; in either case, with
92+
/// the same mutability.
93+
/// * OR, all of the following hold
94+
/// - `Self` and `T` must have the same type constructor, and only vary in a single type parameter
95+
/// formal (the *coerced type*, e.g., `impl DispatchFromDyn<Rc<T>> for Rc<U>` is ok and the
96+
/// single type parameter (instantiated with `T` or `U`) is the coerced type,
97+
/// `impl DispatchFromDyn<Arc<T>> for Rc<U>` is not ok).
98+
/// - The definition for `Self` must be a struct.
99+
/// - The definition for `Self` must not be `#[repr(packed)]` or `#[repr(C)]`.
100+
/// - Other than one-aligned, zero-sized fields, the definition for `Self` must have exactly one
101+
/// field and that field's type must be the coerced type. Furthermore, `Self`'s field type must
102+
/// implement `DispatchFromDyn<F>` where `F` is the type of `T`'s field type.
72103
///
73104
/// An example implementation of the trait:
74105
///

0 commit comments

Comments
 (0)