re_types/datatypes/
keypoint_id.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(
28 Clone,
29 Debug,
30 Copy,
31 Default,
32 PartialEq,
33 Eq,
34 PartialOrd,
35 Ord,
36 Hash,
37 bytemuck::Pod,
38 bytemuck::Zeroable,
39)]
40#[repr(transparent)]
41#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
42pub struct KeypointId(pub u16);
43
44::re_types_core::macros::impl_into_cow!(KeypointId);
45
46impl ::re_types_core::Loggable for KeypointId {
47 #[inline]
48 fn arrow_datatype() -> arrow::datatypes::DataType {
49 #![allow(clippy::wildcard_imports)]
50 use arrow::datatypes::*;
51 DataType::UInt16
52 }
53
54 fn to_arrow_opt<'a>(
55 data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
56 ) -> SerializationResult<arrow::array::ArrayRef>
57 where
58 Self: Clone + 'a,
59 {
60 #![allow(clippy::wildcard_imports)]
61 #![allow(clippy::manual_is_variant_and)]
62 use ::re_types_core::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
63 use arrow::{array::*, buffer::*, datatypes::*};
64 Ok({
65 let (somes, data0): (Vec<_>, Vec<_>) = data
66 .into_iter()
67 .map(|datum| {
68 let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
69 let datum = datum.map(|datum| datum.into_owned().0);
70 (datum.is_some(), datum)
71 })
72 .unzip();
73 let data0_validity: Option<arrow::buffer::NullBuffer> = {
74 let any_nones = somes.iter().any(|some| !*some);
75 any_nones.then(|| somes.into())
76 };
77 as_array_ref(PrimitiveArray::<UInt16Type>::new(
78 ScalarBuffer::from(
79 data0
80 .into_iter()
81 .map(|v| v.unwrap_or_default())
82 .collect::<Vec<_>>(),
83 ),
84 data0_validity,
85 ))
86 })
87 }
88
89 fn from_arrow_opt(
90 arrow_data: &dyn arrow::array::Array,
91 ) -> DeserializationResult<Vec<Option<Self>>>
92 where
93 Self: Sized,
94 {
95 #![allow(clippy::wildcard_imports)]
96 use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
97 use arrow::{array::*, buffer::*, datatypes::*};
98 Ok(arrow_data
99 .as_any()
100 .downcast_ref::<UInt16Array>()
101 .ok_or_else(|| {
102 let expected = Self::arrow_datatype();
103 let actual = arrow_data.data_type().clone();
104 DeserializationError::datatype_mismatch(expected, actual)
105 })
106 .with_context("rerun.datatypes.KeypointId#id")?
107 .into_iter()
108 .map(|v| v.ok_or_else(DeserializationError::missing_data))
109 .map(|res| res.map(|v| Some(Self(v))))
110 .collect::<DeserializationResult<Vec<Option<_>>>>()
111 .with_context("rerun.datatypes.KeypointId#id")
112 .with_context("rerun.datatypes.KeypointId")?)
113 }
114
115 #[inline]
116 fn from_arrow(arrow_data: &dyn arrow::array::Array) -> DeserializationResult<Vec<Self>>
117 where
118 Self: Sized,
119 {
120 #![allow(clippy::wildcard_imports)]
121 use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
122 use arrow::{array::*, buffer::*, datatypes::*};
123 if let Some(nulls) = arrow_data.nulls() {
124 if nulls.null_count() != 0 {
125 return Err(DeserializationError::missing_data());
126 }
127 }
128 Ok({
129 let slice = arrow_data
130 .as_any()
131 .downcast_ref::<UInt16Array>()
132 .ok_or_else(|| {
133 let expected = DataType::UInt16;
134 let actual = arrow_data.data_type().clone();
135 DeserializationError::datatype_mismatch(expected, actual)
136 })
137 .with_context("rerun.datatypes.KeypointId#id")?
138 .values()
139 .as_ref();
140 {
141 slice.iter().copied().map(Self).collect::<Vec<_>>()
142 }
143 })
144 }
145}
146
147impl From<u16> for KeypointId {
148 #[inline]
149 fn from(id: u16) -> Self {
150 Self(id)
151 }
152}
153
154impl From<KeypointId> for u16 {
155 #[inline]
156 fn from(value: KeypointId) -> Self {
157 value.0
158 }
159}
160
161impl ::re_byte_size::SizeBytes for KeypointId {
162 #[inline]
163 fn heap_size_bytes(&self) -> u64 {
164 self.0.heap_size_bytes()
165 }
166
167 #[inline]
168 fn is_pod() -> bool {
169 <u16>::is_pod()
170 }
171}