Skip to content

Commit 05f5cb0

Browse files
committed
End-to-end tagging for everything Rust
1 parent 0e9d221 commit 05f5cb0

File tree

343 files changed

+17880
-11110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

343 files changed

+17880
-11110
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -8021,6 +8021,7 @@ dependencies = [
80218021
"re_build_tools",
80228022
"rerun",
80238023
"rust-format",
8024+
"similar-asserts",
80248025
]
80258026

80268027
[[package]]

crates/build/re_types_builder/src/codegen/rust/api.rs

+111-62
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
should_optimize_buffer_slice_deserialize,
1717
},
1818
serializer::quote_arrow_serializer,
19-
util::{is_tuple_struct_from_obj, iter_archetype_components, quote_doc_line},
19+
util::{is_tuple_struct_from_obj, quote_doc_line},
2020
},
2121
Target,
2222
},
@@ -184,7 +184,7 @@ fn generate_object_file(
184184
code.push_str("use ::re_types_core::external::arrow2;\n");
185185
code.push_str("use ::re_types_core::SerializationResult;\n");
186186
code.push_str("use ::re_types_core::{DeserializationResult, DeserializationError};\n");
187-
code.push_str("use ::re_types_core::ComponentName;\n");
187+
code.push_str("use ::re_types_core::{ComponentDescriptor, ComponentName};\n");
188188
code.push_str("use ::re_types_core::{ComponentBatch, MaybeOwnedComponentBatch};\n");
189189

190190
// NOTE: `TokenStream`s discard whitespacing information by definition, so we need to
@@ -354,13 +354,13 @@ fn quote_struct(
354354
#quoted_deprecation_notice
355355
#quoted_struct
356356

357-
#quoted_heap_size_bytes
357+
#quoted_trait_impls
358358

359359
#quoted_from_impl
360360

361-
#quoted_trait_impls
362-
363361
#quoted_builder
362+
363+
#quoted_heap_size_bytes
364364
};
365365

366366
tokens
@@ -470,9 +470,9 @@ fn quote_union(
470470
#(#quoted_fields,)*
471471
}
472472

473-
#quoted_heap_size_bytes
474-
475473
#quoted_trait_impls
474+
475+
#quoted_heap_size_bytes
476476
};
477477

478478
tokens
@@ -603,6 +603,18 @@ fn quote_enum(
603603
#(#quoted_fields,)*
604604
}
605605

606+
#quoted_trait_impls
607+
608+
// We implement `Display` to match the `PascalCase` name so that
609+
// the enum variants are displayed in the UI exactly how they are displayed in code.
610+
impl std::fmt::Display for #name {
611+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
612+
match self {
613+
#(#display_match_arms,)*
614+
}
615+
}
616+
}
617+
606618
impl ::re_types_core::reflection::Enum for #name {
607619

608620
#[inline]
@@ -629,18 +641,6 @@ fn quote_enum(
629641
true
630642
}
631643
}
632-
633-
// We implement `Display` to match the `PascalCase` name so that
634-
// the enum variants are displayed in the UI exactly how they are displayed in code.
635-
impl std::fmt::Display for #name {
636-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
637-
match self {
638-
#(#display_match_arms,)*
639-
}
640-
}
641-
}
642-
643-
#quoted_trait_impls
644644
};
645645

646646
tokens
@@ -1006,14 +1006,16 @@ fn quote_trait_impls_for_datatype_or_component(
10061006
quote! {
10071007
impl ::re_types_core::Component for #name {
10081008
#[inline]
1009-
fn name() -> ComponentName {
1010-
#fqname.into()
1009+
fn descriptor() -> ComponentDescriptor {
1010+
ComponentDescriptor::new(#fqname)
10111011
}
10121012
}
10131013
}
10141014
});
10151015

10161016
quote! {
1017+
#quoted_impl_component
1018+
10171019
::re_types_core::macros::impl_into_cow!(#name);
10181020

10191021
impl ::re_types_core::Loggable for #name {
@@ -1033,8 +1035,6 @@ fn quote_trait_impls_for_datatype_or_component(
10331035

10341036
#quoted_from_arrow2
10351037
}
1036-
1037-
#quoted_impl_component
10381038
}
10391039
}
10401040

