Skip to main content

core/
field.rs

1//! Field Reflection
2
3use crate::marker::PhantomData;
4
5/// Field Representing Type
6#[unstable(feature = "field_representing_type_raw", issue = "none")]
7#[lang = "field_representing_type"]
8#[expect(missing_debug_implementations)]
9#[fundamental]
10pub struct FieldRepresentingType<T: ?Sized, const VARIANT: u32, const FIELD: u32> {
11    _phantom: PhantomData<T>,
12}
13
14// SAFETY: `FieldRepresentingType` doesn't contain any `T`
15unsafe impl<T: ?Sized, const VARIANT: u32, const FIELD: u32> Send
16    for FieldRepresentingType<T, VARIANT, FIELD>
17{
18}
19
20// SAFETY: `FieldRepresentingType` doesn't contain any `T`
21unsafe impl<T: ?Sized, const VARIANT: u32, const FIELD: u32> Sync
22    for FieldRepresentingType<T, VARIANT, FIELD>
23{
24}
25
26impl<T: ?Sized, const VARIANT: u32, const FIELD: u32> Copy
27    for FieldRepresentingType<T, VARIANT, FIELD>
28{
29}
30
31impl<T: ?Sized, const VARIANT: u32, const FIELD: u32> Clone
32    for FieldRepresentingType<T, VARIANT, FIELD>
33{
34    fn clone(&self) -> Self {
35        *self
36    }
37}
38
39/// Expands to the field representing type of the given field.
40///
41/// The container type may be a tuple, `struct`, `union` or `enum`. In the case of an enum, the
42/// variant must also be specified. Only a single field is supported.
43#[unstable(feature = "field_projections", issue = "145383")]
44#[allow_internal_unstable(field_representing_type_raw, builtin_syntax)]
45// NOTE: when stabilizing this macro, we can never add new trait impls for `FieldRepresentingType`,
46// since it is `#[fundamental]` and thus could break users of this macro, since the compiler expands
47// it to `FieldRepresentingType<...>`. Thus stabilizing this requires careful thought about the
48// completeness of the trait impls for `FieldRepresentingType`.
49pub macro field_of($Container:ty, $($fields:expr)+ $(,)?) {
50    builtin # field_of($Container, $($fields)+)
51}
52
53/// Type representing a field of a `struct`, `union`, `enum` variant or tuple.
54///
55/// # Safety
56///
57/// Given a valid value of type `Self::Base`, there exists a valid value of type `Self::Type` at
58/// byte offset `OFFSET`
59#[lang = "field"]
60#[unstable(feature = "field_projections", issue = "145383")]
61#[rustc_deny_explicit_impl]
62#[rustc_dyn_incompatible_trait]
63// NOTE: the compiler provides the impl of `Field` for `FieldRepresentingType` when it can guarantee
64// the safety requirements of this trait. It also has to manually add the correct trait bounds on
65// associated types (and the `Self` type). Thus any changes to the bounds here must be reflected in
66// the old and new trait solver:
67// - `fn assemble_candidates_for_field_trait` in
68//   `compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs`, and
69// - `fn consider_builtin_field_candidate` in
70//   `compiler/rustc_next_trait_solver/src/solve/trait_goals.rs`.
71pub unsafe trait Field: Send + Sync + Copy {
72    /// The type of the base where this field exists in.
73    #[lang = "field_base"]
74    type Base;
75
76    /// The type of the field.
77    #[lang = "field_type"]
78    type Type;
79
80    /// The offset of the field in bytes.
81    #[lang = "field_offset"]
82    const OFFSET: usize = crate::intrinsics::field_offset::<Self>();
83}