Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement AsComponents for tuples. #4627

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ num-traits = "0.2"
once_cell = "1.17"
ordered-float = "4.2"
parking_lot = "0.12"
paste = "1.0.12"
pathdiff = "0.2"
pico-args = "0.5"
ply-rs = { version = "0.1", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/re_types_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ arrow2 = { workspace = true, features = [
backtrace.workspace = true
document-features.workspace = true
once_cell.workspace = true
paste.workspace = true
smallvec.workspace = true
thiserror.workspace = true

Expand Down
1 change: 1 addition & 0 deletions crates/re_types_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ mod loggable_batch;
mod result;
mod size_bytes;
mod tuid;
mod tuple;

pub use self::archetype::{
Archetype, ArchetypeName, GenericIndicatorComponent, NamedIndicatorComponent,
Expand Down
47 changes: 47 additions & 0 deletions crates/re_types_core/src/tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Implements [`AsComponents`] for tuples.

use crate::{AsComponents, MaybeOwnedComponentBatch};

macro_rules! impl_as_components_for_tuples {
// This recursive macro trick is from <https://stackoverflow.com/questions/55553281/>.
( $_dropped:literal, $( $name:literal, )* ) => {
paste::paste! {
/// A tuple of bundles may act as a bundle.
impl<$( [<B $name>] : AsComponents ),*>
AsComponents for ($( [<B $name>], )*)
{
fn as_component_batches(
&self,
) -> Vec<MaybeOwnedComponentBatch<'_>> {
#[allow(unused_mut)]
let mut vector = Vec::new();
$(
vector.extend(self.$name.as_component_batches());
)*
vector
}

fn num_instances(&self) -> usize {
0 $( .max(self.$name.num_instances()) )*
}

fn to_arrow(
&self
) -> crate::SerializationResult<
Vec<(::arrow2::datatypes::Field, Box<dyn ::arrow2::array::Array>)>,
> {
#[allow(unused_mut)]
let mut vector = Vec::new();
$(
vector.extend(self.$name.to_arrow()?);
)*
Ok(vector)
}
}
}
impl_as_components_for_tuples!($( $name, )*);
};
() => {};
}

impl_as_components_for_tuples!(9999, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,);
Loading