bevy_reflect/
fields.rs

1use crate::{
2    attributes::{impl_custom_attribute_methods, CustomAttributes},
3    type_info::impl_type_methods,
4    MaybeTyped, PartialReflect, Type, TypeInfo, TypePath,
5};
6use alloc::sync::Arc;
7
8/// The named field of a reflected struct.
9#[derive(Clone, Debug)]
10pub struct NamedField {
11    name: &'static str,
12    type_info: fn() -> Option<&'static TypeInfo>,
13    ty: Type,
14    custom_attributes: Arc<CustomAttributes>,
15    #[cfg(feature = "documentation")]
16    docs: Option<&'static str>,
17}
18
19impl NamedField {
20    /// Create a new [`NamedField`].
21    pub fn new<T: PartialReflect + MaybeTyped + TypePath>(name: &'static str) -> Self {
22        Self {
23            name,
24            type_info: T::maybe_type_info,
25            ty: Type::of::<T>(),
26            custom_attributes: Arc::new(CustomAttributes::default()),
27            #[cfg(feature = "documentation")]
28            docs: None,
29        }
30    }
31
32    /// Sets the docstring for this field.
33    #[cfg(feature = "documentation")]
34    pub fn with_docs(self, docs: Option<&'static str>) -> Self {
35        Self { docs, ..self }
36    }
37
38    /// Sets the custom attributes for this field.
39    pub fn with_custom_attributes(self, custom_attributes: CustomAttributes) -> Self {
40        Self {
41            custom_attributes: Arc::new(custom_attributes),
42            ..self
43        }
44    }
45
46    /// The name of the field.
47    pub fn name(&self) -> &'static str {
48        self.name
49    }
50
51    /// The [`TypeInfo`] of the field.
52    ///
53    ///
54    /// Returns `None` if the field does not contain static type information,
55    /// such as for dynamic types.
56    pub fn type_info(&self) -> Option<&'static TypeInfo> {
57        (self.type_info)()
58    }
59
60    impl_type_methods!(ty);
61
62    /// The docstring of this field, if any.
63    #[cfg(feature = "documentation")]
64    pub fn docs(&self) -> Option<&'static str> {
65        self.docs
66    }
67
68    impl_custom_attribute_methods!(self.custom_attributes, "field");
69}
70
71/// The unnamed field of a reflected tuple or tuple struct.
72#[derive(Clone, Debug)]
73pub struct UnnamedField {
74    index: usize,
75    type_info: fn() -> Option<&'static TypeInfo>,
76    ty: Type,
77    custom_attributes: Arc<CustomAttributes>,
78    #[cfg(feature = "documentation")]
79    docs: Option<&'static str>,
80}
81
82impl UnnamedField {
83    pub fn new<T: PartialReflect + MaybeTyped + TypePath>(index: usize) -> Self {
84        Self {
85            index,
86            type_info: T::maybe_type_info,
87            ty: Type::of::<T>(),
88            custom_attributes: Arc::new(CustomAttributes::default()),
89            #[cfg(feature = "documentation")]
90            docs: None,
91        }
92    }
93
94    /// Sets the docstring for this field.
95    #[cfg(feature = "documentation")]
96    pub fn with_docs(self, docs: Option<&'static str>) -> Self {
97        Self { docs, ..self }
98    }
99
100    /// Sets the custom attributes for this field.
101    pub fn with_custom_attributes(self, custom_attributes: CustomAttributes) -> Self {
102        Self {
103            custom_attributes: Arc::new(custom_attributes),
104            ..self
105        }
106    }
107
108    /// Returns the index of the field.
109    pub fn index(&self) -> usize {
110        self.index
111    }
112
113    /// The [`TypeInfo`] of the field.
114    ///
115    ///
116    /// Returns `None` if the field does not contain static type information,
117    /// such as for dynamic types.
118    pub fn type_info(&self) -> Option<&'static TypeInfo> {
119        (self.type_info)()
120    }
121
122    impl_type_methods!(ty);
123
124    /// The docstring of this field, if any.
125    #[cfg(feature = "documentation")]
126    pub fn docs(&self) -> Option<&'static str> {
127        self.docs
128    }
129
130    impl_custom_attribute_methods!(self.custom_attributes, "field");
131}