re_types_core/datatypes/
bool.rs1#![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#[derive(Clone, Debug, Copy, Default, PartialEq, Eq, PartialOrd, Ord)]
23#[repr(transparent)]
24pub struct Bool(pub bool);
25
26crate::macros::impl_into_cow!(Bool);
27
28impl crate::Loggable for Bool {
29 #[inline]
30 fn arrow_datatype() -> arrow::datatypes::DataType {
31 #![allow(clippy::wildcard_imports)]
32 use arrow::datatypes::*;
33 DataType::Boolean
34 }
35
36 fn to_arrow_opt<'a>(
37 data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
38 ) -> SerializationResult<arrow::array::ArrayRef>
39 where
40 Self: Clone + 'a,
41 {
42 #![allow(clippy::wildcard_imports)]
43 #![allow(clippy::manual_is_variant_and)]
44 use crate::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
45 use arrow::{array::*, buffer::*, datatypes::*};
46 Ok({
47 let (somes, data0): (Vec<_>, Vec<_>) = data
48 .into_iter()
49 .map(|datum| {
50 let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
51 let datum = datum.map(|datum| datum.into_owned().0);
52 (datum.is_some(), datum)
53 })
54 .unzip();
55 let data0_validity: Option<arrow::buffer::NullBuffer> = {
56 let any_nones = somes.iter().any(|some| !*some);
57 any_nones.then(|| somes.into())
58 };
59 as_array_ref(BooleanArray::new(
60 BooleanBuffer::from(
61 data0
62 .into_iter()
63 .map(|v| v.unwrap_or_default())
64 .collect::<Vec<_>>(),
65 ),
66 data0_validity,
67 ))
68 })
69 }
70
71 fn from_arrow_opt(
72 arrow_data: &dyn arrow::array::Array,
73 ) -> DeserializationResult<Vec<Option<Self>>>
74 where
75 Self: Sized,
76 {
77 #![allow(clippy::wildcard_imports)]
78 use crate::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
79 use arrow::{array::*, buffer::*, datatypes::*};
80 Ok(arrow_data
81 .as_any()
82 .downcast_ref::<BooleanArray>()
83 .ok_or_else(|| {
84 let expected = Self::arrow_datatype();
85 let actual = arrow_data.data_type().clone();
86 DeserializationError::datatype_mismatch(expected, actual)
87 })
88 .with_context("rerun.datatypes.Bool#value")?
89 .into_iter()
90 .map(|v| v.ok_or_else(DeserializationError::missing_data))
91 .map(|res| res.map(|v| Some(Self(v))))
92 .collect::<DeserializationResult<Vec<Option<_>>>>()
93 .with_context("rerun.datatypes.Bool#value")
94 .with_context("rerun.datatypes.Bool")?)
95 }
96}
97
98impl From<bool> for Bool {
99 #[inline]
100 fn from(value: bool) -> Self {
101 Self(value)
102 }
103}
104
105impl From<Bool> for bool {
106 #[inline]
107 fn from(value: Bool) -> Self {
108 value.0
109 }
110}
111
112impl ::re_byte_size::SizeBytes for Bool {
113 #[inline]
114 fn heap_size_bytes(&self) -> u64 {
115 self.0.heap_size_bytes()
116 }
117
118 #[inline]
119 fn is_pod() -> bool {
120 <bool>::is_pod()
121 }
122}