-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
TypeId implements_trait #151236
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
base: main
Are you sure you want to change the base?
TypeId implements_trait #151236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ mod projection; | |
| mod stack; | ||
| mod step; | ||
| mod traits; | ||
| mod util; | ||
| pub mod util; | ||
| mod validity; | ||
| mod visitor; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2846,6 +2846,24 @@ pub const unsafe fn size_of_val<T: ?Sized>(ptr: *const T) -> usize; | |
| #[rustc_intrinsic_const_stable_indirect] | ||
| pub const unsafe fn align_of_val<T: ?Sized>(ptr: *const T) -> usize; | ||
|
|
||
| /// Check if a type represented by a `TypeId` is a dyn Trait. | ||
| /// It can only be called at compile time, the backends do | ||
| /// not implement it. | ||
| #[rustc_intrinsic] | ||
| #[unstable(feature = "core_intrinsics", issue = "none")] | ||
| pub const fn type_id_is_trait(_trait: crate::any::TypeId) -> bool { | ||
| panic!("`TypeId::implements_trait_represented_by_type_id` can only be called at compile-time") | ||
| } | ||
|
Comment on lines
+2849
to
+2856
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This likely doesn't require a dedicated intrinsic. we can determine it through variants of |
||
|
|
||
| /// Check if a type represented by a `TypeId` implements a trait represented by a `TypeId`. | ||
| /// It can only be called at compile time, the backends do | ||
| /// not implement it. | ||
| #[rustc_intrinsic] | ||
| #[unstable(feature = "core_intrinsics", issue = "none")] | ||
| pub const fn type_id_implements_trait(_id: crate::any::TypeId, _trait: crate::any::TypeId) -> bool { | ||
| panic!("`TypeId::implements_trait` can only be called at compile-time") | ||
| } | ||
|
|
||
| /// Compute the type information of a concrete type. | ||
| /// It can only be called at compile time, the backends do | ||
| /// not implement it. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,8 @@ | |
| //! runtime or const-eval processable way. | ||
|
|
||
| use crate::any::TypeId; | ||
| use crate::intrinsics::type_of; | ||
| use crate::intrinsics::{type_id_implements_trait, type_id_is_trait, type_of}; | ||
| use crate::ptr; | ||
|
|
||
| /// Compile-time type information. | ||
| #[derive(Debug)] | ||
|
|
@@ -24,6 +25,30 @@ impl TypeId { | |
| pub const fn info(self) -> Type { | ||
| type_of(self) | ||
| } | ||
|
|
||
| /// Checks if the type represented by the `TypeId` implements the trait. | ||
| /// It can only be called at compile time. | ||
| pub const fn implements_trait< | ||
| T: ptr::Pointee<Metadata = ptr::DynMetadata<T>> + ?Sized + 'static, | ||
| >( | ||
| self, | ||
| ) -> bool { | ||
| type_id_implements_trait(self, TypeId::of::<T>()) | ||
| } | ||
|
|
||
| /// Checks if the type represented by the `TypeId` implements the trait represented by the secondary `TypeId`. | ||
| /// Returns `None` if the `trait_represented_by_type_id` is not a trait represented by type id. | ||
| /// It can only be called at compile time. | ||
| pub const fn implements_trait_represented_by_type_id( | ||
| self, | ||
| trait_represented_by_type_id: Self, | ||
| ) -> Option<bool> { | ||
| if type_id_is_trait(trait_represented_by_type_id) { | ||
| Some(type_id_implements_trait(self, trait_represented_by_type_id)) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
Comment on lines
+29
to
+51
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically, Regarding whether a struct implements a trait, I think this also relates to "reflection". Based on this, I would suggest that associating them with For naming, personally,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good points. I agree that |
||
| } | ||
|
|
||
| impl Type { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't looked closely at how
CompileTimeMachineinteracts withInterpCx, but I believe there's a better way to share code than putting it into theutilmod.