re_types/components/
annotation_context.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/components/annotation_context.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 ::re_types_core::try_serialize_field;
16use ::re_types_core::SerializationResult;
17use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
18use ::re_types_core::{ComponentDescriptor, ComponentName};
19use ::re_types_core::{DeserializationError, DeserializationResult};
20
21/// **Component**: The annotation context provides additional information on how to display entities.
22///
23/// Entities can use [`datatypes::ClassId`][crate::datatypes::ClassId]s and [`datatypes::KeypointId`][crate::datatypes::KeypointId]s to provide annotations, and
24/// the labels and colors will be looked up in the appropriate
25/// annotation context. We use the *first* annotation context we find in the
26/// path-hierarchy when searching up through the ancestors of a given entity
27/// path.
28///
29/// ⚠️ **This type is _unstable_ and may change significantly in a way that the data won't be backwards compatible.**
30#[derive(Clone, Debug, Default, Eq, PartialEq)]
31pub struct AnnotationContext(
32    /// List of class descriptions, mapping class indices to class names, colors etc.
33    pub Vec<crate::datatypes::ClassDescriptionMapElem>,
34);
35
36impl ::re_types_core::Component for AnnotationContext {
37    #[inline]
38    fn descriptor() -> ComponentDescriptor {
39        ComponentDescriptor::new("rerun.components.AnnotationContext")
40    }
41}
42
43::re_types_core::macros::impl_into_cow!(AnnotationContext);
44
45impl ::re_types_core::Loggable for AnnotationContext {
46    #[inline]
47    fn arrow_datatype() -> arrow::datatypes::DataType {
48        #![allow(clippy::wildcard_imports)]
49        use arrow::datatypes::*;
50        DataType::List(std::sync::Arc::new(Field::new(
51            "item",
52            <crate::datatypes::ClassDescriptionMapElem>::arrow_datatype(),
53            false,
54        )))
55    }
56
57    fn to_arrow_opt<'a>(
58        data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
59    ) -> SerializationResult<arrow::array::ArrayRef>
60    where
61        Self: Clone + 'a,
62    {
63        #![allow(clippy::wildcard_imports)]
64        #![allow(clippy::manual_is_variant_and)]
65        use ::re_types_core::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
66        use arrow::{array::*, buffer::*, datatypes::*};
67        Ok({
68            let (somes, data0): (Vec<_>, Vec<_>) = data
69                .into_iter()
70                .map(|datum| {
71                    let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
72                    let datum = datum.map(|datum| datum.into_owned().0);
73                    (datum.is_some(), datum)
74                })
75                .unzip();
76            let data0_validity: Option<arrow::buffer::NullBuffer> = {
77                let any_nones = somes.iter().any(|some| !*some);
78                any_nones.then(|| somes.into())
79            };
80            {
81                let offsets = arrow::buffer::OffsetBuffer::<i32>::from_lengths(
82                    data0
83                        .iter()
84                        .map(|opt| opt.as_ref().map_or(0, |datum| datum.len())),
85                );
86                let data0_inner_data: Vec<_> = data0.into_iter().flatten().flatten().collect();
87                let data0_inner_validity: Option<arrow::buffer::NullBuffer> = None;
88                as_array_ref(ListArray::try_new(
89                    std::sync::Arc::new(Field::new(
90                        "item",
91                        <crate::datatypes::ClassDescriptionMapElem>::arrow_datatype(),
92                        false,
93                    )),
94                    offsets,
95                    {
96                        _ = data0_inner_validity;
97                        crate::datatypes::ClassDescriptionMapElem::to_arrow_opt(
98                            data0_inner_data.into_iter().map(Some),
99                        )?
100                    },
101                    data0_validity,
102                )?)
103            }
104        })
105    }
106
107    fn from_arrow_opt(
108        arrow_data: &dyn arrow::array::Array,
109    ) -> DeserializationResult<Vec<Option<Self>>>
110    where
111        Self: Sized,
112    {
113        #![allow(clippy::wildcard_imports)]
114        use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
115        use arrow::{array::*, buffer::*, datatypes::*};
116        Ok({
117            let arrow_data = arrow_data
118                .as_any()
119                .downcast_ref::<arrow::array::ListArray>()
120                .ok_or_else(|| {
121                    let expected = Self::arrow_datatype();
122                    let actual = arrow_data.data_type().clone();
123                    DeserializationError::datatype_mismatch(expected, actual)
124                })
125                .with_context("rerun.components.AnnotationContext#class_map")?;
126            if arrow_data.is_empty() {
127                Vec::new()
128            } else {
129                let arrow_data_inner = {
130                    let arrow_data_inner = &**arrow_data.values();
131                    crate::datatypes::ClassDescriptionMapElem::from_arrow_opt(arrow_data_inner)
132                        .with_context("rerun.components.AnnotationContext#class_map")?
133                        .into_iter()
134                        .collect::<Vec<_>>()
135                };
136                let offsets = arrow_data.offsets();
137                ZipValidity::new_with_validity(offsets.windows(2), arrow_data.nulls())
138                    .map(|elem| {
139                        elem.map(|window| {
140                            let start = window[0] as usize;
141                            let end = window[1] as usize;
142                            if arrow_data_inner.len() < end {
143                                return Err(DeserializationError::offset_slice_oob(
144                                    (start, end),
145                                    arrow_data_inner.len(),
146                                ));
147                            }
148
149                            #[allow(unsafe_code, clippy::undocumented_unsafe_blocks)]
150                            let data = unsafe { arrow_data_inner.get_unchecked(start..end) };
151                            let data = data
152                                .iter()
153                                .cloned()
154                                .map(Option::unwrap_or_default)
155                                .collect();
156                            Ok(data)
157                        })
158                        .transpose()
159                    })
160                    .collect::<DeserializationResult<Vec<Option<_>>>>()?
161            }
162            .into_iter()
163        }
164        .map(|v| v.ok_or_else(DeserializationError::missing_data))
165        .map(|res| res.map(|v| Some(Self(v))))
166        .collect::<DeserializationResult<Vec<Option<_>>>>()
167        .with_context("rerun.components.AnnotationContext#class_map")
168        .with_context("rerun.components.AnnotationContext")?)
169    }
170}
171
172impl<I: Into<crate::datatypes::ClassDescriptionMapElem>, T: IntoIterator<Item = I>> From<T>
173    for AnnotationContext
174{
175    fn from(v: T) -> Self {
176        Self(v.into_iter().map(|v| v.into()).collect())
177    }
178}
179
180impl ::re_byte_size::SizeBytes for AnnotationContext {
181    #[inline]
182    fn heap_size_bytes(&self) -> u64 {
183        self.0.heap_size_bytes()
184    }
185
186    #[inline]
187    fn is_pod() -> bool {
188        <Vec<crate::datatypes::ClassDescriptionMapElem>>::is_pod()
189    }
190}