-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ACP: add std::any::is_same_type::<T, U>() -> bool
#411
Comments
This is really mostly a counterproposal to #231, looking to address some of its limitations if this is to be a library feature. I do think that a lang feature allowing Cc @KodrAus since most |
ISTM most of the time one of the types involved is dynamic, a la |
I have encountered both types being statically known a few times e.g. pub enum Select<T, F> {
True(T),
False(F),
}
pub trait Dispatch {
type Type<const V: bool>: 'static;
}
pub fn select<T: Dispatch, const V: bool>(v: &mut T::Type<V>) -> Select<&mut T::Type<true>, &mut T::Type<false>> {
if V {
Select::True(<dyn Any>::downcast_mut(v).unwrap())
} else {
Select::False(<dyn Any>::downcast_mut(v).unwrap())
}
} |
I haven't seen any examples that seem to show this is the case in |
There's a crate castaway which has more generic API without having to specify the It uses a |
The typical example I was thinking of is code that handles |
We discussed this in the libs-api meeting. We don't think this method will be very useful in practice because you often need to deal with a dynamic One alternative that we discussed was a |
Proposal
Problem statement
#231 was recently accepted as a way to check if two types are the same in
const
contexts, providing a newTypeId::matches
. However, this API suffers from a few problems:TypeId
s. Whether or not we can provide a safeconst TypeId::of
is not well known at this time; if it is possible, it will likely be a while off. (Tracking Issue forconst fn
type_id
rust#77125, Collisions in type_id rust#10389)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
TypeId
s. This is currently true by default sinceconst 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:
#231 provides a more complete example.
Solution sketch
Rather than provide an interface that relies on
TypeId
s, 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 constTypeId
uncertainties.Usage is also simpler, which means it serves a simplification purpose even after const
PartialEq
.Alternatives
T == i32
.matches
from ACP: Add const fn TypeId::matches for comparing type ids in consts #231.Links and related work
std::is_same
.@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):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: