re_types/datatypes/
uuid.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, Eq)]
26#[repr(transparent)]
27pub struct Uuid {
28 pub bytes: [u8; 16usize],
30}
31
32::re_types_core::macros::impl_into_cow!(Uuid);
33
34impl ::re_types_core::Loggable for Uuid {
35 #[inline]
36 fn arrow_datatype() -> arrow::datatypes::DataType {
37 use arrow::datatypes::*;
38 DataType::FixedSizeList(
39 std::sync::Arc::new(Field::new("item", DataType::UInt8, false)),
40 16,
41 )
42 }
43
44 fn to_arrow_opt<'a>(
45 data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
46 ) -> SerializationResult<arrow::array::ArrayRef>
47 where
48 Self: Clone + 'a,
49 {
50 #![allow(clippy::manual_is_variant_and)]
51 use ::re_types_core::{Loggable as _, ResultExt as _, arrow_helpers::as_array_ref};
52 use arrow::{array::*, buffer::*, datatypes::*};
53 Ok({
54 let (somes, bytes): (Vec<_>, Vec<_>) = data
55 .into_iter()
56 .map(|datum| {
57 let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
58 let datum = datum.map(|datum| datum.into_owned().bytes);
59 (datum.is_some(), datum)
60 })
61 .unzip();
62 let bytes_validity: Option<arrow::buffer::NullBuffer> = {
63 let any_nones = somes.iter().any(|some| !*some);
64 any_nones.then(|| somes.into())
65 };
66 {
67 let bytes_inner_data: Vec<_> = bytes
68 .into_iter()
69 .flat_map(|v| match v {
70 Some(v) => itertools::Either::Left(v.into_iter()),
71 None => itertools::Either::Right(std::iter::repeat_n(
72 Default::default(),
73 16usize,
74 )),
75 })
76 .collect();
77 let bytes_inner_validity: Option<arrow::buffer::NullBuffer> =
78 bytes_validity.as_ref().map(|validity| {
79 validity
80 .iter()
81 .map(|b| std::iter::repeat_n(b, 16usize))
82 .flatten()
83 .collect::<Vec<_>>()
84 .into()
85 });
86 as_array_ref(FixedSizeListArray::new(
87 std::sync::Arc::new(Field::new("item", DataType::UInt8, false)),
88 16,
89 as_array_ref(PrimitiveArray::<UInt8Type>::new(
90 ScalarBuffer::from(bytes_inner_data.into_iter().collect::<Vec<_>>()),
91 bytes_inner_validity,
92 )),
93 bytes_validity,
94 ))
95 }
96 })
97 }
98
99 fn from_arrow_opt(
100 arrow_data: &dyn arrow::array::Array,
101 ) -> DeserializationResult<Vec<Option<Self>>>
102 where
103 Self: Sized,
104 {
105 use ::re_types_core::{Loggable as _, ResultExt as _, arrow_zip_validity::ZipValidity};
106 use arrow::{array::*, buffer::*, datatypes::*};
107 Ok({
108 let arrow_data = arrow_data
109 .as_any()
110 .downcast_ref::<arrow::array::FixedSizeListArray>()
111 .ok_or_else(|| {
112 let expected = Self::arrow_datatype();
113 let actual = arrow_data.data_type().clone();
114 DeserializationError::datatype_mismatch(expected, actual)
115 })
116 .with_context("rerun.datatypes.Uuid#bytes")?;
117 if arrow_data.is_empty() {
118 Vec::new()
119 } else {
120 let offsets = (0..)
121 .step_by(16usize)
122 .zip((16usize..).step_by(16usize).take(arrow_data.len()));
123 let arrow_data_inner = {
124 let arrow_data_inner = &**arrow_data.values();
125 arrow_data_inner
126 .as_any()
127 .downcast_ref::<UInt8Array>()
128 .ok_or_else(|| {
129 let expected = DataType::UInt8;
130 let actual = arrow_data_inner.data_type().clone();
131 DeserializationError::datatype_mismatch(expected, actual)
132 })
133 .with_context("rerun.datatypes.Uuid#bytes")?
134 .into_iter()
135 .collect::<Vec<_>>()
136 };
137 ZipValidity::new_with_validity(offsets, arrow_data.nulls())
138 .map(|elem| {
139 elem.map(|(start, end): (usize, usize)| {
140 debug_assert!(end - start == 16usize);
141 if arrow_data_inner.len() < end {
142 return Err(DeserializationError::offset_slice_oob(
143 (start, end),
144 arrow_data_inner.len(),
145 ));
146 }
147
148 #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
149 let data = unsafe { arrow_data_inner.get_unchecked(start..end) };
150 let data = data.iter().cloned().map(Option::unwrap_or_default);
151
152 #[expect(clippy::unwrap_used)]
154 Ok(array_init::from_iter(data).unwrap())
155 })
156 .transpose()
157 })
158 .collect::<DeserializationResult<Vec<Option<_>>>>()?
159 }
160 .into_iter()
161 }
162 .map(|v| v.ok_or_else(DeserializationError::missing_data))
163 .map(|res| res.map(|bytes| Some(Self { bytes })))
164 .collect::<DeserializationResult<Vec<Option<_>>>>()
165 .with_context("rerun.datatypes.Uuid#bytes")
166 .with_context("rerun.datatypes.Uuid")?)
167 }
168
169 #[inline]
170 fn from_arrow(arrow_data: &dyn arrow::array::Array) -> DeserializationResult<Vec<Self>>
171 where
172 Self: Sized,
173 {
174 use ::re_types_core::{Loggable as _, ResultExt as _, arrow_zip_validity::ZipValidity};
175 use arrow::{array::*, buffer::*, datatypes::*};
176 if let Some(nulls) = arrow_data.nulls()
177 && nulls.null_count() != 0
178 {
179 return Err(DeserializationError::missing_data());
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}