@@ -451,4 +451,52 @@ impl TypeId {
451451 pub const fn fields ( self , variant_index : usize ) -> usize {
452452 intrinsics:: type_id_fields ( self , variant_index)
453453 }
454+
455+ /// Returns the field `TypeId` at the given index of the type represented by this `TypeId`.
456+ ///
457+ /// ```
458+ /// #![feature(type_info)]
459+ /// use std::any::TypeId;
460+ ///
461+ /// struct Point {
462+ /// x: u32,
463+ /// y: u32,
464+ /// }
465+ /// assert_eq!(const { TypeId::of::<Point>().field(0, 0) }, TypeId::of::<u32>());
466+ /// assert_eq!(const { TypeId::of::<Point>().field(0, 1) }, TypeId::of::<u32>());
467+ ///
468+ /// enum Enum {
469+ /// Unit,
470+ /// Tuple(u32, u64),
471+ /// Struct { x: u32, y: u32, z: String },
472+ /// }
473+ /// assert_eq!(const { TypeId::of::<Enum>().field(1, 0) }, TypeId::of::<u32>());
474+ /// assert_eq!(const { TypeId::of::<Enum>().field(2, 2) }, TypeId::of::<String>());
475+ /// ```
476+ ///
477+ /// Out-of-bounds indexing will be treated as a compile-time error.
478+ ///
479+ /// ```compile_fail,E0080
480+ /// # #![feature(type_info)]
481+ /// # use std::any::TypeId;
482+ /// #
483+ /// # struct Point {
484+ /// # x: u32,
485+ /// # y: u32,
486+ /// # }
487+ /// # enum Enum {
488+ /// # Unit,
489+ /// # Tuple(u32, u64),
490+ /// # Struct { x: u32, y: u32, z: String },
491+ /// # }
492+ /// const {
493+ /// _ = TypeId::of::<Point>().field(0, 2); // error: indexing out of bounds: the len is 2 but the index is 2
494+ /// _ = TypeId::of::<Enum>().field(2, 3); // error: indexing out of bounds: the len is 3 but the index is 3
495+ /// }
496+ /// ```
497+ #[ unstable( feature = "type_info" , issue = "146922" ) ]
498+ #[ rustc_const_unstable( feature = "type_info" , issue = "146922" ) ]
499+ pub const fn field ( self , variant_index : usize , field_index : usize ) -> TypeId {
500+ intrinsics:: type_id_field ( self , variant_index, field_index)
501+ }
454502}
0 commit comments