bevy/crates/bevy_utils/src/label.rs

117 lines
3.1 KiB
Rust
Raw Normal View History

//! Traits used by label implementations
use std::{
any::Any,
hash::{Hash, Hasher},
};
pub trait DynEq: Any {
fn as_any(&self) -> &dyn Any;
fn dyn_eq(&self, other: &dyn DynEq) -> bool;
}
impl<T> DynEq for T
where
T: Any + Eq,
{
fn as_any(&self) -> &dyn Any {
self
}
fn dyn_eq(&self, other: &dyn DynEq) -> bool {
if let Some(other) = other.as_any().downcast_ref::<T>() {
return self == other;
}
false
}
}
pub trait DynHash: DynEq {
fn as_dyn_eq(&self) -> &dyn DynEq;
fn dyn_hash(&self, state: &mut dyn Hasher);
}
impl<T> DynHash for T
where
T: DynEq + Hash,
{
fn as_dyn_eq(&self) -> &dyn DynEq {
self
}
fn dyn_hash(&self, mut state: &mut dyn Hasher) {
T::hash(self, &mut state);
self.type_id().hash(&mut state);
}
}
#[doc(hidden)]
pub use concat_idents::concat_idents;
/// Macro to define a new label trait
///
/// # Example
///
/// ```
/// # use bevy_utils::define_label;
/// define_label!(MyNewLabelTrait);
/// ```
#[macro_export]
macro_rules! define_label {
($label_name:ident) => {
$crate::label::concat_idents!(id_name = $label_name, Id {
/// Stores one of a set of strongly-typed labels for a class of objects.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct id_name(::core::any::TypeId, &'static str);
impl ::core::fmt::Debug for id_name {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}", self.1)
}
}
/// Types that can be converted to a(n) [`id_name`].
///
/// Check the docs for [`define_label`](bevy_ecs::define_label) for more info.
pub trait $label_name: 'static {
/// Converts this type into an opaque, strongly-typed label.
fn as_label(&self) -> id_name {
let id = self.type_id();
let label = self.as_str();
id_name(id, label)
}
/// Returns the [`TypeId`] used to differentiate labels.
fn type_id(&self) -> ::core::any::TypeId {
::core::any::TypeId::of::<Self>()
}
/// Returns the representation of this label as a string literal.
///
/// In cases where you absolutely need a label to be determined at runtime,
/// you can use [`Box::leak`] to get a `'static` reference.
fn as_str(&self) -> &'static str;
}
impl $label_name for id_name {
fn as_label(&self) -> Self {
*self
}
fn type_id(&self) -> ::core::any::TypeId {
self.0
}
fn as_str(&self) -> &'static str {
self.1
}
}
impl $label_name for &'static str {
fn as_str(&self) -> Self {
self
}
}
});
};
}