re_types_core/
as_components.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use crate::{SerializationResult, SerializedComponentBatch};

/// Describes the interface for interpreting an object as a bundle of [`Component`]s.
///
/// ## Custom bundles
///
/// While, in most cases, component bundles are code generated from our [IDL definitions],
/// it is possible to manually extend existing bundles, or even implement fully custom ones.
///
/// All [`AsComponents`] methods are optional to implement, with the exception of
/// [`AsComponents::as_serialized_batches`], which describes how the bundle can be interpreted
/// as a set of [`SerializedComponentBatch`]es: serialized component data.
///
/// Have a look at our [Custom Data Loader] example to learn more about handwritten bundles.
///
/// [IDL definitions]: https://github.com/rerun-io/rerun/tree/latest/crates/store/re_types/definitions/rerun
/// [Custom Data Loader]: https://github.com/rerun-io/rerun/blob/latest/examples/rust/custom_data_loader
/// [`Component`]: [crate::Component]
pub trait AsComponents {
    /// Exposes the object's contents as a set of [`SerializedComponentBatch`]es.
    ///
    /// This is the main mechanism for easily extending builtin archetypes or even writing
    /// fully custom ones.
    /// Have a look at our [Custom Data Loader] example to learn more about extending archetypes.
    ///
    /// Implementers of [`AsComponents`] get one last chance to override the tags in the
    /// [`ComponentDescriptor`], see [`SerializedComponentBatch::with_descriptor_override`].
    ///
    /// [Custom Data Loader]: https://github.com/rerun-io/rerun/blob/latest/docs/snippets/all/tutorials/custom_data.rs
    /// [`ComponentDescriptor`]: [crate::ComponentDescriptor]
    //
    // NOTE: Don't bother returning a CoW here: we need to dynamically discard optional components
    // depending on their presence (or lack thereof) at runtime anyway.
    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch>;

    // ---

    /// Serializes all non-null [`Component`]s of this bundle into Arrow arrays.
    ///
    /// The default implementation will simply serialize the result of [`Self::as_serialized_batches`]
    /// as-is, which is what you want in 99.9% of cases.
    ///
    /// [`Component`]: [crate::Component]
    #[inline]
    fn to_arrow(
        &self,
    ) -> SerializationResult<Vec<(::arrow::datatypes::Field, ::arrow::array::ArrayRef)>> {
        self.as_serialized_batches()
            .into_iter()
            .map(|comp_batch| Ok((arrow::datatypes::Field::from(&comp_batch), comp_batch.array)))
            .collect()
    }
}

#[allow(dead_code)]
fn assert_object_safe() {
    let _: &dyn AsComponents;
}

impl AsComponents for SerializedComponentBatch {
    #[inline]
    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
        vec![self.clone()]
    }
}

impl<AS: AsComponents, const N: usize> AsComponents for [AS; N] {
    #[inline]
    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
        self.iter()
            .flat_map(|as_components| as_components.as_serialized_batches())
            .collect()
    }
}

impl<const N: usize> AsComponents for [&dyn AsComponents; N] {
    #[inline]
    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
        self.iter()
            .flat_map(|as_components| as_components.as_serialized_batches())
            .collect()
    }
}

impl<const N: usize> AsComponents for [Box<dyn AsComponents>; N] {
    #[inline]
    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
        self.iter()
            .flat_map(|as_components| as_components.as_serialized_batches())
            .collect()
    }
}

impl<AS: AsComponents> AsComponents for Vec<AS> {
    #[inline]
    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
        self.iter()
            .flat_map(|as_components| as_components.as_serialized_batches())
            .collect()
    }
}

impl AsComponents for Vec<&dyn AsComponents> {
    #[inline]
    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
        self.iter()
            .flat_map(|as_components| as_components.as_serialized_batches())
            .collect()
    }
}

impl AsComponents for Vec<Box<dyn AsComponents>> {
    #[inline]
    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
        self.iter()
            .flat_map(|as_components| as_components.as_serialized_batches())
            .collect()
    }
}

// ---

// NOTE: These needs to not be tests in order for doc-tests to work.

/// ```compile_fail
/// let comp = re_types_core::components::ClearIsRecursive::default();
/// let _ = (&comp as &dyn re_types_core::AsComponents).as_serialized_batches();
/// ```
#[allow(dead_code)]
#[allow(rustdoc::private_doc_tests)] // doc-tests are the only way to assert failed compilation
fn single_ascomponents() {}

/// ```compile_fail
/// let comp = re_types_core::components::ClearIsRecursive::default();
/// let _ = (&[comp] as &dyn re_types_core::AsComponents).as_serialized_batches();
/// ```
#[allow(dead_code)]
#[allow(rustdoc::private_doc_tests)] // doc-tests are the only way to assert failed compilation
fn single_ascomponents_wrapped() {
    // This is non-sense (and more importantly: dangerous): a single component shouldn't be able to
    // autocast straight to a collection of batches.
}

