re_types_core/
archetype.rs1use std::{borrow::Cow, sync::Arc};
2
3use crate::{
4 ComponentBatch, ComponentDescriptor, ComponentName, DeserializationResult, SerializationResult,
5 SerializedComponentBatch, _Backtrace,
6};
7
8#[expect(unused_imports, clippy::unused_trait_names)] use crate::{Component, Loggable, LoggableBatch};
10
11pub trait Archetype {
23 type Indicator: 'static + ComponentBatch + Default;
41
42 fn name() -> ArchetypeName;
44
45 fn display_name() -> &'static str;
47
48 fn indicator() -> SerializedComponentBatch;
54
55 fn required_components() -> std::borrow::Cow<'static, [ComponentDescriptor]>;
57
58 #[inline]
60 fn recommended_components() -> std::borrow::Cow<'static, [ComponentDescriptor]> {
61 std::borrow::Cow::Owned(vec![Self::indicator().descriptor.clone()])
62 }
63
64 #[inline]
66 fn optional_components() -> std::borrow::Cow<'static, [ComponentDescriptor]> {
67 std::borrow::Cow::Borrowed(&[])
68 }
69
70 #[inline]
78 fn all_components() -> std::borrow::Cow<'static, [ComponentDescriptor]> {
79 [
80 Self::required_components().into_owned(),
81 Self::recommended_components().into_owned(),
82 Self::optional_components().into_owned(),
83 ]
84 .into_iter()
85 .flatten()
86 .collect::<Vec<_>>()
87 .into()
88 }
89
90 #[inline]
98 fn from_arrow(
99 data: impl IntoIterator<Item = (arrow::datatypes::Field, ::arrow::array::ArrayRef)>,
100 ) -> DeserializationResult<Self>
101 where
102 Self: Sized,
103 {
104 Self::from_arrow_components(
105 data.into_iter()
106 .map(|(field, array)| (ComponentDescriptor::from(field), array)),
107 )
108 }
109
110 #[inline]
116 fn from_arrow_components(
117 data: impl IntoIterator<Item = (ComponentDescriptor, ::arrow::array::ArrayRef)>,
118 ) -> DeserializationResult<Self>
119 where
120 Self: Sized,
121 {
122 _ = data; Err(crate::DeserializationError::NotImplemented {
124 fqname: Self::name().to_string(),
125 backtrace: _Backtrace::new_unresolved(),
126 })
127 }
128}
129
130pub trait ArchetypeReflectionMarker {}
132
133re_string_interner::declare_new_type!(
136 #[cfg_attr(feature = "serde", derive(::serde::Deserialize, ::serde::Serialize))]
138 pub struct ArchetypeName;
139);
140
141impl ArchetypeName {
142 #[inline]
144 #[track_caller]
145 pub fn sanity_check(&self) {
146 let full_name = self.0.as_str();
147 debug_assert!(
148 !full_name.starts_with("rerun.archetypes.rerun.archetypes.")
149 && !full_name.contains(':'),
150 "DEBUG ASSERT: Found archetype with full name {full_name:?}. Maybe some bad round-tripping?"
151 );
152 }
153
154 #[inline]
158 pub fn full_name(&self) -> &'static str {
159 self.sanity_check();
160 self.0.as_str()
161 }
162
163 #[inline]
172 pub fn short_name(&self) -> &'static str {
173 self.sanity_check();
174 let full_name = self.0.as_str();
175 if let Some(short_name) = full_name.strip_prefix("rerun.archetypes.") {
176 short_name
177 } else if let Some(short_name) = full_name.strip_prefix("rerun.blueprint.archetypes.") {
178 short_name
179 } else if let Some(short_name) = full_name.strip_prefix("rerun.") {
180 short_name
181 } else {
182 full_name
183 }
184 }
185}
186
187impl re_byte_size::SizeBytes for ArchetypeName {
188 #[inline]
189 fn heap_size_bytes(&self) -> u64 {
190 0
191 }
192}
193
194re_string_interner::declare_new_type!(
197 #[cfg_attr(feature = "serde", derive(::serde::Deserialize, ::serde::Serialize))]
199 pub struct ArchetypeFieldName;
200);
201
202impl re_byte_size::SizeBytes for ArchetypeFieldName {
203 #[inline]
204 fn heap_size_bytes(&self) -> u64 {
205 0
206 }
207}
208
209#[derive(Debug, Clone, Copy)]
219pub struct GenericIndicatorComponent<A: Archetype> {
220 _phantom: std::marker::PhantomData<A>,
221}
222
223impl<A: Archetype> GenericIndicatorComponent<A> {
224 pub const DEFAULT: Self = Self {
225 _phantom: std::marker::PhantomData::<A>,
226 };
227
228 #[inline]
233 pub fn new_array(len: usize) -> GenericIndicatorComponentArray<A> {
234 GenericIndicatorComponentArray {
235 len,
236 _phantom: std::marker::PhantomData::<A>,
237 }
238 }
239}
240
241impl<A: Archetype> Default for GenericIndicatorComponent<A> {
242 fn default() -> Self {
243 Self::DEFAULT
244 }
245}
246
247impl<A: Archetype> crate::LoggableBatch for GenericIndicatorComponent<A> {
248 #[inline]
249 fn to_arrow(&self) -> SerializationResult<arrow::array::ArrayRef> {
250 Ok(Arc::new(arrow::array::NullArray::new(1)))
251 }
252}
253
254impl<A: Archetype> crate::ComponentBatch for GenericIndicatorComponent<A> {
255 #[inline]
256 fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
257 let component_name =
258 format!("{}Indicator", A::name().full_name()).replace("archetypes", "components");
259 ComponentDescriptor::new(component_name).into()
260 }
261}
262
263#[derive(Debug, Clone, Copy)]
272pub struct GenericIndicatorComponentArray<A: Archetype> {
273 len: usize,
274 _phantom: std::marker::PhantomData<A>,
275}
276
277impl<A: Archetype> crate::LoggableBatch for GenericIndicatorComponentArray<A> {
278 #[inline]
279 fn to_arrow(&self) -> SerializationResult<arrow::array::ArrayRef> {
280 Ok(Arc::new(arrow::array::NullArray::new(self.len)))
281 }
282}
283
284impl<A: Archetype> crate::ComponentBatch for GenericIndicatorComponentArray<A> {
285 #[inline]
286 fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
287 ComponentDescriptor::new(GenericIndicatorComponent::<A>::DEFAULT.name()).into()
288 }
289}
290
291#[derive(Debug, Clone, Copy)]
297pub struct NamedIndicatorComponent(pub ComponentName);
298
299impl crate::LoggableBatch for NamedIndicatorComponent {
300 #[inline]
301 fn to_arrow(&self) -> SerializationResult<arrow::array::ArrayRef> {
302 Ok(Arc::new(arrow::array::NullArray::new(1)))
303 }
304}
305
306impl crate::ComponentBatch for NamedIndicatorComponent {
307 #[inline]
308 fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
309 ComponentDescriptor::new(self.0).into()
310 }
311}