use crate::{result::_Backtrace, DeserializationResult, ResultExt as _, SerializationResult};
#[allow(unused_imports)] use crate::{Archetype, ComponentBatch, DatatypeBatch, LoggableBatch};
pub trait Loggable: Clone + Sized {
type Name: std::fmt::Display;
fn name() -> Self::Name;
fn arrow_datatype() -> arrow2::datatypes::DataType;
fn to_arrow_opt<'a>(
data: impl IntoIterator<Item = Option<impl Into<std::borrow::Cow<'a, Self>>>>,
) -> SerializationResult<Box<dyn ::arrow2::array::Array>>
where
Self: 'a;
#[inline]
fn extended_arrow_datatype() -> arrow2::datatypes::DataType {
arrow2::datatypes::DataType::Extension(
Self::name().to_string(),
Box::new(Self::arrow_datatype()),
None,
)
}
#[inline]
fn arrow_field() -> arrow2::datatypes::Field {
arrow2::datatypes::Field::new(
Self::name().to_string(),
Self::extended_arrow_datatype(),
false,
)
}
#[inline]
fn to_arrow<'a>(
data: impl IntoIterator<Item = impl Into<std::borrow::Cow<'a, Self>>>,
) -> SerializationResult<Box<dyn ::arrow2::array::Array>>
where
Self: 'a,
{
re_tracing::profile_function!();
Self::to_arrow_opt(data.into_iter().map(Some))
}
#[inline]
fn from_arrow(data: &dyn ::arrow2::array::Array) -> DeserializationResult<Vec<Self>> {
re_tracing::profile_function!();
Self::from_arrow_opt(data)?
.into_iter()
.map(|opt| {
opt.ok_or_else(|| crate::DeserializationError::MissingData {
backtrace: _Backtrace::new_unresolved(),
})
})
.collect::<DeserializationResult<Vec<_>>>()
.with_context(Self::name().to_string())
}
fn from_arrow_opt(
data: &dyn ::arrow2::array::Array,
) -> DeserializationResult<Vec<Option<Self>>> {
_ = data; Err(crate::DeserializationError::NotImplemented {
fqname: Self::name().to_string(),
backtrace: _Backtrace::new_unresolved(),
})
}
}
pub trait Datatype: Loggable<Name = DatatypeName> {}
impl<L: Loggable<Name = DatatypeName>> Datatype for L {}
pub trait Component: Loggable<Name = ComponentName> {}
impl<L: Loggable<Name = ComponentName>> Component for L {}
pub type ComponentNameSet = std::collections::BTreeSet<ComponentName>;
re_string_interner::declare_new_type!(
pub struct ComponentName;
);
impl ComponentName {
#[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.components.") {
short_name
} else if let Some(short_name) = full_name.strip_prefix("rerun.") {
short_name
} else {
full_name
}
}
pub fn is_indicator_component(&self) -> bool {
self.starts_with("rerun.components.") && self.ends_with("Indicator")
}
pub fn indicator_component_archetype(&self) -> Option<String> {
if let Some(name) = self.strip_prefix("rerun.components.") {
if let Some(name) = name.strip_suffix("Indicator") {
return Some(name.to_owned());
}
}
None
}
}
impl crate::SizeBytes for ComponentName {
#[inline]
fn heap_size_bytes(&self) -> u64 {
0
}
}
re_string_interner::declare_new_type!(
pub struct DatatypeName;
);
impl DatatypeName {
#[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.datatypes.") {
short_name
} else if let Some(short_name) = full_name.strip_prefix("rerun.") {
short_name
} else {
full_name
}
}
}
impl crate::SizeBytes for DatatypeName {
#[inline]
fn heap_size_bytes(&self) -> u64 {
0
}
}