re_types_core/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
//! The core types and traits that power Rerun's data model.
//!
//! The [`Archetype`] trait is the core of this crate and is a good starting point to get familiar
//! with the code.
//! An archetype is a logical collection of batches of [`Component`]s that play well with each other.
//!
//! Rerun (and the underlying Arrow data framework) is designed to work with large arrays of
//! [`Component`]s, as opposed to single instances.
//! When multiple instances of a [`Component`] are put together in an array, they yield a
//! [`ComponentBatch`]: the atomic unit of (de)serialization.
//!
//! Internally, [`Component`]s are implemented using many different [`Loggable`]s.
//!
//! ## Feature flags
#![doc = document_features::document_features!()]
//!
// TODO(#6330): remove unwrap()
#![allow(clippy::unwrap_used)]
// ---
/// Number of decimals shown for all float display methods.
pub const DEFAULT_DISPLAY_DECIMALS: usize = 3;
/// Describes the interface for interpreting an object as a bundle of [`Component`]s.
///
/// ## Custom bundles
///
/// While, in most cases, component bundles are code generated from our [IDL definitions],
/// it is possible to manually extend existing bundles, or even implement fully custom ones.
///
/// All [`AsComponents`] methods are optional to implement, with the exception of
/// [`AsComponents::as_component_batches`], which describes how the bundle can be interpreted
/// as a set of [`ComponentBatch`]es: arrays of components that are ready to be serialized.
///
/// Have a look at our [Custom Data Loader] example to learn more about handwritten bundles.
///
/// [IDL definitions]: https://github.com/rerun-io/rerun/tree/latest/crates/store/re_types/definitions/rerun
/// [Custom Data Loader]: https://github.com/rerun-io/rerun/blob/latest/examples/rust/custom_data_loader
pub trait AsComponents {
/// Exposes the object's contents as a set of [`ComponentBatch`]s.
///
/// This is the main mechanism for easily extending builtin archetypes or even writing
/// fully custom ones.
/// Have a look at our [Custom Data Loader] example to learn more about extending archetypes.
///
/// Implementers of [`AsComponents`] get one last chance to override the tags in the
/// [`ComponentDescriptor`], see [`ComponentBatchCowWithDescriptor::descriptor_override`].
///
/// [Custom Data Loader]: https://github.com/rerun-io/rerun/tree/latest/examples/rust/custom_data_loader
//
// NOTE: Don't bother returning a CoW here: we need to dynamically discard optional components
// depending on their presence (or lack thereof) at runtime anyway.
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>>;
// ---
/// Serializes all non-null [`Component`]s of this bundle into Arrow arrays.
///
/// The default implementation will simply serialize the result of [`Self::as_component_batches`]
/// as-is, which is what you want in 99.9% of cases.
#[inline]
fn to_arrow2(
&self,
) -> SerializationResult<Vec<(::arrow2::datatypes::Field, Box<dyn ::arrow2::array::Array>)>>
{
self.as_component_batches()
.into_iter()
.map(|comp_batch| {
comp_batch
.to_arrow2()
.map(|array| {
let field = arrow2::datatypes::Field::new(
comp_batch.name().to_string(),
array.data_type().clone(),
false,
);
(field, array)
})
.with_context(comp_batch.name())
})
.collect()
}
}
impl<C: Component> AsComponents for C {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
vec![ComponentBatchCowWithDescriptor::new(
self as &dyn ComponentBatch,
)]
}
}
impl AsComponents for dyn ComponentBatch {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
vec![ComponentBatchCowWithDescriptor::new(self)]
}
}
impl<const N: usize> AsComponents for [&dyn ComponentBatch; N] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.map(|batch| ComponentBatchCowWithDescriptor::new(*batch))
.collect()
}
}
impl<const N: usize> AsComponents for [Box<dyn ComponentBatch>; N] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.map(|batch| ComponentBatchCowWithDescriptor::new(&**batch))
.collect()
}
}
impl AsComponents for &[&dyn ComponentBatch] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.map(|batch| ComponentBatchCowWithDescriptor::new(*batch))
.collect()
}
}
impl AsComponents for &[Box<dyn ComponentBatch>] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.map(|batch| ComponentBatchCowWithDescriptor::new(&**batch))
.collect()
}
}
impl AsComponents for Vec<&dyn ComponentBatch> {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.map(|batch| ComponentBatchCowWithDescriptor::new(*batch))
.collect()
}
}
impl AsComponents for Vec<Box<dyn ComponentBatch>> {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.map(|batch| ComponentBatchCowWithDescriptor::new(&**batch))
.collect()
}
}
impl<AS: AsComponents, const N: usize> AsComponents for [AS; N] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.flat_map(|as_components| as_components.as_component_batches())
.collect()
}
}
impl<const N: usize> AsComponents for [&dyn AsComponents; N] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.flat_map(|as_components| as_components.as_component_batches())
.collect()
}
}
impl<const N: usize> AsComponents for [Box<dyn AsComponents>; N] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.flat_map(|as_components| as_components.as_component_batches())
.collect()
}
}
impl<AS: AsComponents> AsComponents for &[AS] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.flat_map(|as_components| as_components.as_component_batches())
.collect()
}
}
impl AsComponents for &[&dyn AsComponents] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.flat_map(|as_components| as_components.as_component_batches())
.collect()
}
}
impl AsComponents for &[Box<dyn AsComponents>] {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.flat_map(|as_components| as_components.as_component_batches())
.collect()
}
}
impl<AS: AsComponents> AsComponents for Vec<AS> {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.flat_map(|as_components| as_components.as_component_batches())
.collect()
}
}
impl AsComponents for Vec<&dyn AsComponents> {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.flat_map(|as_components| as_components.as_component_batches())
.collect()
}
}
impl AsComponents for Vec<Box<dyn AsComponents>> {
#[inline]
fn as_component_batches(&self) -> Vec<ComponentBatchCowWithDescriptor<'_>> {
self.iter()
.flat_map(|as_components| as_components.as_component_batches())
.collect()
}
}
// ---
mod archetype;
mod arrow_buffer;
pub mod arrow_helpers;
mod arrow_string;
mod component_descriptor;
mod loggable;
mod loggable_batch;
pub mod reflection;
mod result;
mod size_bytes;
mod tuid;
mod view;
pub use self::{
archetype::{
Archetype, ArchetypeFieldName, ArchetypeName, ArchetypeReflectionMarker,
GenericIndicatorComponent, NamedIndicatorComponent,
},
arrow_buffer::ArrowBuffer,
arrow_string::ArrowString,
component_descriptor::ComponentDescriptor,
loggable::{
Component, ComponentName, ComponentNameSet, DatatypeName, Loggable,
UnorderedComponentNameSet,
},
loggable_batch::{
ComponentBatch, ComponentBatchCow, ComponentBatchCowWithDescriptor, LoggableBatch,
},
result::{
DeserializationError, DeserializationResult, ResultExt, SerializationError,
SerializationResult, _Backtrace,
},
size_bytes::SizeBytes,
view::{View, ViewClassIdentifier},
};
/// Fundamental [`Archetype`]s that are implemented in `re_types_core` directly for convenience and
/// dependency optimization.
///
/// There are also re-exported by `re_types`.
pub mod archetypes;
/// Fundamental [`Component`]s that are implemented in `re_types_core` directly for convenience and
/// dependency optimization.
///
/// There are also re-exported by `re_types`.
pub mod components;
/// Fundamental datatypes that are implemented in `re_types_core` directly for convenience and
/// dependency optimization.
///
/// There are also re-exported by `re_types`.
pub mod datatypes;
// ---
#[path = "macros.rs"]
mod _macros; // just for the side-effect of exporting the macros
pub mod macros {
pub use super::impl_into_cow;
}
pub mod external {
pub use anyhow;
pub use arrow;
pub use arrow2;
pub use re_tuid;
}
/// Useful macro for statically 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;)+
};
}
}