re_types_core/
loggable_batch.rsuse std::borrow::Cow;
use crate::{Component, ComponentDescriptor, ComponentName, Loggable, SerializationResult};
use arrow2::array::ListArray as Arrow2ListArray;
#[allow(unused_imports)] use crate::Archetype;
pub trait LoggableBatch {
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>>;
}
pub trait ComponentBatch: LoggableBatch {
fn to_arrow_list_array(&self) -> SerializationResult<Arrow2ListArray<i32>> {
let array = self.to_arrow2()?;
let offsets =
arrow2::offset::Offsets::try_from_lengths(std::iter::repeat(1).take(array.len()))?;
let data_type = Arrow2ListArray::<i32>::default_datatype(array.data_type().clone());
Arrow2ListArray::<i32>::try_new(data_type, offsets.into(), array.to_boxed(), None)
.map_err(|err| err.into())
}
fn descriptor(&self) -> Cow<'_, ComponentDescriptor>;
fn with_descriptor(
&self,
descriptor: ComponentDescriptor,
) -> ComponentBatchCowWithDescriptor<'_>
where
Self: Sized,
{
ComponentBatchCowWithDescriptor::new(ComponentBatchCow::Ref(self as &dyn ComponentBatch))
.with_descriptor_override(descriptor)
}
#[inline]
fn name(&self) -> ComponentName {
self.descriptor().component_name
}
}
pub struct ComponentBatchCowWithDescriptor<'a> {
pub batch: ComponentBatchCow<'a>,
pub descriptor_override: Option<ComponentDescriptor>,
}
impl<'a> From<ComponentBatchCow<'a>> for ComponentBatchCowWithDescriptor<'a> {
#[inline]
fn from(batch: ComponentBatchCow<'a>) -> Self {
Self::new(batch)
}
}
impl<'a> ComponentBatchCowWithDescriptor<'a> {
#[inline]
pub fn new(batch: impl Into<ComponentBatchCow<'a>>) -> Self {
Self {
batch: batch.into(),
descriptor_override: None,
}
}
#[inline]
pub fn with_descriptor_override(self, descriptor: ComponentDescriptor) -> Self {
Self {
descriptor_override: Some(descriptor),
..self
}
}
}
impl LoggableBatch for ComponentBatchCowWithDescriptor<'_> {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
self.batch.to_arrow2()
}
}
impl<'a> ComponentBatch for ComponentBatchCowWithDescriptor<'a> {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
self.descriptor_override
.as_ref()
.map(Into::into)
.unwrap_or_else(|| self.batch.descriptor())
}
#[inline]
fn name(&self) -> ComponentName {
self.batch.name()
}
}
pub enum ComponentBatchCow<'a> {
Owned(Box<dyn ComponentBatch>),
Ref(&'a dyn ComponentBatch),
}
impl<'a> From<&'a dyn ComponentBatch> for ComponentBatchCow<'a> {
#[inline]
fn from(comp_batch: &'a dyn ComponentBatch) -> Self {
Self::Ref(comp_batch)
}
}
impl From<Box<dyn ComponentBatch>> for ComponentBatchCow<'_> {
#[inline]
fn from(comp_batch: Box<dyn ComponentBatch>) -> Self {
Self::Owned(comp_batch)
}
}
impl<'a> std::ops::Deref for ComponentBatchCow<'a> {
type Target = dyn ComponentBatch + 'a;
#[inline]
fn deref(&self) -> &(dyn ComponentBatch + 'a) {
match self {
ComponentBatchCow::Owned(this) => &**this,
ComponentBatchCow::Ref(this) => *this,
}
}
}
impl<'a> LoggableBatch for ComponentBatchCow<'a> {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
(**self).to_arrow2()
}
}
impl<'a> ComponentBatch for ComponentBatchCow<'a> {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
(**self).descriptor()
}
}
impl<L: Clone + Loggable> LoggableBatch for L {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2([std::borrow::Cow::Borrowed(self)])
}
}
impl<C: Component> ComponentBatch for C {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}
impl<L: Clone + Loggable> LoggableBatch for Option<L> {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2(self.iter().map(|v| std::borrow::Cow::Borrowed(v)))
}
}
impl<C: Component> ComponentBatch for Option<C> {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}
impl<L: Clone + Loggable> LoggableBatch for Vec<L> {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2(self.iter().map(|v| std::borrow::Cow::Borrowed(v)))
}
}
impl<C: Component> ComponentBatch for Vec<C> {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}
impl<L: Loggable> LoggableBatch for Vec<Option<L>> {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2_opt(
self.iter()
.map(|opt| opt.as_ref().map(|v| std::borrow::Cow::Borrowed(v))),
)
}
}
impl<C: Component> ComponentBatch for Vec<Option<C>> {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}
impl<L: Loggable, const N: usize> LoggableBatch for [L; N] {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2(self.iter().map(|v| std::borrow::Cow::Borrowed(v)))
}
}
impl<C: Component, const N: usize> ComponentBatch for [C; N] {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}
impl<L: Loggable, const N: usize> LoggableBatch for [Option<L>; N] {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2_opt(
self.iter()
.map(|opt| opt.as_ref().map(|v| std::borrow::Cow::Borrowed(v))),
)
}
}
impl<C: Component, const N: usize> ComponentBatch for [Option<C>; N] {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}
impl<L: Loggable> LoggableBatch for &[L] {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2(self.iter().map(|v| std::borrow::Cow::Borrowed(v)))
}
}
impl<C: Component> ComponentBatch for &[C] {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}
impl<L: Loggable> LoggableBatch for &[Option<L>] {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2_opt(
self.iter()
.map(|opt| opt.as_ref().map(|v| std::borrow::Cow::Borrowed(v))),
)
}
}
impl<C: Component> ComponentBatch for &[Option<C>] {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}
impl<L: Loggable, const N: usize> LoggableBatch for &[L; N] {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2(self.iter().map(|v| std::borrow::Cow::Borrowed(v)))
}
}
impl<C: Component, const N: usize> ComponentBatch for &[C; N] {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}
impl<L: Loggable, const N: usize> LoggableBatch for &[Option<L>; N] {
#[inline]
fn to_arrow2(&self) -> SerializationResult<Box<dyn ::arrow2::array::Array>> {
L::to_arrow2_opt(
self.iter()
.map(|opt| opt.as_ref().map(|v| std::borrow::Cow::Borrowed(v))),
)
}
}
impl<C: Component, const N: usize> ComponentBatch for &[Option<C>; N] {
#[inline]
fn descriptor(&self) -> Cow<'_, ComponentDescriptor> {
C::descriptor().into()
}
}