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

Rust: more impl<AsComponents> helpers #8401

Merged
merged 2 commits into from
Dec 11, 2024
Merged
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
108 changes: 108 additions & 0 deletions crates/store/re_types_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ impl<const N: usize> AsComponents for [&dyn ComponentBatch; N] {
}
}

impl<const N: usize> AsComponents for [Box<dyn ComponentBatch>; N] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.map(|batch| ComponentBatchCowWithDescriptor::new(&**batch))
.collect()
}
}

impl AsComponents for &[&dyn ComponentBatch] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
Expand All @@ -118,6 +127,15 @@ impl AsComponents for &[&dyn ComponentBatch] {
}
}

impl AsComponents for &[Box<dyn ComponentBatch>] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.map(|batch| ComponentBatchCowWithDescriptor::new(&**batch))
.collect()
}
}

impl AsComponents for Vec<&dyn ComponentBatch> {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
Expand All @@ -127,6 +145,96 @@ impl AsComponents for Vec<&dyn ComponentBatch> {
}
}

impl AsComponents for Vec<Box<dyn ComponentBatch>> {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.map(|batch| ComponentBatchCowWithDescriptor::new(&**batch))
.collect()
}
}

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

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

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

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

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

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

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

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

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

// ---

mod archetype;
Expand Down
8 changes: 7 additions & 1 deletion docs/snippets/all/archetypes/image_send_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Log the ImageFormat and indicator once, as static.
let format = rerun::components::ImageFormat::rgb8([width as _, height as _]);
rec.log_static("images", &[&format as _, &rerun::Image::indicator() as _])?;
rec.log_static(
"images",
&[
&format as &dyn rerun::ComponentBatch,
&rerun::Image::indicator(),
],
)?;
Comment on lines +26 to +32
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inference only breaks due to the explicit indicator shenanigans here, which I think is fine since they're going away.
Without the indicator, this would now come down to this:

rec.log_static("images", &format)?;

which is pretty neat.


// Split up the image data into several components referencing the underlying data.
let image_size_in_bytes = width * height * 3;
Expand Down
2 changes: 1 addition & 1 deletion docs/snippets/all/archetypes/mesh3d_partial_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
(glam::Vec3::from(vertex_positions[1]) * factor).into(),
(glam::Vec3::from(vertex_positions[2]) * factor).into(),
];
rec.log("triangle", &[&vertex_positions as _])?;
rec.log("triangle", &vertex_positions)?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inference broke because of the unspecified as _. Now we don't need it at all, so that's just better.

}

Ok(())
Expand Down
Loading