re_types_core/
wrapper_component.rs1use re_byte_size::SizeBytes;
2
3use crate::{Component, ComponentType, DeserializationResult, Loggable, SerializationResult};
4
5pub 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 type Datatype: Loggable + Sized;
20
21 fn name() -> ComponentType;
23
24 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 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 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}