Skip to content

Commit

Permalink
automatically implement partial updates APIs for eager archetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jan 10, 2025
1 parent cb797a8 commit a1331e1
Show file tree
Hide file tree
Showing 2 changed files with 93 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.

0 comments on commit a1331e1

Please sign in to comment.