re_types/datatypes/
vec4d.rs1#![allow(unused_braces)]
5#![allow(unused_imports)]
6#![allow(unused_parens)]
7#![allow(clippy::allow_attributes)]
8#![allow(clippy::clone_on_copy)]
9#![allow(clippy::cloned_instead_of_copied)]
10#![allow(clippy::map_flatten)]
11#![allow(clippy::needless_question_mark)]
12#![allow(clippy::new_without_default)]
13#![allow(clippy::redundant_closure)]
14#![allow(clippy::too_many_arguments)]
15#![allow(clippy::too_many_lines)]
16#![allow(clippy::wildcard_imports)]
17
18use ::re_types_core::SerializationResult;
19use ::re_types_core::try_serialize_field;
20use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
21use ::re_types_core::{ComponentDescriptor, ComponentType};
22use ::re_types_core::{DeserializationError, DeserializationResult};
23
24#[derive(Clone, Debug, Default, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
26#[repr(C)]
27pub struct Vec4D(pub [f32; 4usize]);
28
29::re_types_core::macros::impl_into_cow!(Vec4D);
30
31impl ::re_types_core::Loggable for Vec4D {
32 #[inline]
33 fn arrow_datatype() -> arrow::datatypes::DataType {
34 use arrow::datatypes::*;
35 DataType::FixedSizeList(
36 std::sync::Arc::new(Field::new("item", DataType::Float32, false)),
37 4,
38 )
39 }
40
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 #![allow(clippy::manual_is_variant_and)]
48 use ::re_types_core::{Loggable as _, ResultExt as _, arrow_helpers::as_array_ref};
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(std::iter::repeat_n(
69 Default::default(),
70 4usize,
71 )),
72 })
73 .collect();
74 let data0_inner_validity: Option<arrow::buffer::NullBuffer> =
75 data0_validity.as_ref().map(|validity| {
76 validity
77 .iter()
78 .map(|b| std::iter::repeat_n(b, 4usize))
79 .flatten()
80 .collect::<Vec<_>>()
81 .into()
82 });
83 as_array_ref(FixedSizeListArray::new(
84 std::sync::Arc::new(Field::new("item", DataType::Float32, false)),
85 4,
86 as_array_ref(PrimitiveArray::<Float32Type>::new(
87 ScalarBuffer::from(data0_inner_data.into_iter().collect::<Vec<_>>()),
88 data0_inner_validity,
89 )),
90 data0_validity,
91 ))
92 }
93 })
94 }
95
96 fn from_arrow_opt(
97 arrow_data: &dyn arrow::array::Array,
98 ) -> DeserializationResult<Vec<Option<Self>>>
99 where
100 Self: Sized,
101 {
102 use ::re_types_core::{Loggable as _, ResultExt as _, arrow_zip_validity::ZipValidity};
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.Vec4D#xyzw")?;
114 if arrow_data.is_empty() {
115 Vec::new()
116 } else {
117 let offsets = (0..)
118 .step_by(4usize)
119 .zip((4usize..).step_by(4usize).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::<Float32Array>()
125 .ok_or_else(|| {
126 let expected = DataType::Float32;
127 let actual = arrow_data_inner.data_type().clone();
128 DeserializationError::datatype_mismatch(expected, actual)
129 })
130 .with_context("rerun.datatypes.Vec4D#xyzw")?
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 == 4usize);
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 #[expect(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 #[expect(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.Vec4D#xyzw")
163 .with_context("rerun.datatypes.Vec4D")?)
164 }
165
166 #[inline]
167 fn from_arrow(arrow_data: &dyn arrow::array::Array) -> DeserializationResult<Vec<Self>>
168 where
169 Self: Sized,
170 {
171 use ::re_types_core::{Loggable as _, ResultExt as _, arrow_zip_validity::ZipValidity};
172 use arrow::{array::*, buffer::*, datatypes::*};
173 if let Some(nulls) = arrow_data.nulls()
174 && nulls.null_count() != 0
175 {
176 return Err(DeserializationError::missing_data());
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::Float32, false)),
186 4,
187 );
188 let actual = arrow_data.data_type().clone();
189 DeserializationError::datatype_mismatch(expected, actual)
190 })
191 .with_context("rerun.datatypes.Vec4D#xyzw")?;
192 let arrow_data_inner = &**arrow_data.values();
193 bytemuck::cast_slice::<_, [_; 4usize]>(
194 arrow_data_inner
195 .as_any()
196 .downcast_ref::<Float32Array>()
197 .ok_or_else(|| {
198 let expected = DataType::Float32;
199 let actual = arrow_data_inner.data_type().clone();
200 DeserializationError::datatype_mismatch(expected, actual)
201 })
202 .with_context("rerun.datatypes.Vec4D#xyzw")?
203 .values()
204 .as_ref(),
205 )
206 };
207 { slice.iter().copied().map(Self).collect::<Vec<_>>() }
208 })
209 }
210}
211
212impl From<[f32; 4usize]> for Vec4D {
213 #[inline]
214 fn from(xyzw: [f32; 4usize]) -> Self {
215 Self(xyzw)
216 }
217}
218
219impl From<Vec4D> for [f32; 4usize] {
220 #[inline]
221 fn from(value: Vec4D) -> Self {
222 value.0
223 }
224}
225
226impl ::re_byte_size::SizeBytes for Vec4D {
227 #[inline]
228 fn heap_size_bytes(&self) -> u64 {
229 self.0.heap_size_bytes()
230 }
231
232 #[inline]
233 fn is_pod() -> bool {
234 <[f32; 4usize]>::is_pod()
235 }
236}