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
#231 was recently accepted as a way to check if two types are the same in const contexts, providing a new TypeId::matches. However, this API suffers from a few problems:
Its goal is to be useful at compile time, but it relies on being able to construct TypeIds. Whether or not we can provide a safe const TypeId::of is not well known at this time; if it is possible, it will likely be a while off. (Tracking Issue for const fntype_id rust#77125, Collisions in type_id rust#10389)
If we eventually get a const PartialEq, TypeId::matches will become a duplicate of a more natural API.
These two problems seem to provide significant barriers to eventual stabiliztion of TypeId::matches
Motivating examples or use cases
Most of the use cases for const type equality checks is comparing types that are available either as a generic or as a concrete type, rather than unknown TypeIds. This is currently true by default since const TypeId::of is not available, but seems likely to be the case more generally.
One use case of checking types for equality is to provide downcasting behavior:
use core::ptr;constfnconst_downcast<T,Expected>(value:&T) -> Option<&Expected>{if/* magic expression */{Some(unsafe{&*ptr::from_ref(value).cast::<Expected>()})}else{None}}
Rather than provide an interface that relies on TypeIds, provide a standalone function that takes the types as generic parameters:
// core::any/// Return `true` if `T` and `U` are the same type.constfnis_same_type<T:'static,U:'static>() -> bool{// or intrinsic callTypeId::of::<T>().t == TypeId::of::<U>().t}
Usage:
use core::{any, ptr};constfnconst_downcast<T,Expected>(value:&T) -> Option<&Expected>{if any::is_same_type::<T,Expected>(){Some(unsafe{&*ptr::from_ref(value).cast::<Expected>()})}else{None}}
This can be implemented using TypeId (as in this example), but it could also be implemented as an intrinsic. The intrinsic option allows hooking into compiler logic to provide the check, meaning const stability is not blocked on const TypeId uncertainties.
Usage is also simpler, which means it serves a simplification purpose even after const PartialEq.
Alternatives
A lang feature that allows comparing types directly such as T == i32.
Zig has @typeInfo for reflection, the result of which can be compared.
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
We think this problem seems worth solving, and the standard library might be the right place to solve it.
We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
Proposal
Problem statement
#231 was recently accepted as a way to check if two types are the same in
constcontexts, providing a newTypeId::matches. However, this API suffers from a few problems:TypeIds. Whether or not we can provide a safeconst TypeId::ofis not well known at this time; if it is possible, it will likely be a while off. (Tracking Issue forconst fntype_idrust#77125, Collisions in type_id rust#10389)PartialEq,TypeId::matcheswill become a duplicate of a more natural API.These two problems seem to provide significant barriers to eventual stabiliztion of
TypeId::matchesMotivating examples or use cases
Most of the use cases for const type equality checks is comparing types that are available either as a generic or as a concrete type, rather than unknown
TypeIds. This is currently true by default sinceconst TypeId::ofis not available, but seems likely to be the case more generally.One use case of checking types for equality is to provide downcasting behavior:
#231 provides a more complete example.
Solution sketch
Rather than provide an interface that relies on
TypeIds, provide a standalone function that takes the types as generic parameters:Usage:
This can be implemented using
TypeId(as in this example), but it could also be implemented as an intrinsic. The intrinsic option allows hooking into compiler logic to provide the check, meaning const stability is not blocked on constTypeIduncertainties.Usage is also simpler, which means it serves a simplification purpose even after const
PartialEq.Alternatives
T == i32.matchesfrom ACP: Add const fn TypeId::matches for comparing type ids in consts #231.Links and related work
std::is_same.@typeInfofor reflection, the result of which can be compared.What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution: