re_types_core/datatypes/
bool.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, ComponentType};
20use crate::{DeserializationError, DeserializationResult};
21
22#[derive(Clone, Debug, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
24#[repr(transparent)]
25pub struct Bool(pub bool);
26
27crate::macros::impl_into_cow!(Bool);
28
29impl crate::Loggable for Bool {
30 #[inline]
31 fn arrow_datatype() -> arrow::datatypes::DataType {
32 #![allow(clippy::wildcard_imports)]
33 use arrow::datatypes::*;
34 DataType::Boolean
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(BooleanArray::new(
61 BooleanBuffer::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::<BooleanArray>()
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.Bool#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.Bool#value")
95 .with_context("rerun.datatypes.Bool")?)
96 }
97}
98
99impl From<bool> for Bool {
100 #[inline]
101 fn from(value: bool) -> Self {
102 Self(value)
103 }
104}
105
106impl From<Bool> for bool {
107 #[inline]
108 fn from(value: Bool) -> Self {
109 value.0
110 }
111}
112
113impl ::re_byte_size::SizeBytes for Bool {
114 #[inline]
115 fn heap_size_bytes(&self) -> u64 {
116 self.0.heap_size_bytes()
117 }
118
119 #[inline]
120 fn is_pod() -> bool {
121 <bool>::is_pod()
122 }
123}