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