use std::sync::Arc;
use crate::{
ComponentBatch, ComponentName, DeserializationResult, MaybeOwnedComponentBatch,
SerializationResult, _Backtrace,
};
#[allow(unused_imports)] use crate::{Component, Loggable, LoggableBatch};
pub trait Archetype {
type Indicator: 'static + ComponentBatch + Default;
fn name() -> ArchetypeName;
#[inline]
fn indicator() -> MaybeOwnedComponentBatch<'static> {
MaybeOwnedComponentBatch::Owned(Box::<<Self as Archetype>::Indicator>::default())
}
fn required_components() -> std::borrow::Cow<'static, [ComponentName]>;
#[inline]
fn recommended_components() -> std::borrow::Cow<'static, [ComponentName]> {
std::borrow::Cow::Owned(vec![Self::indicator().name()])
}
#[inline]
fn optional_components() -> std::borrow::Cow<'static, [ComponentName]> {
std::borrow::Cow::Borrowed(&[])
}
#[inline]
fn all_components() -> std::borrow::Cow<'static, [ComponentName]> {
[
Self::required_components().into_owned(),
Self::recommended_components().into_owned(),
Self::optional_components().into_owned(),
]
.into_iter()
.flatten()
.collect::<Vec<_>>()
.into()
}
#[inline]
fn from_arrow(
data: impl IntoIterator<Item = (arrow2::datatypes::Field, Box<dyn ::arrow2::array::Array>)>,
) -> DeserializationResult<Self>
where
Self: Sized,
{
Self::from_arrow_components(
data.into_iter()
.map(|(field, array)| (field.name.into(), array)),
)
}
#[inline]
fn from_arrow_components(
data: impl IntoIterator<Item = (ComponentName, Box<dyn ::arrow2::array::Array>)>,
) -> DeserializationResult<Self>
where
Self: Sized,
{
_ = data; Err(crate::DeserializationError::NotImplemented {
fqname: Self::name().to_string(),
backtrace: _Backtrace::new_unresolved(),
})
}
}
re_string_interner::declare_new_type!(
pub struct ArchetypeName;
);
impl ArchetypeName {
#[inline]
pub fn full_name(&self) -> &'static str {
self.0.as_str()
}
#[inline]
pub fn short_name(&self) -> &'static str {
let full_name = self.0.as_str();
if let Some(short_name) = full_name.strip_prefix("rerun.archetypes.") {
short_name
} else if let Some(short_name) = full_name.strip_prefix("rerun.blueprint.archetypes.") {
short_name
} else if let Some(short_name) = full_name.strip_prefix("rerun.") {
short_name
} else {
full_name
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct GenericIndicatorComponent<A: Archetype> {
_phantom: std::marker::PhantomData<A>,
}
impl<A: Archetype> GenericIndicatorComponent<A> {
pub const DEFAULT: Self = Self {
_phantom: std::marker::PhantomData::<A>,
};
}
impl<A: Archetype> Default for GenericIndicatorComponent<A> {
fn default() -> Self {
Self::DEFAULT
}
}
impl<A: Archetype> crate::LoggableBatch for GenericIndicatorComponent<A> {
type Name = ComponentName;
#[inline]
fn name(&self) -> Self::Name {
format!("{}Indicator", A::name().full_name())
.replace("archetypes", "components")
.into()
}
#[inline]
fn num_instances(&self) -> usize {
1
}
#[inline]
fn arrow_field(&self) -> arrow2::datatypes::Field {
let name = self.name().to_string();
arrow2::datatypes::Field::new(
name.clone(),
arrow2::datatypes::DataType::Extension(
name,
Arc::new(arrow2::datatypes::DataType::Null),
None,
),
false,
)
}
#[inline]
fn to_arrow(&self) -> SerializationResult<Box<dyn arrow2::array::Array>> {
Ok(
arrow2::array::NullArray::new(arrow2::datatypes::DataType::Null, self.num_instances())
.boxed(),
)
}
}
impl<A: Archetype> crate::ComponentBatch for GenericIndicatorComponent<A> {}
#[derive(Debug, Clone, Copy)]
pub struct NamedIndicatorComponent(pub ComponentName);
impl NamedIndicatorComponent {
#[inline]
pub fn as_batch(&self) -> MaybeOwnedComponentBatch<'_> {
MaybeOwnedComponentBatch::Ref(self)
}
#[inline]
pub fn to_batch(self) -> MaybeOwnedComponentBatch<'static> {
MaybeOwnedComponentBatch::Owned(Box::new(self))
}
}
impl crate::LoggableBatch for NamedIndicatorComponent {
type Name = ComponentName;
#[inline]
fn name(&self) -> Self::Name {
self.0
}
#[inline]
fn num_instances(&self) -> usize {
1
}
#[inline]
fn arrow_field(&self) -> arrow2::datatypes::Field {
let name = self.name().to_string();
arrow2::datatypes::Field::new(
name.clone(),
arrow2::datatypes::DataType::Extension(
name,
Arc::new(arrow2::datatypes::DataType::Null),
None,
),
false,
)
}
#[inline]
fn to_arrow(&self) -> SerializationResult<Box<dyn arrow2::array::Array>> {
Ok(
arrow2::array::NullArray::new(arrow2::datatypes::DataType::Null, self.num_instances())
.boxed(),
)
}
}
impl crate::ComponentBatch for NamedIndicatorComponent {}