/// ```compile_fail
/// let comp = re_types_core::components::ClearIsRecursive::default();
/// let _ = (&[comp, comp, comp] as &dyn re_types_core::AsComponents).as_serialized_batches();
/// ```
#[allow(dead_code)]
#[allow(rustdoc::private_doc_tests)] // doc-tests are the only way to assert failed compilation
fn single_ascomponents_wrapped_many() {
    // This is non-sense (and more importantly: dangerous): a single component shouldn't be able to
    // autocast straight to a collection of batches.
}

/// ```compile_fail
/// let comp = re_types_core::components::ClearIsRecursive::default();
/// let comps = vec![comp, comp, comp];
/// let _ = (&comps as &dyn re_types_core::AsComponents).as_serialized_batches();
/// ```
#[allow(dead_code)]
#[allow(rustdoc::private_doc_tests)] // doc-tests are the only way to assert failed compilation
fn many_ascomponents() {}

/// ```compile_fail
/// let comp = re_types_core::components::ClearIsRecursive::default();
/// let comps = vec![comp, comp, comp];
/// let _ = (&[comps] as &dyn re_types_core::AsComponents).as_serialized_batches();
/// ```
#[allow(dead_code)]
#[allow(rustdoc::private_doc_tests)] // doc-tests are the only way to assert failed compilation
fn many_ascomponents_wrapped() {}

/// ```compile_fail
/// let comp = re_types_core::components::ClearIsRecursive::default();
/// let comps = vec![comp, comp, comp];
/// let _ = (&[comps] as &dyn re_types_core::ComponentBatch).to_arrow();
/// ```
#[allow(dead_code)]
#[allow(rustdoc::private_doc_tests)] // doc-tests are the only way to assert failed compilation
fn many_componentbatch_wrapped() {}

/// ```compile_fail
/// let comp = re_types_core::components::ClearIsRecursive::default();
/// let comps = vec![comp, comp, comp];
/// let _ = (&[comps.clone(), comps.clone(), comps.clone()] as &dyn re_types_core::AsComponents).as_serialized_batches();
/// ```
#[allow(dead_code)]
#[allow(rustdoc::private_doc_tests)] // doc-tests are the only way to assert failed compilation
fn many_ascomponents_wrapped_many() {}

