re_types/datatypes/
vec3d.rs

1// DO NOT EDIT! This file was auto-generated by crates/build/re_types_builder/src/codegen/rust/api.rs
2// Based on "crates/store/re_types/definitions/rerun/datatypes/vec3d.fbs".
3
4#![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/// **Datatype**: A vector in 3D space.
23#[derive(Clone, Debug, Default, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
24#[repr(C)]
25pub struct Vec3D(pub [f32; 3usize]);
26
27::re_types_core::macros::impl_into_cow!(Vec3D);
28
29impl ::re_types_core::Loggable for Vec3D {
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::Float32, false)),
36            3,
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(std::iter::repeat_n(
69                            Default::default(),
70                            3usize,
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, 3usize))
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                    3,
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        #![allow(clippy::wildcard_imports)]
103        use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
104        use arrow::{array::*, buffer::*, datatypes::*};
105        Ok({
106            let arrow_data = arrow_data
107                .as_any()
108                .downcast_ref::<arrow::array::FixedSizeListArray>()
109                .ok_or_else(|| {
110                    let expected = Self::arrow_datatype();
111                    let actual = arrow_data.data_type().clone();
112                    DeserializationError::datatype_mismatch(expected, actual)
113                })
114                .with_context("rerun.datatypes.Vec3D#xyz")?;
115            if arrow_data.is_empty() {
116                Vec::new()
117            } else {
118                let offsets = (0..)
119                    .step_by(3usize)
120                    .zip((3usize..).step_by(3usize).take(arrow_data.len()));
121                let arrow_data_inner = {
122                    let arrow_data_inner = &**arrow_data.values();
123                    arrow_data_inner
124                        .as_any()
125                        .downcast_ref::<Float32Array>()
126                        .ok_or_else(|| {
127                            let expected = DataType::Float32;
128                            let actual = arrow_data_inner.data_type().clone();
129                            DeserializationError::datatype_mismatch(expected, actual)
130                        })
131                        .with_context("rerun.datatypes.Vec3D#xyz")?
132                        .into_iter()
133                        .collect::<Vec<_>>()
134                };
135                ZipValidity::new_with_validity(offsets, arrow_data.nulls())
136                    .map(|elem| {
137                        elem.map(|(start, end): (usize, usize)| {
138                            debug_assert!(end - start == 3usize);
139                            if arrow_data_inner.len() < end {
140                                return Err(DeserializationError::offset_slice_oob(
141                                    (start, end),
142                                    arrow_data_inner.len(),
143                                ));
144                            }
145
146                            #[allow(unsafe_code, clippy::undocumented_unsafe_blocks)]
147                            let data = unsafe { arrow_data_inner.get_unchecked(start..end) };
148                            let data = data.iter().cloned().map(Option::unwrap_or_default);
149
150                            // NOTE: Unwrapping cannot fail: the length must be correct.
151                            #[allow(clippy::unwrap_used)]
152                            Ok(array_init::from_iter(data).unwrap())
153                        })
154                        .transpose()
155                    })
156                    .collect::<DeserializationResult<Vec<Option<_>>>>()?
157            }
158            .into_iter()
159        }
160        .map(|v| v.ok_or_else(DeserializationError::missing_data))
161        .map(|res| res.map(|v| Some(Self(v))))
162        .collect::<DeserializationResult<Vec<Option<_>>>>()
163        .with_context("rerun.datatypes.Vec3D#xyz")
164        .with_context("rerun.datatypes.Vec3D")?)
165    }
166
167    #[inline]
168    fn from_arrow(arrow_data: &dyn arrow::array::Array) -> DeserializationResult<Vec<Self>>
169    where
170        Self: Sized,
171    {
172        #![allow(clippy::wildcard_imports)]
173        use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
174        use arrow::{array::*, buffer::*, datatypes::*};
175        if let Some(nulls) = arrow_data.nulls() {
176            if nulls.null_count() != 0 {
177                return Err(DeserializationError::missing_data());
178            }
179        }
180        Ok({
181            let slice = {
182                let arrow_data = arrow_data
183                    .as_any()
184                    .downcast_ref::<arrow::array::FixedSizeListArray>()
185                    .ok_or_else(|| {
186                        let expected = DataType::FixedSizeList(
187                            std::sync::Arc::new(Field::new("item", DataType::Float32, false)),
188                            3,
189                        );
190                        let actual = arrow_data.data_type().clone();
191                        DeserializationError::datatype_mismatch(expected, actual)
192                    })
193                    .with_context("rerun.datatypes.Vec3D#xyz")?;
194                let arrow_data_inner = &**arrow_data.values();
195                bytemuck::cast_slice::<_, [_; 3usize]>(
196                    arrow_data_inner
197                        .as_any()
198                        .downcast_ref::<Float32Array>()
199                        .ok_or_else(|| {
200                            let expected = DataType::Float32;
201                            let actual = arrow_data_inner.data_type().clone();
202                            DeserializationError::datatype_mismatch(expected, actual)
203                        })
204                        .with_context("rerun.datatypes.Vec3D#xyz")?
205                        .values()
206                        .as_ref(),
207                )
208            };
209            {
210                slice.iter().copied().map(Self).collect::<Vec<_>>()
211            }
212        })
213    }
214}
215
216impl From<[f32; 3usize]> for Vec3D {
217    #[inline]
218    fn from(xyz: [f32; 3usize]) -> Self {
219        Self(xyz)
220    }
221}
222
223impl From<Vec3D> for [f32; 3usize] {
224    #[inline]
225    fn from(value: Vec3D) -> Self {
226        value.0
227    }
228}
229
230impl ::re_byte_size::SizeBytes for Vec3D {
231    #[inline]
232    fn heap_size_bytes(&self) -> u64 {
233        self.0.heap_size_bytes()
234    }
235
236    #[inline]
237    fn is_pod() -> bool {
238        <[f32; 3usize]>::is_pod()
239    }
240}