Stabilize derive(CoercePointee)
, RFC 3621
#133862
Labels
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
cc @traviscross @Darksonn
Tracked by #123430
Stabilization: #133820
What is being proposed for stabilization
This stabilization report concerns the macro specified by RFC3621,
now called
CoercePointee
.This macro enables the derivation of trait implementation of
Unsize
,CoerceUnsize
andDispatchFromDyn
traits, both of which are unstable at the moment, under a well-defined and structured schema, so that the users who would like to allow their custom type to admit unsizing coercion and use ofdyn
DST in one of its generic type parameter in a safe way. The greatest use case of this macro is to allow users to equip their types with this behaviour without dependence onalloc
crate, or whenalloc
is not available and a custom implementation of pointer-like data structure in the likes ofRc
andArc
is highly desirable. The most prominent example is thekernel
crate by Rust-for-Linux, which would greatly benefit from this macro due to reduction of boilerplate, and reduce the project's dependence on unstable features behindUnsize
,CoerceUnsize
andDispatchFromDyn
.In a nutshell,
derive(CoercePointee)
derives the implementation ofCoerceUnsize
andDispatchFromDyn
traits on the targetstruct
definition with all the necessary trait bounds. It identifies one generic type variable in the generic list of the targetstruct
, that is either uniquely annotated with the#[pointee]
attribute or is the only type variable among the generics. This identified generic, which is called source type in this document, will be treated as the target of unsizing operation anddyn
trait object dispatch. Correspondingly, the resultant type after the unsizing coercion will be called target type in this document. In case additional trait bounds applies on the source type, the said trait bounds will be migrated to both the source type and the target type in the trait implementation.Out of necessity of unsizing coercion, the macro requires the
struct
to adopt therepr(transparent)
layout. The only data field in thestruct
definition, which is required by this layout, should also implementCoerceUnsize
andDispatchFromDyn
at the moment, or at least conform to requirement of the unsizing coercion and trait object dispatching protocol.As an example, here is how the generated code would look like after expanding the macro. The following is a user source that invokes this macro.
This is the expansion result. The source type
T
after the unsizing coercion is assumed to be__S
and the trait bounds requested by the original definition are migrated for__S
as well throughtout.Future interaction
This stabilization of this macro does not require the stabilization of any of the unstable traits mentioned. This macro aims at stabilising a subset of features thereof, which has widely proliferated in the ecosystem, without blocking further development of DSTs and unsizing coercion. In case of change in the design of these language features, as long as basic provision of the coercion operation remains the same, the macro should be updated to use the new facility inside its implementation when necessary, to preserve only the semantics and capabilities that this macro exposes users to.
The text was updated successfully, but these errors were encountered: