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