re_types_core/
wrapper_component.rs

1use re_byte_size::SizeBytes;
2
3use crate::{Component, ComponentType, DeserializationResult, Loggable, SerializationResult};
4
5/// Implementation helper for [`Component`]s that wrap a single [`Loggable`] datatype.
6///
7/// This should be almost all components with the exception of enum-components.
8pub trait WrapperComponent:
9    'static
10    + Send
11    + Sync
12    + Clone
13    + Sized
14    + SizeBytes
15    + From<Self::Datatype>
16    + std::ops::Deref<Target = Self::Datatype>
17{
18    /// The underlying [`Loggable`] datatype for this component.
19    type Datatype: Loggable + Sized;
20
21    /// The fully-qualified type of this component, e.g. `rerun.components.Position2D`.
22    fn name() -> ComponentType;
23
24    /// Unwraps the component into its underlying datatype.
25    fn into_inner(self) -> Self::Datatype;
26}
27
28impl<T: WrapperComponent> Component for T {
29    fn name() -> ComponentType {
30        Self::name()
31    }
32}
33
34impl<T: WrapperComponent> Loggable for T {
35    #[inline]
36    fn arrow_datatype() -> arrow::datatypes::DataType {
37        <Self as WrapperComponent>::Datatype::arrow_datatype()
38    }
39
40    // NOTE: Don't inline this, this gets _huge_.
41    fn to_arrow_opt<'a>(
42        data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
43    ) -> SerializationResult<arrow::array::ArrayRef>
44    where
45        Self: Clone + 'a,
46    {
47        <Self as WrapperComponent>::Datatype::to_arrow_opt(data.into_iter().map(|datum| {
48            datum.map(|datum| match datum.into() {
49                ::std::borrow::Cow::Borrowed(datum) => ::std::borrow::Cow::Borrowed(&**datum),
50                ::std::borrow::Cow::Owned(datum) => ::std::borrow::Cow::Owned(
51                    <Self as WrapperComponent>::Datatype::from(datum.into_inner()),
52                ),
53            })
54        }))
55    }
56
57    // NOTE: Don't inline this, this gets _huge_.
58    fn from_arrow_opt(
59        arrow_data: &dyn arrow::array::Array,
60    ) -> DeserializationResult<Vec<Option<Self>>>
61    where
62        Self: Sized,
63    {
64        <Self as WrapperComponent>::Datatype::from_arrow_opt(arrow_data)
65            .map(|v| v.into_iter().map(|v| v.map(Self::from)).collect())
66    }
67
68    #[inline]
69    fn from_arrow(arrow_data: &dyn arrow::array::Array) -> DeserializationResult<Vec<Self>>
70    where
71        Self: Sized,
72    {
73        <Self as WrapperComponent>::Datatype::from_arrow(arrow_data)
74            .map(|v| v.into_iter().map(Self::from).collect())
75    }
76}