Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jan 9, 2025
1 parent abce9f5 commit 8959ac9
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 2 deletions.
43 changes: 41 additions & 2 deletions crates/build/re_types_builder/src/codegen/rust/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ fn quote_builder_from_obj(reporter: &Reporter, objects: &Objects, obj: &Object)
}
});

let eager_with_methods = optional.iter().map(|field| {
let eager_with_methods = required.iter().chain(optional.iter()).map(|field| {
// fn with_*()
let field_name = format_ident!("{}", field.name);
let descr_fn_name = format_ident!("descriptor_{field_name}");
Expand Down Expand Up @@ -1775,8 +1775,47 @@ fn quote_builder_from_obj(reporter: &Reporter, objects: &Objects, obj: &Object)
}
});

let partial_update_methods = obj.is_eager_rust_archetype().then(|| {
let update_fields_doc =
quote_doc_line(&format!("Update only some specific fields of a `{name}`."));
let clear_fields_doc = quote_doc_line(&format!("Clear all the fields of a `{name}`."));

let fields = required.iter().chain(optional.iter()).map(|field| {
let field_name = format_ident!("{}", field.name);
let descr_fn_name = format_ident!("descriptor_{field_name}");
let (typ, _) = quote_field_type_from_typ(&field.typ, true);
quote! {
#field_name: Some(SerializedComponentBatch::new(
#typ::arrow_empty(),
Self::#descr_fn_name(),
))
}
});

quote! {
#update_fields_doc
#[inline]
pub fn update_fields() -> Self {
Self::default()
}

#clear_fields_doc
#[inline]
pub fn clear_fields() -> Self {
use ::re_types_core::Loggable as _;
Self {
#(#fields),*
}
}
}
});

let with_methods = if obj.is_eager_rust_archetype() {
quote!(#(#eager_with_methods)*)
quote! {
#partial_update_methods

#(#eager_with_methods)*
}
} else {
quote!(#(#native_with_methods)*)
};
Expand Down
52 changes: 52 additions & 0 deletions crates/store/re_types/src/archetypes/points3d.rs

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

92 changes: 92 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! TODO
use rand::{distributions::Uniform, Rng as _};

// TODO: we need to open an issue about this. and test and clean up all of that in general...

// TODO: let's impl the legacy versions for all languages first!

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let dist = Uniform::new(-5., 5.);

// Before
#[cfg(TODO)]
{
let rec =
// rerun::RecordingStreamBuilder::new("rerun_example_points3d_partial_updates").spawn()?;
// rerun::RecordingStreamBuilder::new("rerun_example_points3d_partial_updates_before").stdout()?;
rerun::RecordingStreamBuilder::new("rerun_example_points3d_partial_updates_before").connect_tcp()?;

rec.set_time_sequence("frame", 0);
rec.log(
"points",
&rerun::Points3D::new(
(0..10).map(|_| (rng.sample(dist), rng.sample(dist), rng.sample(dist))),
),
)?;

for i in 0..10 {
let colors: Vec<rerun::components::Color> = (0..10)
.map(|n| {
if n < i {
rerun::Color::from_rgb(20, 200, 20)
} else {
rerun::Color::from_rgb(200, 20, 20)
}
})
.collect();
let radii: Vec<rerun::components::Radius> = (0..10)
.map(|n| if n < i { 0.6 } else { 0.2 })
.map(Into::into)
.collect();

rec.set_time_sequence("frame", i);
rec.log("points", &[&colors as &dyn rerun::ComponentBatch, &radii])?;
}
}

// After
{
let rec =
// rerun::RecordingStreamBuilder::new("rerun_example_points3d_partial_updates").spawn()?;
// rerun::RecordingStreamBuilder::new("rerun_example_points3d_partial_updates_after").stdout()?;
rerun::RecordingStreamBuilder::new("rerun_example_points3d_partial_updates_before").connect_tcp()?;

let positions = (0..10)
.map(|_| (rng.sample(dist), rng.sample(dist), rng.sample(dist)))
.collect::<Vec<_>>();

rec.set_time_sequence("frame", 0);
rec.log("points", &rerun::Points3D::new(&positions))?;

for i in 0..10 {
let colors = (0..10).map(|n| {
if n < i {
rerun::Color::from_rgb(20, 200, 20)
} else {
rerun::Color::from_rgb(200, 20, 20)
}
});
let radii = (0..10).map(|n| if n < i { 0.6 } else { 0.2 });

rec.set_time_sequence("frame", i);
rec.log(
"points",
&rerun::Points3D::update_fields()
.with_radii(radii)
.with_colors(colors),
)?;
}

rec.set_time_sequence("frame", 20);
rec.log(
"points",
&rerun::Points3D::clear_fields()
.with_positions(positions)
.with_radii([0.3]),
)?;
}

Ok(())
}
4 changes: 4 additions & 0 deletions docs/snippets/snippets.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
# You should only ever use this if the test isn't implemented and cannot yet be implemented
# for one or more specific SDKs.
[opt_out.run]
"archetypes/points3d_partial_updates" = [ # TODO
"cpp",
"py",
]
"concepts/explicit_recording" = [ # python-specific check
"cpp",
"rust",
Expand Down

0 comments on commit 8959ac9

Please sign in to comment.