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