bevy_reflect/
fields.rs

1use crate::attributes::{impl_custom_attribute_methods, CustomAttributes};
2use crate::{Reflect, TypePath, TypePathTable};
3use std::any::{Any, TypeId};
4use std::sync::Arc;
5
6/// The named field of a reflected struct.
7#[derive(Clone, Debug)]
8pub struct NamedField {
9    name: &'static str,
10    type_path: TypePathTable,
11    type_id: TypeId,
12    custom_attributes: Arc<CustomAttributes>,
13    #[cfg(feature = "documentation")]
14    docs: Option<&'static str>,
15}
16
17impl NamedField {
18    /// Create a new [`NamedField`].
19    pub fn new<T: Reflect + TypePath>(name: &'static str) -> Self {
20        Self {
21            name,
22            type_path: TypePathTable::of::<T>(),
23            type_id: TypeId::of::<T>(),
24            custom_attributes: Arc::new(CustomAttributes::default()),
25            #[cfg(feature = "documentation")]
26            docs: None,
27        }
28    }
29
30    /// Sets the docstring for this field.
31    #[cfg(feature = "documentation")]
32    pub fn with_docs(self, docs: Option<&'static str>) -> Self {
33        Self { docs, ..self }
34    }
35
36    /// Sets the custom attributes for this field.
37    pub fn with_custom_attributes(self, custom_attributes: CustomAttributes) -> Self {
38        Self {
39            custom_attributes: Arc::new(custom_attributes),
40            ..self
41        }
42    }
43
44    /// The name of the field.
45    pub fn name(&self) -> &'static str {
46        self.name
47    }
48
49    /// A representation of the type path of the field.
50    ///
51    /// Provides dynamic access to all methods on [`TypePath`].
52    pub fn type_path_table(&self) -> &TypePathTable {
53        &self.type_path
54    }
55
56    /// The [stable, full type path] of the field.
57    ///
58    /// Use [`type_path_table`] if you need access to the other methods on [`TypePath`].
59    ///
60    /// [stable, full type path]: TypePath
61    /// [`type_path_table`]: Self::type_path_table
62    pub fn type_path(&self) -> &'static str {
63        self.type_path_table().path()
64    }
65
66    /// The [`TypeId`] of the field.
67    pub fn type_id(&self) -> TypeId {
68        self.type_id
69    }
70
71    /// Check if the given type matches the field type.
72    pub fn is<T: Any>(&self) -> bool {
73        TypeId::of::<T>() == self.type_id
74    }
75
76    /// The docstring of this field, if any.
77    #[cfg(feature = "documentation")]
78    pub fn docs(&self) -> Option<&'static str> {
79        self.docs
80    }
81
82    impl_custom_attribute_methods!(self.custom_attributes, "field");
83}
84
85/// The unnamed field of a reflected tuple or tuple struct.
86#[derive(Clone, Debug)]
87pub struct UnnamedField {
88    index: usize,
89    type_path: TypePathTable,
90    type_id: TypeId,
91    custom_attributes: Arc<CustomAttributes>,
92    #[cfg(feature = "documentation")]
93    docs: Option<&'static str>,
94}
95
96impl UnnamedField {
97    pub fn new<T: Reflect + TypePath>(index: usize) -> Self {
98        Self {
99            index,
100            type_path: TypePathTable::of::<T>(),
101            type_id: TypeId::of::<T>(),
102            custom_attributes: Arc::new(CustomAttributes::default()),
103            #[cfg(feature = "documentation")]
104            docs: None,
105        }
106    }
107
108    /// Sets the docstring for this field.
109    #[cfg(feature = "documentation")]
110    pub fn with_docs(self, docs: Option<&'static str>) -> Self {
111        Self { docs, ..self }
112    }
113
114    /// Sets the custom attributes for this field.
115    pub fn with_custom_attributes(self, custom_attributes: CustomAttributes) -> Self {
116        Self {
117            custom_attributes: Arc::new(custom_attributes),
118            ..self
119        }
120    }
121
122    /// Returns the index of the field.
123    pub fn index(&self) -> usize {
124        self.index
125    }
126
127    /// A representation of the type path of the field.
128    ///
129    /// Provides dynamic access to all methods on [`TypePath`].
130    pub fn type_path_table(&self) -> &TypePathTable {
131        &self.type_path
132    }
133
134    /// The [stable, full type path] of the field.
135    ///
136    /// Use [`type_path_table`] if you need access to the other methods on [`TypePath`].
137    ///
138    /// [stable, full type path]: TypePath
139    /// [`type_path_table`]: Self::type_path_table
140    pub fn type_path(&self) -> &'static str {
141        self.type_path_table().path()
142    }
143
144    /// The [`TypeId`] of the field.
145    pub fn type_id(&self) -> TypeId {
146        self.type_id
147    }
148
149    /// Check if the given type matches the field type.
150    pub fn is<T: Any>(&self) -> bool {
151        TypeId::of::<T>() == self.type_id
152    }
153
154    /// The docstring of this field, if any.
155    #[cfg(feature = "documentation")]
156    pub fn docs(&self) -> Option<&'static str> {
157        self.docs
158    }
159
160    impl_custom_attribute_methods!(self.custom_attributes, "field");
161}