@@ -1048,40 +1048,70 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
10481048
assert_eq!(kind, &ObjectKind::Archetype);
10491049

10501050
let display_name = re_case::to_human_case(name);
1051+
let archetype_name = &obj.fqname;
10511052
let name = format_ident!("{name}");
10521053

1053-
fn compute_components(
1054+
fn compute_component_descriptors(
10541055
obj: &Object,
1055-
attr: &'static str,
1056-
extras: impl IntoIterator<Item = String>,
1056+
requirement_attr_value: &'static str,
10571057
) -> (usize, TokenStream) {
1058-
let components = iter_archetype_components(obj, attr)
1059-
.chain(extras)
1060-
// Do *not* sort again, we want to preserve the order given by the datatype definition
1061-
.collect::<Vec<_>>();
1058+
let descriptors = obj
1059+
.fields
1060+
.iter()
1061+
.filter_map(move |field| {
1062+
field
1063+
.try_get_attr::<String>(requirement_attr_value)
1064+
.map(|_| {
1065+
let Some(component_name) = field.typ.fqname() else {
1066+
panic!("Archetype field must be an object/union or an array/vector of such")
1067+
};
1068+
1069+
let archetype_name = &obj.fqname;
1070+
let archetype_field_name = field.snake_case_name();
1071+
1072+
quote!(ComponentDescriptor {
1073+
archetype_name: Some(#archetype_name.into()),
1074+
component_name: #component_name.into(),
1075+
archetype_field_name: Some(#archetype_field_name.into()),
1076+
})
1077+
})
1078+
})
1079+
.collect_vec();
10621080

1063-
let num_components = components.len();
1064-
let quoted_components = quote!(#(#components.into(),)*);
1081+
let num_descriptors = descriptors.len();
1082+
let quoted_descriptors = quote!(#(#descriptors,)*);
10651083

1066-
(num_components, quoted_components)
1084+
(num_descriptors, quoted_descriptors)
10671085
}
10681086

10691087
let indicator_name = format!("{}Indicator", obj.name);
1070-
let indicator_fqname = format!("{}Indicator", obj.fqname).replace("archetypes", "components");
10711088

10721089
let quoted_indicator_name = format_ident!("{indicator_name}");
10731090
let quoted_indicator_doc =
10741091
format!("Indicator component for the [`{name}`] [`::re_types_core::Archetype`]");
10751092

1076-
let (num_required, required) = compute_components(obj, ATTR_RERUN_COMPONENT_REQUIRED, []);
1077-
let (num_recommended, recommended) =
1078-
compute_components(obj, ATTR_RERUN_COMPONENT_RECOMMENDED, [indicator_fqname]);
1079-
let (num_optional, optional) = compute_components(obj, ATTR_RERUN_COMPONENT_OPTIONAL, []);
1093+
let (num_required_descriptors, required_descriptors) =
1094+
compute_component_descriptors(obj, ATTR_RERUN_COMPONENT_REQUIRED);
1095+
let (mut num_recommended_descriptors, mut recommended_descriptors) =
1096+
compute_component_descriptors(obj, ATTR_RERUN_COMPONENT_RECOMMENDED);
1097+
let (num_optional_descriptors, optional_descriptors) =
1098+
compute_component_descriptors(obj, ATTR_RERUN_COMPONENT_OPTIONAL);
1099+
1100+
num_recommended_descriptors += 1;
1101+
recommended_descriptors = quote! {
1102+
#recommended_descriptors
1103+
ComponentDescriptor {
1104+
archetype_name: Some(#archetype_name.into()),
1105+
component_name: #indicator_name.into(),
1106+
archetype_field_name: None,
1107+
},
1108+
};
10801109

10811110
let num_components_docstring = quote_doc_line(&format!(
1082-
"The total number of components in the archetype: {num_required} required, {num_recommended} recommended, {num_optional} optional"
1111+
"The total number of components in the archetype: {num_required_descriptors} required, {num_recommended_descriptors} recommended, {num_optional_descriptors} optional"
10831112
));
1084-
let num_all = num_required + num_recommended + num_optional;
1113+
let num_all_descriptors =
1114+
num_required_descriptors + num_recommended_descriptors + num_optional_descriptors;
10851115

10861116
let quoted_field_names = obj
10871117
.fields
@@ -1099,13 +1129,13 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
10991129

11001130
// NOTE: The nullability we're dealing with here is the nullability of an entire array of components,
11011131
// not the nullability of individual elements (i.e. instances)!
1102-
if is_nullable {
1132+
let batch = if is_nullable {
11031133
if obj.attrs.has(ATTR_RERUN_LOG_MISSING_AS_EMPTY) {
11041134
if is_plural {
11051135
// Always log Option<Vec<C>> as Vec<V>, mapping None to empty batch
11061136
let component_type = quote_field_type_from_typ(&obj_field.typ, false).0;
11071137
quote!{
1108-
Some((
1138+
Some(
11091139
if let Some(comp_batch) = &self.#field_name {
11101140
(comp_batch as &dyn ComponentBatch)
11111141
} else {
@@ -1114,24 +1144,44 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
11141144
let empty_batch: &#component_type = EMPTY_BATCH.get_or_init(|| Vec::new());
11151145
(empty_batch as &dyn ComponentBatch)
11161146
}
1117-
).into())
1147+
)
11181148
}
11191149
} else {
11201150
// Always log Option<C>, mapping None to empty batch
1121-
quote!{ Some((&self.#field_name as &dyn ComponentBatch).into()) }
1151+
quote!{ Some(&self.#field_name as &dyn ComponentBatch) }
11221152
}
11231153
} else {
11241154
if is_plural {
11251155
// Maybe logging an Option<Vec<C>>
1126-
quote!{ self.#field_name.as_ref().map(|comp_batch| (comp_batch as &dyn ComponentBatch).into()) }
1156+
quote!{ self.#field_name.as_ref().map(|comp_batch| (comp_batch as &dyn ComponentBatch)) }
11271157
} else {
11281158
// Maybe logging an Option<C>
1129-
quote!{ self.#field_name.as_ref().map(|comp| (comp as &dyn ComponentBatch).into()) }
1159+
quote!{ self.#field_name.as_ref().map(|comp| (comp as &dyn ComponentBatch)) }
11301160
}
11311161
}
11321162
} else {
11331163
// Always logging a Vec<C> or C
1134-
quote!{ Some((&self.#field_name as &dyn ComponentBatch).into()) }
1164+
quote!{ Some(&self.#field_name as &dyn ComponentBatch) }
1165+
};
1166+
1167+
let Some(component_name) = obj_field.typ.fqname() else {
1168+
panic!("Archetype field must be an object/union or an array/vector of such")
1169+
};
1170+
let archetype_name = &obj.fqname;
1171+
let archetype_field_name = obj_field.snake_case_name();
1172+
1173+
quote! {
1174+
(#batch).map(|batch| {
1175+
::re_types_core::MaybeOwnedComponentBatch {
1176+
batch: batch.into(),
1177+
descriptor_override: Some(ComponentDescriptor {
1178+
archetype_name: Some(#archetype_name.into()),
1179+
archetype_field_name: Some((#archetype_field_name).into()),
1180+
component_name: (#component_name).into(),
1181+
}),
1182+
}
1183+
})
1184+
11351185
}
11361186
}))
11371187
};
@@ -1215,21 +1265,21 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
12151265
};
12161266

12171267
quote! {
1218-
static REQUIRED_COMPONENTS: once_cell::sync::Lazy<[ComponentName; #num_required]> =
1219-
once_cell::sync::Lazy::new(|| {[#required]});
1268+
static REQUIRED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; #num_required_descriptors]> =
1269+
once_cell::sync::Lazy::new(|| {[#required_descriptors]});
12201270

1221-
static RECOMMENDED_COMPONENTS: once_cell::sync::Lazy<[ComponentName; #num_recommended]> =
1222-
once_cell::sync::Lazy::new(|| {[#recommended]});
1271+
static RECOMMENDED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; #num_recommended_descriptors]> =
1272+
once_cell::sync::Lazy::new(|| {[#recommended_descriptors]});
12231273

1224-
static OPTIONAL_COMPONENTS: once_cell::sync::Lazy<[ComponentName; #num_optional]> =
1225-
once_cell::sync::Lazy::new(|| {[#optional]});
1274+
static OPTIONAL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; #num_optional_descriptors]> =
1275+
once_cell::sync::Lazy::new(|| {[#optional_descriptors]});
12261276

1227-
static ALL_COMPONENTS: once_cell::sync::Lazy<[ComponentName; #num_all]> =
1228-
once_cell::sync::Lazy::new(|| {[#required #recommended #optional]});
1277+
static ALL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; #num_all_descriptors]> =
1278+
once_cell::sync::Lazy::new(|| {[#required_descriptors #recommended_descriptors #optional_descriptors]});
12291279

12301280
impl #name {
12311281
#num_components_docstring
1232-
pub const NUM_COMPONENTS: usize = #num_all;
1282+
pub const NUM_COMPONENTS: usize = #num_all_descriptors;
12331283
}
12341284

12351285
#[doc = #quoted_indicator_doc]
@@ -1251,27 +1301,27 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
12511301
#[inline]
12521302
fn indicator() -> MaybeOwnedComponentBatch<'static> {
12531303
static INDICATOR: #quoted_indicator_name = #quoted_indicator_name::DEFAULT;
1254-
MaybeOwnedComponentBatch::Ref(&INDICATOR)
1304+
MaybeOwnedComponentBatch::new(&INDICATOR as &dyn ::re_types_core::ComponentBatch)
12551305
}
12561306

12571307
#[inline]
1258-
fn required_components() -> ::std::borrow::Cow<'static, [ComponentName]> {
1308+
fn required_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
12591309
REQUIRED_COMPONENTS.as_slice().into()
12601310
}
12611311

12621312
#[inline]
1263-
fn recommended_components() -> ::std::borrow::Cow<'static, [ComponentName]> {
1313+
fn recommended_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
12641314
RECOMMENDED_COMPONENTS.as_slice().into()
12651315
}
12661316

12671317
#[inline]
1268-
fn optional_components() -> ::std::borrow::Cow<'static, [ComponentName]> {
1318+
fn optional_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
12691319
OPTIONAL_COMPONENTS.as_slice().into()
12701320
}
12711321

12721322
// NOTE: Don't rely on default implementation so that we can keep everything static.
12731323
#[inline]
1274-
fn all_components() -> ::std::borrow::Cow<'static, [ComponentName]> {
1324+
fn all_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
12751325
ALL_COMPONENTS.as_slice().into()
12761326
}
12771327

@@ -1306,7 +1356,6 @@ fn quote_trait_impls_for_archetype(obj: &Object) -> TokenStream {
13061356
re_tracing::profile_function!();
13071357

13081358
use ::re_types_core::Archetype as _;
1309-
13101359
[#(#all_component_batches,)*].into_iter().flatten().collect()
13111360
}
13121361
}

crates/build/re_types_builder/src/codegen/rust/util.rs

-19
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,6 @@ pub fn is_tuple_struct_from_obj(obj: &Object) -> bool {
5050
is_tuple_struct
5151
}
5252

53-
pub fn iter_archetype_components<'a>(
54-
obj: &'a Object,
55-
requirement_attr_value: &'static str,
56-
) -> impl Iterator<Item = String> + 'a {
57-
assert_eq!(ObjectKind::Archetype, obj.kind);
58-
59-
obj.fields.iter().filter_map(move |field| {
60-
field
61-
.try_get_attr::<String>(requirement_attr_value)
62-
.map(|_| {
63-
if let Some(fqname) = field.typ.fqname() {
64-
fqname.to_owned()
65-
} else {
66-
panic!("Archetype field must be an object/union or an array/vector of such")
67-
}
68-
})
69-
})
70-
}
71-
7253
pub fn string_from_quoted(
7354
reporter: &Reporter,
7455
acc: &TokenStream,

0 commit comments

Comments
 (0)