1use crate::attributes::{impl_custom_attribute_methods, CustomAttributes};
2use crate::{Reflect, TypePath, TypePathTable};
3use std::any::{Any, TypeId};
4use std::sync::Arc;
5
6#[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 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 #[cfg(feature = "documentation")]
32 pub fn with_docs(self, docs: Option<&'static str>) -> Self {
33 Self { docs, ..self }
34 }
35
36 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 pub fn name(&self) -> &'static str {
46 self.name
47 }
48
49 pub fn type_path_table(&self) -> &TypePathTable {
53 &self.type_path
54 }
55
56 pub fn type_path(&self) -> &'static str {
63 self.type_path_table().path()
64 }
65
66 pub fn type_id(&self) -> TypeId {
68 self.type_id
69 }
70
71 pub fn is<T: Any>(&self) -> bool {
73 TypeId::of::<T>() == self.type_id
74 }
75
76 #[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#[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 #[cfg(feature = "documentation")]
110 pub fn with_docs(self, docs: Option<&'static str>) -> Self {
111 Self { docs, ..self }
112 }
113
114 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 pub fn index(&self) -> usize {
124 self.index
125 }
126
127 pub fn type_path_table(&self) -> &TypePathTable {
131 &self.type_path
132 }
133
134 pub fn type_path(&self) -> &'static str {
141 self.type_path_table().path()
142 }
143
144 pub fn type_id(&self) -> TypeId {
146 self.type_id
147 }
148
149 pub fn is<T: Any>(&self) -> bool {
151 TypeId::of::<T>() == self.type_id
152 }
153
154 #[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}