re_types/datatypes/
dvec2d.rs1#![allow(unused_imports)]
5#![allow(unused_parens)]
6#![allow(clippy::clone_on_copy)]
7#![allow(clippy::cloned_instead_of_copied)]
8#![allow(clippy::map_flatten)]
9#![allow(clippy::needless_question_mark)]
10#![allow(clippy::new_without_default)]
11#![allow(clippy::redundant_closure)]
12#![allow(clippy::too_many_arguments)]
13#![allow(clippy::too_many_lines)]
14
15use ::re_types_core::try_serialize_field;
16use ::re_types_core::SerializationResult;
17use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
18use ::re_types_core::{ComponentDescriptor, ComponentName};
19use ::re_types_core::{DeserializationError, DeserializationResult};
20
21#[derive(Clone, Debug, Default, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
23#[repr(C)]
24pub struct DVec2D(pub [f64; 2usize]);
25
26::re_types_core::macros::impl_into_cow!(DVec2D);
27
28impl ::re_types_core::Loggable for DVec2D {
29 #[inline]
30 fn arrow_datatype() -> arrow::datatypes::DataType {
31 #![allow(clippy::wildcard_imports)]
32 use arrow::datatypes::*;
33 DataType::FixedSizeList(
34 std::sync::Arc::new(Field::new("item", DataType::Float64, false)),
35 2,
36 )
37 }
38
39 fn to_arrow_opt<'a>(
40 data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
41 ) -> SerializationResult<arrow::array::ArrayRef>
42 where
43 Self: Clone + 'a,
44 {
45 #![allow(clippy::wildcard_imports)]
46 #![allow(clippy::manual_is_variant_and)]
47 use ::re_types_core::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
48 use arrow::{array::*, buffer::*, datatypes::*};
49 Ok({
50 let (somes, data0): (Vec<_>, Vec<_>) = data
51 .into_iter()
52 .map(|datum| {
53 let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
54 let datum = datum.map(|datum| datum.into_owned().0);
55 (datum.is_some(), datum)
56 })
57 .unzip();
58 let data0_validity: Option<arrow::buffer::NullBuffer> = {
59 let any_nones = somes.iter().any(|some| !*some);
60 any_nones.then(|| somes.into())
61 };
62 {
63 let data0_inner_data: Vec<_> = data0
64 .into_iter()
65 .flat_map(|v| match v {
66 Some(v) => itertools::Either::Left(v.into_iter()),
67 None => itertools::Either::Right(
68 std::iter::repeat(Default::default()).take(2usize),
69 ),
70 })
71 .collect();
72 let data0_inner_validity: Option<arrow::buffer::NullBuffer> =
73 data0_validity.as_ref().map(|validity| {
74 validity
75 .iter()
76 .map(|b| std::iter::repeat(b).take(2usize))
77 .flatten()
78 .collect::<Vec<_>>()
79 .into()
80 });
81 as_array_ref(FixedSizeListArray::new(
82 std::sync::Arc::new(Field::new("item", DataType::Float64, false)),
83 2,
84 as_array_ref(PrimitiveArray::<Float64Type>::new(
85 ScalarBuffer::from(data0_inner_data.into_iter().collect::<Vec<_>>()),
86 data0_inner_validity,
87 )),
88 data0_validity,
89 ))
90 }
91 })
92 }
93
94 fn from_arrow_opt(
95 arrow_data: &dyn arrow::array::Array,
96 ) -> DeserializationResult<Vec<Option<Self>>>
97 where
98 Self: Sized,
99 {
100 #![allow(clippy::wildcard_imports)]
101 use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
102 use arrow::{array::*, buffer::*, datatypes::*};
103 Ok({
104 let arrow_data = arrow_data
105 .as_any()
106 .downcast_ref::<arrow::array::FixedSizeListArray>()
107 .ok_or_else(|| {
108 let expected = Self::arrow_datatype();
109 let actual = arrow_data.data_type().clone();
110 DeserializationError::datatype_mismatch(expected, actual)
111 })
112 .with_context("rerun.datatypes.DVec2D#xy")?;
113 if arrow_data.is_empty() {
114 Vec::new()
115 } else {
116 let offsets = (0..)
117 .step_by(2usize)
118 .zip((2usize..).step_by(2usize).take(arrow_data.len()));
119 let arrow_data_inner = {
120 let arrow_data_inner = &**arrow_data.values();
121 arrow_data_inner
122 .as_any()
123 .downcast_ref::<Float64Array>()
124 .ok_or_else(|| {
125 let expected = DataType::Float64;
126 let actual = arrow_data_inner.data_type().clone();
127 DeserializationError::datatype_mismatch(expected, actual)
128 })
129 .with_context("rerun.datatypes.DVec2D#xy")?
130 .into_iter()
131 .collect::<Vec<_>>()
132 };
133 ZipValidity::new_with_validity(offsets, arrow_data.nulls())
134 .map(|elem| {
135 elem.map(|(start, end): (usize, usize)| {
136 debug_assert!(end - start == 2usize);
137 if arrow_data_inner.len() < end {
138 return Err(DeserializationError::offset_slice_oob(
139 (start, end),
140 arrow_data_inner.len(),
141 ));
142 }
143
144 #[allow(unsafe_code, clippy::undocumented_unsafe_blocks)]
145 let data = unsafe { arrow_data_inner.get_unchecked(start..end) };
146 let data = data.iter().cloned().map(Option::unwrap_or_default);
147
148 #[allow(clippy::unwrap_used)]
150 Ok(array_init::from_iter(data).unwrap())
151 })
152 .transpose()
153 })
154 .collect::<DeserializationResult<Vec<Option<_>>>>()?
155 }
156 .into_iter()
157 }
158 .map(|v| v.ok_or_else(DeserializationError::missing_data))
159 .map(|res| res.map(|v| Some(Self(v))))
160 .collect::<DeserializationResult<Vec<Option<_>>>>()
161 .with_context("rerun.datatypes.DVec2D#xy")
162 .with_context("rerun.datatypes.DVec2D")?)
163 }
164
165 #[inline]
166 fn from_arrow(arrow_data: &dyn arrow::array::Array) -> DeserializationResult<Vec<Self>>
167 where
168 Self: Sized,
169 {
170 #![allow(clippy::wildcard_imports)]
171 use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
172 use arrow::{array::*, buffer::*, datatypes::*};
173 if let Some(nulls) = arrow_data.nulls() {
174 if nulls.null_count() != 0 {
175 return Err(DeserializationError::missing_data());
176 }
177 }
178 Ok({
179 let slice = {
180 let arrow_data = arrow_data
181 .as_any()
182 .downcast_ref::<arrow::array::FixedSizeListArray>()
183 .ok_or_else(|| {
184 let expected = DataType::FixedSizeList(
185 std::sync::Arc::new(Field::new("item", DataType::Float64, false)),
186 2,
187 );
188 let actual = arrow_data.data_type().clone();
189 DeserializationError::datatype_mismatch(expected, actual)
190 })
191 .with_context("rerun.datatypes.DVec2D#xy")?;
192 let arrow_data_inner = &**arrow_data.values();
193 bytemuck::cast_slice::<_, [_; 2usize]>(
194 arrow_data_inner
195 .as_any()
196 .downcast_ref::<Float64Array>()
197 .ok_or_else(|| {
198 let expected = DataType::Float64;
199 let actual = arrow_data_inner.data_type().clone();
200 DeserializationError::datatype_mismatch(expected, actual)
201 })
202 .with_context("rerun.datatypes.DVec2D#xy")?
203 .values()
204 .as_ref(),
205 )
206 };
207 {
208 slice.iter().copied().map(Self).collect::<Vec<_>>()
209 }
210 })
211 }
212}
213
214impl From<[f64; 2usize]> for DVec2D {
215 #[inline]
216 fn from(xy: [f64; 2usize]) -> Self {
217 Self(xy)
218 }
219}
220
221impl From<DVec2D> for [f64; 2usize] {
222 #[inline]
223 fn from(value: DVec2D) -> Self {
224 value.0
225 }
226}
227
228impl ::re_byte_size::SizeBytes for DVec2D {
229 #[inline]
230 fn heap_size_bytes(&self) -> u64 {
231 self.0.heap_size_bytes()
232 }
233
234 #[inline]
235 fn is_pod() -> bool {
236 <[f64; 2usize]>::is_pod()
237 }
238}