/// ```compile_fail
/// let comp = re_types_core::components::ClearIsRecursive::default();
/// let comps = vec![comp, comp, comp];
/// let _ = (&[comps.clone(), comps.clone(), comps.clone()] as &dyn re_types_core::ComponentBatch).to_arrow();
/// ```
#[allow(dead_code)]
#[allow(rustdoc::private_doc_tests)] // doc-tests are the only way to assert failed compilation
fn many_componentbatch_wrapped_many() {}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use arrow::array::{
        types::UInt32Type, Array as ArrowArray, PrimitiveArray as ArrowPrimitiveArray,
    };
    use itertools::Itertools;
    use similar_asserts::assert_eq;

    #[derive(Clone, Copy, Debug, PartialEq, Eq, bytemuck::Pod, bytemuck::Zeroable)]
    #[repr(transparent)]
    pub struct MyColor(pub u32);

    crate::macros::impl_into_cow!(MyColor);

    impl re_byte_size::SizeBytes for MyColor {
        #[inline]
        fn heap_size_bytes(&self) -> u64 {
            let Self(_) = self;
            0
        }
    }

    impl crate::Loggable for MyColor {
        fn arrow_datatype() -> arrow::datatypes::DataType {
            arrow::datatypes::DataType::UInt32
        }

        fn to_arrow_opt<'a>(
            data: impl IntoIterator<Item = Option<impl Into<std::borrow::Cow<'a, Self>>>>,
        ) -> crate::SerializationResult<arrow::array::ArrayRef>
        where
            Self: 'a,
        {
            use crate::datatypes::UInt32;
            UInt32::to_arrow_opt(
                data.into_iter()
                    .map(|opt| opt.map(Into::into).map(|c| UInt32(c.0))),
            )
        }

        fn from_arrow_opt(
            data: &dyn arrow::array::Array,
        ) -> crate::DeserializationResult<Vec<Option<Self>>> {
            use crate::datatypes::UInt32;
            Ok(UInt32::from_arrow_opt(data)?
                .into_iter()
                .map(|opt| opt.map(|v| Self(v.0)))
                .collect())
        }
    }

    impl crate::Component for MyColor {
        fn descriptor() -> crate::ComponentDescriptor {
            crate::ComponentDescriptor::new("example.MyColor")
        }
    }

    #[allow(dead_code)]
    fn data() -> (MyColor, MyColor, MyColor, Vec<MyColor>) {
        let red = MyColor(0xDD0000FF);
        let green = MyColor(0x00DD00FF);
        let blue = MyColor(0x0000DDFF);
        let colors = vec![red, green, blue];
        (red, green, blue, colors)
    }

    #[test]
    fn single_ascomponents_howto() {
        let (red, _, _, _) = data();

        let got = {
            let red = &red as &dyn crate::ComponentBatch;
            vec![red.try_serialized().unwrap().array]
        };
        let expected = vec![
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![red.0])) as Arc<dyn ArrowArray>,
        ];
        assert_eq!(&expected, &got);
    }

    #[test]
    fn single_componentbatch() -> anyhow::Result<()> {
        let (red, _, _, _) = data();

        // A single component should autocast to a batch with a single instance.
        let got = (&red as &dyn crate::ComponentBatch).to_arrow()?;
        let expected =
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![red.0])) as Arc<dyn ArrowArray>;
        similar_asserts::assert_eq!(&expected, &got);

        Ok(())
    }

    #[test]
    fn single_ascomponents_wrapped_howto() {
        let (red, _, _, _) = data();

        let got = {
            let red = &red as &dyn crate::ComponentBatch;
            vec![red.try_serialized().unwrap().array]
        };
        let expected = vec![
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![red.0])) as Arc<dyn ArrowArray>,
        ];
        assert_eq!(&expected, &got);
    }

    #[test]
    fn single_componentbatch_wrapped() -> anyhow::Result<()> {
        let (red, _, _, _) = data();

        // Nothing out of the ordinary here, a slice of components is indeed a batch.
        let got = (&[red] as &dyn crate::ComponentBatch).to_arrow()?;
        let expected =
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![red.0])) as Arc<dyn ArrowArray>;
        similar_asserts::assert_eq!(&expected, &got);

        Ok(())
    }

    #[test]
    fn single_ascomponents_wrapped_many_howto() {
        let (red, green, blue, _) = data();

        let got = {
            let red = &red as &dyn crate::ComponentBatch;
            let green = &green as &dyn crate::ComponentBatch;
            let blue = &blue as &dyn crate::ComponentBatch;
            [
                red.try_serialized().unwrap(),
                green.try_serialized().unwrap(),
                blue.try_serialized().unwrap(),
            ]
            .into_iter()
            .map(|batch| batch.array)
            .collect_vec()
        };
        let expected = vec![
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![red.0])) as Arc<dyn ArrowArray>,
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![green.0])) as Arc<dyn ArrowArray>,
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![blue.0])) as Arc<dyn ArrowArray>,
        ];
        assert_eq!(&expected, &got);
    }

    #[test]
    fn single_componentbatch_wrapped_many() -> anyhow::Result<()> {
        let (red, green, blue, _) = data();

        // Nothing out of the ordinary here, a slice of components is indeed a batch.
        let got = (&[red, green, blue] as &dyn crate::ComponentBatch).to_arrow()?;
        let expected = Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![
            red.0, green.0, blue.0,
        ])) as Arc<dyn ArrowArray>;
        similar_asserts::assert_eq!(&expected, &got);

        Ok(())
    }

    #[test]
    fn many_componentbatch() -> anyhow::Result<()> {
        let (red, green, blue, colors) = data();

        // Nothing out of the ordinary here, a batch is indeed a batch.
        let got = (&colors as &dyn crate::ComponentBatch).to_arrow()?;
        let expected = Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![
            red.0, green.0, blue.0,
        ])) as Arc<dyn ArrowArray>;
        similar_asserts::assert_eq!(&expected, &got);

        Ok(())
    }

    #[test]
    fn many_ascomponents_wrapped_howto() {
        let (red, green, blue, colors) = data();

        let got = {
            let colors = &colors as &dyn crate::ComponentBatch;
            vec![colors.try_serialized().unwrap().array]
        };
        let expected = vec![Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![
            red.0, green.0, blue.0,
        ])) as Arc<dyn ArrowArray>];
        assert_eq!(&expected, &got);
    }

    #[test]
    fn many_ascomponents_wrapped_many_howto() {
        let (red, green, blue, colors) = data();

        // Nothing out of the ordinary here, a collection of batches is indeed a collection of batches.
        let got = {
            let colors = &colors as &dyn crate::ComponentBatch;
            vec![
                colors.try_serialized().unwrap().array,
                colors.try_serialized().unwrap().array,
                colors.try_serialized().unwrap().array,
            ]
        };
        let expected = vec![
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![
                red.0, green.0, blue.0,
            ])) as Arc<dyn ArrowArray>,
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![
                red.0, green.0, blue.0,
            ])) as Arc<dyn ArrowArray>,
            Arc::new(ArrowPrimitiveArray::<UInt32Type>::from(vec![
                red.0, green.0, blue.0,
            ])) as Arc<dyn ArrowArray>,
        ];
        assert_eq!(&expected, &got);
    }
}