Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanity-check that an archetype has a component #7330

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions crates/store/re_types_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,50 @@ pub mod external {
pub use arrow2;
pub use re_tuid;
}

/// Useful macro for staticlly asserting that a `struct` contains some specific fields.
///
/// In particular, this is useful to statcially check that an archetype
/// has a specific component.
///
/// ```
/// # #[macro_use] extern crate re_types_core;
/// struct Data {
/// x: f32,
/// y: String,
/// z: u32,
/// }
///
/// static_assert_struct_has_fields!(Data, x: f32, y: String);
/// ```
///
/// This will fail to compile because the type is wrong:
///
/// ```compile_fail
/// # #[macro_use] extern crate re_types_core;
/// struct Data {
/// x: f32,
/// }
///
/// static_assert_struct_has_fields!(Data, x: u32);
/// ```
///
/// This will fail to compile because the field is missing:
///
/// ```compile_fail
/// # #[macro_use] extern crate re_types_core;
/// struct Data {
/// x: f32,
/// }
///
/// static_assert_struct_has_fields!(Data, nosuch: f32);
/// ```
///
#[macro_export]
macro_rules! static_assert_struct_has_fields {
($strct:ty, $($field:ident: $field_typ:ty),+) => {
const _: fn(&$strct) = |s: &$strct| {
$(let _: &$field_typ = &s.$field;)+
};
}
}
19 changes: 18 additions & 1 deletion crates/viewer/re_data_ui/src/instance_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use re_types::{
archetypes, components,
datatypes::{ChannelDatatype, ColorModel},
image::ImageKind,
Archetype, ComponentName, Loggable,
static_assert_struct_has_fields, Archetype, ComponentName, Loggable,
};
use re_ui::{ContextExt as _, UiExt as _};
use re_viewer_context::{
Expand Down Expand Up @@ -251,6 +251,23 @@ fn preview_if_image_ui(
entity_path: &re_log_types::EntityPath,
component_map: &IntMap<ComponentName, UnitChunkShared>,
) -> Option<()> {
// First check assumptions:
static_assert_struct_has_fields!(
archetypes::Image,
buffer: components::ImageBuffer,
format: components::ImageFormat
);
static_assert_struct_has_fields!(
archetypes::DepthImage,
buffer: components::ImageBuffer,
format: components::ImageFormat
);
static_assert_struct_has_fields!(
archetypes::SegmentationImage,
buffer: components::ImageBuffer,
format: components::ImageFormat
);

let image_buffer = component_map.get(&components::ImageBuffer::name())?;
let buffer_row_id = image_buffer.row_id()?;
let image_buffer = image_buffer
Expand Down
12 changes: 10 additions & 2 deletions crates/viewer/re_space_view_spatial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ use re_space_view::DataResultQuery as _;
use re_viewer_context::{ImageDecodeCache, ViewContext, ViewerContext};

use re_renderer::RenderContext;
use re_types::components::{Color, MediaType, Resolution};
use re_types::{blueprint::components::BackgroundKind, components::ImageFormat};
use re_types::{
archetypes,
blueprint::components::BackgroundKind,
components::{self, Color, ImageFormat, MediaType, Resolution},
static_assert_struct_has_fields,
};
use re_viewport_blueprint::{ViewProperty, ViewPropertyQueryError};

mod view_kind {
Expand All @@ -57,6 +61,10 @@ fn resolution_of_image_at(
query: &re_chunk_store::LatestAtQuery,
entity_path: &re_log_types::EntityPath,
) -> Option<Resolution> {
// First check assumptions:
static_assert_struct_has_fields!(archetypes::Image, format: components::ImageFormat);
static_assert_struct_has_fields!(archetypes::EncodedImage, blob: components::Blob);

let db = ctx.recording();

if let Some((_, image_format)) = db.latest_at_component::<ImageFormat>(entity_path, query) {
Expand Down
Loading