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