-
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 const fn TypeId::matches for comparing type ids in consts #231
Comments
cc @eddyb who likes to scuttle all proposals relating to |
Uhm, as long as it's just (frankly I think such a |
I was working under the assumption that wasn't a possibility, but it may well be. I'm not really up-to-date on the status-quo of |
It would be possible, but the current implementation of the feature is very much in flux, so we'd like to avoid doing that in the near future. cc @fee1-dead |
A Another way would be to only allow desugared calls to |
From an end-user perspective that's at least already the case with many primitives so it doesn't seem like a big leap to allow some const equality to be exposed from std without also exposing that mechanism to end-users. It was also the case for const generics for a while so also isn't a surprising thing to do. If the libs-api team would prefer not to add a special case |
Given that we have https://rust-lang.github.io/rfcs/2316-safe-unsafe-trait-methods.html and https://rust-lang.github.io/rfcs/3245-refined-impls.html, I think a feature-gated way to allow putting (Actually exposing that to stable would, of course, need some sort of FCP or maybe more, but something like that in nightly seems entirely plausible to me, if it would be useful for people.) |
To be upfront, that's absolutely the goal of this proposal; to line things up so we could stabilize I want to work towards something we'd all be comfortable saying "we'd probably FCP that". |
I could certainly imagine FCPing the calling of |
We could implement a hacky solution for non-generic impls methods today without relevant tech debt or complexity. We could allow adding |
That sounds good to me. If we suddenly had a way to stabilize some methods on trait impls as Would that belong as a proposal in this repo here? Or is there a lang process I should follow? |
well, a list of things i think need
types that need
|
I think those |
can we mark |
We removed all const trait impls and |
Ah, I see the machinery for impl PartialEq for TypeId {
#[rustc_const_stable(..)]
fn eq(&self, other: &Self) -> bool { .. }
} so that all we guarantee is that |
Correct |
Is this more of a lang proposal then and I should peddle it through their process somewhere? Or is it ok here? If so, I'll update the proposal to reflect the |
I believe this is still entirely in the purview of the libs team. We may do an FCP of both teams on the first PR that adds this though. |
We discussed this just now in the libs-api meeting. We'd much prefer the regular |
A year later, it seems clear we're not close to having Given that, we're happy to see this as an unstable feature. As for the name, we think Feel free to open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. |
To make this method not quite so pointless when pub const fn matches<Other: 'static>(&self) -> bool {
self == TypeId::of::<Other>()
}
I'm not aware that anyone tried to push for the solution suggested above. So unsurprisingly it did not happen. |
+1 that maybe this exact signature isn't the best method to keep around long term. But I think maybe // Stealing the name `is_same::<T, U>()` from C++ `std::is_same<T, U>::value`
const fn is_same<A: 'static, B: 'static>() -> bool {
// or intrinsic call
TypeId::of::<A>().t == TypeId::of::<B>().t
} It seems like the goal at comptime is probably not often actually the comparison of Another benefit - not using |
That sounds reasonable to me. However, please open a new ACP if you want to propose a new API like that. Then we'll discuss it in the libs-api meeting. :) |
Thanks, I did so at #411 |
Proposal
Problem statement
To provide an API for comparing
TypeId
s inconst
contexts that can be stabilized "soon" along withTypeId::of
without depending onconst
in trait impls. This API should make stabilization ofTypeId
inconst
contexts uncontentious, but isn't interpreted as a commitment to actually do that stabilization.Motivating examples or use cases
TypeId
s can be used inconst
contexts as a limited form of specialization; they can be used to dispatch at compile-time based on the type of a value. As a real-world example, I have a library that allows capturing values using a standard trait, likefmt::Display
, but specializes internally when that value is a primitive like a string or integer:This pattern is useful when working with dynamic data such as when templating or wiring up loosely-coupled state. Unfortunately, it doesn't currently work even on
nightly
because structural-matching ofTypeId
s has been removed (for perfectly valid reasons). The only thing you can do with aTypeId
is compare it with otherTypeId
s, so without someconst
way to compare them,TypeId
is currently useless inconst
contexts.Solution sketch
This proposes
TypeId::matches
; an API for asserting twoTypeId
s match at compile-time that could be stabilized "soon" alongsideTypeId::of
to makeTypeId
s usable inconst
functions:It relies on the standard library having some kind of support for
const
equality, without exposing what that support is. It decouples this pattern of specialization fromconst
trait support or from full specialization.Our example from before becomes:
Alternatives
You could do this using plain-old
==
, but that needsconst
trait support, which is still in its design stages. When equality does become possible inconst
contexts, this method is simply a semantic alternative to it that's also a good place to document what the implications of twoTypeId
s being equal or not are.You could also use specialization, which isn't being actively pushed and has no clear path to stabilization.
You could use
Any::is
andAny::downcast_ref
for this, butAny
requiresSized + 'static
(Sized
for coercing todyn Any
and'static
from its trait bounds), which rules out unsized types likestr
.Links and related work
const fn
type_id
rust#77125TypeId
rust#103291What happens now?
This issue is part of the libs-api team API change proposal process. 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: