Skip to content

Commit

Permalink
re_viewer::reflection -> re_types::reflection (#8420)
Browse files Browse the repository at this point in the history
Make the reflection available to everyone.

* DNM: requires #8419
  • Loading branch information
teh-cmc authored Dec 12, 2024
1 parent 1a9135d commit d9b65f8
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 deletions.
35 changes: 23 additions & 12 deletions crates/build/re_types_builder/src/codegen/rust/reflection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn generate_reflection(
// Put into its own subfolder since codegen is set up in a way that it thinks that everything
// inside the folder is either generated or an extension to the generated code.
// This way we don't have to build an exception just for this file.
let path = Utf8PathBuf::from("crates/viewer/re_viewer/src/reflection/mod.rs");
let path = Utf8PathBuf::from("crates/store/re_types/src/reflection/mod.rs");

let mut imports = BTreeSet::new();
let component_reflection = generate_component_reflection(
Expand Down Expand Up @@ -60,9 +60,9 @@ pub fn generate_reflection(
SerializationError,
};

/// Generates reflection about all known components.
///
/// Call only once and reuse the results.
#[doc = "Generates reflection about all known components."]
#[doc = ""]
#[doc = "Call only once and reuse the results."]
pub fn generate_reflection() -> Result<Reflection, SerializationError> {
re_tracing::profile_function!();

Expand Down Expand Up @@ -95,10 +95,11 @@ fn generate_component_reflection(
.objects_of_kind(ObjectKind::Component)
.filter(|obj| !obj.is_testing())
{
let crate_name = patched_crate_name(&obj.crate_name());
if let Some(scope) = obj.scope() {
imports.insert(format!("{}::{scope}::components::*", obj.crate_name()));
imports.insert(format!("{crate_name}::{scope}::components::*"));
} else {
imports.insert(format!("{}::components::*", obj.crate_name()));
imports.insert(format!("{crate_name}::components::*"));
}

let type_name = format_ident!("{}", obj.name);
Expand Down Expand Up @@ -152,9 +153,9 @@ fn generate_component_reflection(
}

quote! {
/// Generates reflection about all known components.
///
/// Call only once and reuse the results.
#[doc = "Generates reflection about all known components."]
#[doc = ""]
#[doc = "Call only once and reuse the results."]
fn generate_component_reflection() -> Result<ComponentReflectionMap, SerializationError> {
re_tracing::profile_function!();
let array = [
Expand Down Expand Up @@ -246,9 +247,9 @@ fn generate_archetype_reflection(reporter: &Reporter, objects: &Objects) -> Toke
}

quote! {
/// Generates reflection about all known archetypes.
///
/// Call only once and reuse the results.
#[doc = "Generates reflection about all known archetypes."]
#[doc = ""]
#[doc = "Call only once and reuse the results."]
fn generate_archetype_reflection() -> ArchetypeReflectionMap {
re_tracing::profile_function!();
let array = [
Expand All @@ -258,3 +259,13 @@ fn generate_archetype_reflection(reporter: &Reporter, objects: &Objects) -> Toke
}
}
}

/// Returns `crate_name` as is, unless it's `re_types`, in which case it's replace by `crate`,
/// because that's where this code lives.
fn patched_crate_name(crate_name: &str) -> String {
if crate_name == "re_types" {
"crate".to_owned()
} else {
crate_name.to_owned()
}
}
2 changes: 1 addition & 1 deletion crates/store/re_chunk/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl ChunkComponents {
}

/// Returns all list arrays for the given component name.
///
///
/// I.e semantically equivalent to `get("MyComponent:*.*")`
#[inline]
pub fn get_by_component_name<'a>(
Expand Down
16 changes: 16 additions & 0 deletions crates/store/re_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,22 @@ pub mod datatypes {
/// The blueprint-specific components.
pub mod blueprint;

/// Run-time reflection for reading meta-data about components and archetypes.
pub mod reflection {

// Some reflection types are so fundamental and used everywhere that we want them to be
// exposed by `re_types_core` directly; that way we don't force a dependency on the `re_types`
// behemoth just so one can use one of these fundamental reflection types.
//
// To do so, re-inject `re_types_core`'s datatypes into our own module.

#[path = "../reflection/mod.rs"]
mod _reflection;

pub use self::_reflection::*;
pub use re_types_core::reflection::*;
}

// ---

// One almost never uses `re_types` without `re_types_core`, so we reexport these core types
Expand Down

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

2 changes: 1 addition & 1 deletion crates/viewer/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl App {

let panel_state_overrides = startup_options.panel_state_overrides;

let reflection = crate::reflection::generate_reflection().unwrap_or_else(|err| {
let reflection = re_types::reflection::generate_reflection().unwrap_or_else(|err| {
re_log::error!(
"Failed to create list of serialized default values for components: {err}"
);
Expand Down
6 changes: 3 additions & 3 deletions crates/viewer/re_viewer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ mod app_blueprint;
mod app_state;
mod background_tasks;
pub mod env_vars;
#[cfg(not(target_arch = "wasm32"))]
mod loading;
mod reflection;
mod saving;
mod screenshotter;
mod ui;
mod viewer_analytics;

#[cfg(not(target_arch = "wasm32"))]
mod loading;

/// Auto-generated blueprint-related types.
///
/// They all implement the [`re_types_core::Component`] trait.
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/check_large_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"crates/build/re_types_builder/src/reflection.rs",
"crates/store/re_dataframe/src/query.rs",
"crates/store/re_types/src/datatypes/tensor_buffer.rs",
"crates/store/re_types/src/reflection/mod.rs",
"crates/viewer/re_ui/data/Inter-Medium.otf",
"crates/viewer/re_viewer/src/reflection/mod.rs",
"docs/snippets/INDEX.md",
"pixi.lock",
"rerun_cpp/docs/Doxyfile",
Expand Down

0 comments on commit d9b65f8

Please sign in to comment.