re_types/datatypes/
uuid.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, Eq)]
24#[repr(transparent)]
25pub struct Uuid {
26 pub bytes: [u8; 16usize],
28}
29
30::re_types_core::macros::impl_into_cow!(Uuid);
31
32impl ::re_types_core::Loggable for Uuid {
33 #[inline]
34 fn arrow_datatype() -> arrow::datatypes::DataType {
35 #![allow(clippy::wildcard_imports)]
36 use arrow::datatypes::*;
37 DataType::FixedSizeList(
38 std::sync::Arc::new(Field::new("item", DataType::UInt8, false)),
39 16,
40 )
41 }
42
43 fn to_arrow_opt<'a>(
44 data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
45 ) -> SerializationResult<arrow::array::ArrayRef>
46 where
47 Self: Clone + 'a,
48 {
49 #![allow(clippy::wildcard_imports)]
50 #![allow(clippy::manual_is_variant_and)]
51 use ::re_types_core::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
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(
72 std::iter::repeat(Default::default()).take(16usize),
73 ),
74 })
75 .collect();
76 let bytes_inner_validity: Option<arrow::buffer::NullBuffer> =
77 bytes_validity.as_ref().map(|validity| {
78 validity
79 .iter()
80 .map(|b| std::iter::repeat(b).take(16usize))
81 .flatten()
82 .collect::<Vec<_>>()
83 .into()
84 });
85 as_array_ref(FixedSizeListArray::new(
86 std::sync::Arc::new(Field::new("item", DataType::UInt8, false)),
87 16,
88 as_array_ref(PrimitiveArray::<UInt8Type>::new(
89 ScalarBuffer::from(bytes_inner_data.into_iter().collect::<Vec<_>>()),
90 bytes_inner_validity,
91 )),
92 bytes_validity,
93 ))
94 }
95 })
96 }
97
98 fn from_arrow_opt(
99 arrow_data: &dyn arrow::array::Array,
100 ) -> DeserializationResult<Vec<Option<Self>>>
101 where
102 Self: Sized,
103 {
104 #![allow(clippy::wildcard_imports)]
105 use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
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 #[allow(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 #[allow(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 #![allow(clippy::wildcard_imports)]
175 use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
176 use arrow::{array::*, buffer::*, datatypes::*};
177 if let Some(nulls) = arrow_data.nulls() {
178 if nulls.null_count() != 0 {
179 return Err(DeserializationError::missing_data());
180 }
181 }
182 Ok({
183 let slice = {
184 let arrow_data = arrow_data
185 .as_any()
186 .downcast_ref::<arrow::array::FixedSizeListArray>()
187 .ok_or_else(|| {
188 let expected = DataType::FixedSizeList(
189 std::sync::Arc::new(Field::new("item", DataType::UInt8, false)),
190 16,
191 );
192 let actual = arrow_data.data_type().clone();
193 DeserializationError::datatype_mismatch(expected, actual)
194 })
195 .with_context("rerun.datatypes.Uuid#bytes")?;
196 let arrow_data_inner = &**arrow_data.values();
197 bytemuck::cast_slice::<_, [_; 16usize]>(
198 arrow_data_inner
199 .as_any()
200 .downcast_ref::<UInt8Array>()
201 .ok_or_else(|| {
202 let expected = DataType::UInt8;
203 let actual = arrow_data_inner.data_type().clone();
204 DeserializationError::datatype_mismatch(expected, actual)
205 })
206 .with_context("rerun.datatypes.Uuid#bytes")?
207 .values()
208 .as_ref(),
209 )
210 };
211 {
212 slice
213 .iter()
214 .copied()
215 .map(|bytes| Self { bytes })
216 .collect::<Vec<_>>()
217 }
218 })
219 }
220}
221
222impl From<[u8; 16usize]> for Uuid {
223 #[inline]
224 fn from(bytes: [u8; 16usize]) -> Self {
225 Self { bytes }
226 }
227}
228
229impl From<Uuid> for [u8; 16usize] {
230 #[inline]
231 fn from(value: Uuid) -> Self {
232 value.bytes
233 }
234}
235
236impl ::re_byte_size::SizeBytes for Uuid {
237 #[inline]
238 fn heap_size_bytes(&self) -> u64 {
239 self.bytes.heap_size_bytes()
240 }
241
242 #[inline]
243 fn is_pod() -> bool {
244 <[u8; 16usize]>::is_pod()
245 }
246}