From a1331e115c6d22dc92f7c035af4f1c5986bdc1f4 Mon Sep 17 00:00:00 2001 From: Clement Rey Date: Thu, 9 Jan 2025 19:12:28 +0100 Subject: [PATCH] automatically implement partial updates APIs for eager archetypes --- .../re_types_builder/src/codegen/rust/api.rs | 43 ++++++++++++++- .../store/re_types/src/archetypes/points3d.rs | 52 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/crates/build/re_types_builder/src/codegen/rust/api.rs b/crates/build/re_types_builder/src/codegen/rust/api.rs index c290df9e4230..fc7726998799 100644 --- a/crates/build/re_types_builder/src/codegen/rust/api.rs +++ b/crates/build/re_types_builder/src/codegen/rust/api.rs @@ -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}"); @@ -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)*) }; diff --git a/crates/store/re_types/src/archetypes/points3d.rs b/crates/store/re_types/src/archetypes/points3d.rs index c44716f9489c..c540dcdc24a6 100644 --- a/crates/store/re_types/src/archetypes/points3d.rs +++ b/crates/store/re_types/src/archetypes/points3d.rs @@ -382,6 +382,58 @@ impl Points3D { } } + /// Update only some specific fields of a `Points3D`. + #[inline] + pub fn update_fields() -> Self { + Self::default() + } + + /// Clear all the fields of a `Points3D`. + #[inline] + pub fn clear_fields() -> Self { + use ::re_types_core::Loggable as _; + Self { + positions: Some(SerializedComponentBatch::new( + crate::components::Position3D::arrow_empty(), + Self::descriptor_positions(), + )), + radii: Some(SerializedComponentBatch::new( + crate::components::Radius::arrow_empty(), + Self::descriptor_radii(), + )), + colors: Some(SerializedComponentBatch::new( + crate::components::Color::arrow_empty(), + Self::descriptor_colors(), + )), + labels: Some(SerializedComponentBatch::new( + crate::components::Text::arrow_empty(), + Self::descriptor_labels(), + )), + show_labels: Some(SerializedComponentBatch::new( + crate::components::ShowLabels::arrow_empty(), + Self::descriptor_show_labels(), + )), + class_ids: Some(SerializedComponentBatch::new( + crate::components::ClassId::arrow_empty(), + Self::descriptor_class_ids(), + )), + keypoint_ids: Some(SerializedComponentBatch::new( + crate::components::KeypointId::arrow_empty(), + Self::descriptor_keypoint_ids(), + )), + } + } + + /// All the 3D positions at which the point cloud shows points. + #[inline] + pub fn with_positions( + mut self, + positions: impl IntoIterator>, + ) -> Self { + self.positions = try_serialize_field(Self::descriptor_positions(), positions); + self + } + /// Optional radii for the points, effectively turning them into circles. #[inline] pub fn with_radii(