re_types/archetypes/
graph_edges.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/archetypes/graph_edges.fbs".
3
4#![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 ::re_types_core::try_serialize_field;
17use ::re_types_core::SerializationResult;
18use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
19use ::re_types_core::{ComponentDescriptor, ComponentType};
20use ::re_types_core::{DeserializationError, DeserializationResult};
21
22/// **Archetype**: A list of edges in a graph.
23///
24/// By default, edges are undirected.
25///
26/// ## Example
27///
28/// ### Simple directed graph
29/// ```ignore
30/// fn main() -> Result<(), Box<dyn std::error::Error>> {
31///     let rec = rerun::RecordingStreamBuilder::new("rerun_example_graph_directed").spawn()?;
32///
33///     rec.log(
34///         "simple",
35///         &[
36///             &rerun::GraphNodes::new(["a", "b", "c"])
37///                 .with_positions([(0.0, 100.0), (-100.0, 0.0), (100.0, 0.0)])
38///                 .with_labels(["A", "B", "C"]) as &dyn rerun::AsComponents,
39///             &rerun::GraphEdges::new([("a", "b"), ("b", "c"), ("c", "a")]).with_directed_edges(),
40///         ],
41///     )?;
42///
43///     Ok(())
44/// }
45/// ```
46/// <center>
47/// <picture>
48///   <source media="(max-width: 480px)" srcset="https://static.rerun.io/graph_directed/ca29a37b65e1e0b6482251dce401982a0bc568fa/480w.png">
49///   <source media="(max-width: 768px)" srcset="https://static.rerun.io/graph_directed/ca29a37b65e1e0b6482251dce401982a0bc568fa/768w.png">
50///   <source media="(max-width: 1024px)" srcset="https://static.rerun.io/graph_directed/ca29a37b65e1e0b6482251dce401982a0bc568fa/1024w.png">
51///   <source media="(max-width: 1200px)" srcset="https://static.rerun.io/graph_directed/ca29a37b65e1e0b6482251dce401982a0bc568fa/1200w.png">
52///   <img src="https://static.rerun.io/graph_directed/ca29a37b65e1e0b6482251dce401982a0bc568fa/full.png" width="640">
53/// </picture>
54/// </center>
55#[derive(Clone, Debug, PartialEq, Default)]
56pub struct GraphEdges {
57    /// A list of node tuples.
58    pub edges: Option<SerializedComponentBatch>,
59
60    /// Specifies if the graph is directed or undirected.
61    ///
62    /// If no [`components::GraphType`][crate::components::GraphType] is provided, the graph is assumed to be undirected.
63    pub graph_type: Option<SerializedComponentBatch>,
64}
65
66impl GraphEdges {
67    /// Returns the [`ComponentDescriptor`] for [`Self::edges`].
68    ///
69    /// The corresponding component is [`crate::components::GraphEdge`].
70    #[inline]
71    pub fn descriptor_edges() -> ComponentDescriptor {
72        ComponentDescriptor {
73            archetype: Some("rerun.archetypes.GraphEdges".into()),
74            component: "GraphEdges:edges".into(),
75            component_type: Some("rerun.components.GraphEdge".into()),
76        }
77    }
78
79    /// Returns the [`ComponentDescriptor`] for [`Self::graph_type`].
80    ///
81    /// The corresponding component is [`crate::components::GraphType`].
82    #[inline]
83    pub fn descriptor_graph_type() -> ComponentDescriptor {
84        ComponentDescriptor {
85            archetype: Some("rerun.archetypes.GraphEdges".into()),
86            component: "GraphEdges:graph_type".into(),
87            component_type: Some("rerun.components.GraphType".into()),
88        }
89    }
90}
91
92static REQUIRED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 1usize]> =
93    once_cell::sync::Lazy::new(|| [GraphEdges::descriptor_edges()]);
94
95static RECOMMENDED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 1usize]> =
96    once_cell::sync::Lazy::new(|| [GraphEdges::descriptor_graph_type()]);
97
98static OPTIONAL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 0usize]> =
99    once_cell::sync::Lazy::new(|| []);
100
101static ALL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 2usize]> =
102    once_cell::sync::Lazy::new(|| {
103        [
104            GraphEdges::descriptor_edges(),
105            GraphEdges::descriptor_graph_type(),
106        ]
107    });
108
109impl GraphEdges {
110    /// The total number of components in the archetype: 1 required, 1 recommended, 0 optional
111    pub const NUM_COMPONENTS: usize = 2usize;
112}
113
114impl ::re_types_core::Archetype for GraphEdges {
115    #[inline]
116    fn name() -> ::re_types_core::ArchetypeName {
117        "rerun.archetypes.GraphEdges".into()
118    }
119
120    #[inline]
121    fn display_name() -> &'static str {
122        "Graph edges"
123    }
124
125    #[inline]
126    fn required_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
127        REQUIRED_COMPONENTS.as_slice().into()
128    }
129
130    #[inline]
131    fn recommended_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
132        RECOMMENDED_COMPONENTS.as_slice().into()
133    }
134
135    #[inline]
136    fn optional_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
137        OPTIONAL_COMPONENTS.as_slice().into()
138    }
139
140    #[inline]
141    fn all_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
142        ALL_COMPONENTS.as_slice().into()
143    }
144
145    #[inline]
146    fn from_arrow_components(
147        arrow_data: impl IntoIterator<Item = (ComponentDescriptor, arrow::array::ArrayRef)>,
148    ) -> DeserializationResult<Self> {
149        re_tracing::profile_function!();
150        use ::re_types_core::{Loggable as _, ResultExt as _};
151        let arrays_by_descr: ::nohash_hasher::IntMap<_, _> = arrow_data.into_iter().collect();
152        let edges = arrays_by_descr
153            .get(&Self::descriptor_edges())
154            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_edges()));
155        let graph_type = arrays_by_descr
156            .get(&Self::descriptor_graph_type())
157            .map(|array| {
158                SerializedComponentBatch::new(array.clone(), Self::descriptor_graph_type())
159            });
160        Ok(Self { edges, graph_type })
161    }
162}
163
164impl ::re_types_core::AsComponents for GraphEdges {
165    #[inline]
166    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
167        use ::re_types_core::Archetype as _;
168        [self.edges.clone(), self.graph_type.clone()]
169            .into_iter()
170            .flatten()
171            .collect()
172    }
173}
174
175impl ::re_types_core::ArchetypeReflectionMarker for GraphEdges {}
176
177impl GraphEdges {
178    /// Create a new `GraphEdges`.
179    #[inline]
180    pub fn new(edges: impl IntoIterator<Item = impl Into<crate::components::GraphEdge>>) -> Self {
181        Self {
182            edges: try_serialize_field(Self::descriptor_edges(), edges),
183            graph_type: None,
184        }
185    }
186
187    /// Update only some specific fields of a `GraphEdges`.
188    #[inline]
189    pub fn update_fields() -> Self {
190        Self::default()
191    }
192
193    /// Clear all the fields of a `GraphEdges`.
194    #[inline]
195    pub fn clear_fields() -> Self {
196        use ::re_types_core::Loggable as _;
197        Self {
198            edges: Some(SerializedComponentBatch::new(
199                crate::components::GraphEdge::arrow_empty(),
200                Self::descriptor_edges(),
201            )),
202            graph_type: Some(SerializedComponentBatch::new(
203                crate::components::GraphType::arrow_empty(),
204                Self::descriptor_graph_type(),
205            )),
206        }
207    }
208
209    /// Partitions the component data into multiple sub-batches.
210    ///
211    /// Specifically, this transforms the existing [`SerializedComponentBatch`]es data into [`SerializedComponentColumn`]s
212    /// instead, via [`SerializedComponentBatch::partitioned`].
213    ///
214    /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
215    ///
216    /// The specified `lengths` must sum to the total length of the component batch.
217    ///
218    /// [`SerializedComponentColumn`]: [::re_types_core::SerializedComponentColumn]
219    #[inline]
220    pub fn columns<I>(
221        self,
222        _lengths: I,
223    ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>>
224    where
225        I: IntoIterator<Item = usize> + Clone,
226    {
227        let columns = [
228            self.edges
229                .map(|edges| edges.partitioned(_lengths.clone()))
230                .transpose()?,
231            self.graph_type
232                .map(|graph_type| graph_type.partitioned(_lengths.clone()))
233                .transpose()?,
234        ];
235        Ok(columns.into_iter().flatten())
236    }
237
238    /// Helper to partition the component data into unit-length sub-batches.
239    ///
240    /// This is semantically similar to calling [`Self::columns`] with `std::iter::take(1).repeat(n)`,
241    /// where `n` is automatically guessed.
242    #[inline]
243    pub fn columns_of_unit_batches(
244        self,
245    ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>> {
246        let len_edges = self.edges.as_ref().map(|b| b.array.len());
247        let len_graph_type = self.graph_type.as_ref().map(|b| b.array.len());
248        let len = None.or(len_edges).or(len_graph_type).unwrap_or(0);
249        self.columns(std::iter::repeat(1).take(len))
250    }
251
252    /// A list of node tuples.
253    #[inline]
254    pub fn with_edges(
255        mut self,
256        edges: impl IntoIterator<Item = impl Into<crate::components::GraphEdge>>,
257    ) -> Self {
258        self.edges = try_serialize_field(Self::descriptor_edges(), edges);
259        self
260    }
261
262    /// Specifies if the graph is directed or undirected.
263    ///
264    /// If no [`components::GraphType`][crate::components::GraphType] is provided, the graph is assumed to be undirected.
265    #[inline]
266    pub fn with_graph_type(mut self, graph_type: impl Into<crate::components::GraphType>) -> Self {
267        self.graph_type = try_serialize_field(Self::descriptor_graph_type(), [graph_type]);
268        self
269    }
270
271    /// This method makes it possible to pack multiple [`crate::components::GraphType`] in a single component batch.
272    ///
273    /// This only makes sense when used in conjunction with [`Self::columns`]. [`Self::with_graph_type`] should
274    /// be used when logging a single row's worth of data.
275    #[inline]
276    pub fn with_many_graph_type(
277        mut self,
278        graph_type: impl IntoIterator<Item = impl Into<crate::components::GraphType>>,
279    ) -> Self {
280        self.graph_type = try_serialize_field(Self::descriptor_graph_type(), graph_type);
281        self
282    }
283}
284
285impl ::re_byte_size::SizeBytes for GraphEdges {
286    #[inline]
287    fn heap_size_bytes(&self) -> u64 {
288        self.edges.heap_size_bytes() + self.graph_type.heap_size_bytes()
289    }
290}