-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-unimplementedStatus: The feature has not been implemented.Status: The feature has not been implemented.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Description
Feature gate: #![feature(downcast_trait)]
This is a tracking issue for the downcast_trait
and downcast_trait_mut
functions.
This feature will let us try to turn any value into a dyn MyTrait
. If the value does not implement MyTrait
then we simply return None
so that the user can have some default functionality happen instead.
We expect to be able to remove many cases of specialization from the compiler and instead perform this kind of downcasting in the bodies of functions that were relying on specialization before.
A simple example could be
// Look ma, no `T: Debug`
fn downcast_debug_format<T: 'static>(t: &T) -> String {
match std::any::downcast_trait::<_, dyn Debug>(t) {
Some(d) => format!("{d:?}"),
None => "default".to_string()
}
}
Public API
pub const fn downcast_trait<
T: Any + 'static,
U: ptr::Pointee<Metadata = ptr::DynMetadata<U>> + ?Sized + 'static,
>(
t: &T,
) -> Option<&U>
pub const fn downcast_trait_mut<
T: Any + 'static,
U: ptr::Pointee<Metadata = ptr::DynMetadata<U>> + ?Sized + 'static,
>(
t: &mut T,
) -> Option<&mut U>
Steps / History
(Remember to update the S-tracking-*
label when checking boxes.)
- Implementation: [WIP] Add downcast_trait and downcast_trait_mut #144363
- Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- Where should these functions live?
Currently the implementations are in theany
module, which can be very confusing as thesedowncast
concepts are for statically known types, while thedowncast
concepts inAny
proper are for dynamic runtime types.
Footnotes
danielhenrymantilla, tim-blackbird, Scripter17, jakobhellermann and dimpolodanielhenrymantilla, cramertj, tim-blackbird and Scripter17
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-unimplementedStatus: The feature has not been implemented.Status: The feature has not been implemented.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.