re_types_core/
component_descriptor.rsuse std::borrow::Cow;
use crate::{ArchetypeFieldName, ArchetypeName, ComponentName, SizeBytes};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ComponentDescriptor {
pub archetype_name: Option<ArchetypeName>,
pub archetype_field_name: Option<ArchetypeFieldName>,
pub component_name: ComponentName,
}
impl std::hash::Hash for ComponentDescriptor {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let Self {
archetype_name,
archetype_field_name,
component_name,
} = self;
let archetype_name = archetype_name.map_or(0, |v| v.hash());
let archetype_field_name = archetype_field_name.map_or(0, |v| v.hash());
let component_name = component_name.hash();
state.write_u64(archetype_name ^ archetype_field_name ^ component_name);
}
}
impl nohash_hasher::IsEnabled for ComponentDescriptor {}
impl std::fmt::Display for ComponentDescriptor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.to_any_string(false))
}
}
impl From<ComponentDescriptor> for Cow<'static, ComponentDescriptor> {
#[inline]
fn from(descr: ComponentDescriptor) -> Self {
Cow::Owned(descr)
}
}
impl<'d> From<&'d ComponentDescriptor> for Cow<'d, ComponentDescriptor> {
#[inline]
fn from(descr: &'d ComponentDescriptor) -> Self {
Cow::Borrowed(descr)
}
}
impl ComponentDescriptor {
fn to_any_string(&self, use_short_names: bool) -> String {
let Self {
archetype_name,
archetype_field_name,
component_name,
} = self;
let (archetype_name, component_name) = if use_short_names {
(
archetype_name.map(|s| s.short_name()),
component_name.short_name(),
)
} else {
(archetype_name.map(|s| s.as_str()), component_name.as_str())
};
match (archetype_name, component_name, archetype_field_name) {
(None, component_name, None) => component_name.to_owned(),
(Some(archetype_name), component_name, None) => {
format!("{archetype_name}:{component_name}")
}
(None, component_name, Some(archetype_field_name)) => {
format!("{component_name}#{archetype_field_name}")
}
(Some(archetype_name), component_name, Some(archetype_field_name)) => {
format!("{archetype_name}:{component_name}#{archetype_field_name}")
}
}
}
#[inline]
pub fn full_name(&self) -> String {
self.to_string()
}
#[inline]
pub fn short_name(&self) -> String {
self.to_any_string(true)
}
}
impl SizeBytes for ComponentDescriptor {
#[inline]
fn heap_size_bytes(&self) -> u64 {
let Self {
archetype_name,
archetype_field_name,
component_name,
} = self;
archetype_name.heap_size_bytes()
+ component_name.heap_size_bytes()
+ archetype_field_name.heap_size_bytes()
}
}
impl ComponentDescriptor {
#[inline]
pub fn new(component_name: impl Into<ComponentName>) -> Self {
let component_name = component_name.into();
Self {
archetype_name: None,
archetype_field_name: None,
component_name,
}
}
pub fn untagged(self) -> Self {
Self::new(self.component_name)
}
#[inline]
pub fn with_archetype_name(mut self, archetype_name: ArchetypeName) -> Self {
self.archetype_name = Some(archetype_name);
self
}
#[inline]
pub fn with_archetype_field_name(mut self, archetype_field_name: ArchetypeFieldName) -> Self {
self.archetype_field_name = Some(archetype_field_name);
self
}
#[inline]
pub fn or_with_archetype_name(mut self, archetype_name: impl Fn() -> ArchetypeName) -> Self {
if self.archetype_name.is_none() {
self.archetype_name = Some(archetype_name());
}
self
}
#[inline]
pub fn or_with_archetype_field_name(
mut self,
archetype_field_name: impl FnOnce() -> ArchetypeFieldName,
) -> Self {
if self.archetype_field_name.is_none() {
self.archetype_field_name = Some(archetype_field_name());
}
self
}
}