re_types/datatypes/
class_id.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/class_id.fbs".
3
4#![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/// **Datatype**: A 16-bit ID representing a type of semantic class.
25///
26/// Used to look up a [`crate::datatypes::ClassDescription`] within the [`crate::components::AnnotationContext`].
27#[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 ClassId(pub u16);
43
44::re_types_core::macros::impl_into_cow!(ClassId);
45
46impl ::re_types_core::Loggable for ClassId {
47    #[inline]
48    fn arrow_datatype() -> arrow::datatypes::DataType {
49        use arrow::datatypes::*;
50        DataType::UInt16
51    }
52
53    fn to_arrow_opt<'a>(
54        data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
55    ) -> SerializationResult<arrow::array::ArrayRef>
56    where
57        Self: Clone + 'a,
58    {
59        #![allow(clippy::manual_is_variant_and)]
60        use ::re_types_core::{Loggable as _, ResultExt as _, arrow_helpers::as_array_ref};
61        use arrow::{array::*, buffer::*, datatypes::*};
62        Ok({
63            let (somes, data0): (Vec<_>, Vec<_>) = data
64                .into_iter()
65                .map(|datum| {
66                    let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
67                    let datum = datum.map(|datum| datum.into_owned().0);
68                    (datum.is_some(), datum)
69                })
70                .unzip();
71            let data0_validity: Option<arrow::buffer::NullBuffer> = {
72                let any_nones = somes.iter().any(|some| !*some);
73                any_nones.then(|| somes.into())
74            };
75            as_array_ref(PrimitiveArray::<UInt16Type>::new(
76                ScalarBuffer::from(
77                    data0
78                        .into_iter()
79                        .map(|v| v.unwrap_or_default())
80                        .collect::<Vec<_>>(),
81                ),
82                data0_validity,
83            ))
84        })
85    }
86
87    fn from_arrow_opt(
88        arrow_data: &dyn arrow::array::Array,
89    ) -> DeserializationResult<Vec<Option<Self>>>
90    where
91        Self: Sized,
92    {
93        use ::re_types_core::{Loggable as _, ResultExt as _, arrow_zip_validity::ZipValidity};
94        use arrow::{array::*, buffer::*, datatypes::*};
95        Ok(arrow_data
96            .as_any()
97            .downcast_ref::<UInt16Array>()
98            .ok_or_else(|| {
99                let expected = Self::arrow_datatype();
100                let actual = arrow_data.data_type().clone();
101                DeserializationError::datatype_mismatch(expected, actual)
102            })
103            .with_context("rerun.datatypes.ClassId#id")?
104            .into_iter()
105            .map(|v| v.ok_or_else(DeserializationError::missing_data))
106            .map(|res| res.map(|v| Some(Self(v))))
107            .collect::<DeserializationResult<Vec<Option<_>>>>()
108            .with_context("rerun.datatypes.ClassId#id")
109            .with_context("rerun.datatypes.ClassId")?)
110    }
111
112    #[inline]
113    fn from_arrow(arrow_data: &dyn arrow::array::Array) -> DeserializationResult<Vec<Self>>
114    where
115        Self: Sized,
116    {
117        use ::re_types_core::{Loggable as _, ResultExt as _, arrow_zip_validity::ZipValidity};
118        use arrow::{array::*, buffer::*, datatypes::*};
119        if let Some(nulls) = arrow_data.nulls()
120            && nulls.null_count() != 0
121        {
122            return Err(DeserializationError::missing_data());
123        }
124        Ok({
125            let slice = arrow_data
126                .as_any()
127                .downcast_ref::<UInt16Array>()
128                .ok_or_else(|| {
129                    let expected = DataType::UInt16;
130                    let actual = arrow_data.data_type().clone();
131                    DeserializationError::datatype_mismatch(expected, actual)
132                })
133                .with_context("rerun.datatypes.ClassId#id")?
134                .values()
135                .as_ref();
136            { slice.iter().copied().map(Self).collect::<Vec<_>>() }
137        })
138    }
139}
140
141impl From<u16> for ClassId {
142    #[inline]
143    fn from(id: u16) -> Self {
144        Self(id)
145    }
146}
147
148impl From<ClassId> for u16 {
149    #[inline]
150    fn from(value: ClassId) -> Self {
151        value.0
152    }
153}
154
155impl ::re_byte_size::SizeBytes for ClassId {
156    #[inline]
157    fn heap_size_bytes(&self) -> u64 {
158        self.0.heap_size_bytes()
159    }
160
161    #[inline]
162    fn is_pod() -> bool {
163        <u16>::is_pod()
164    }
165}