# Objective We have some macros that are public but only used internally for now. They fail on user's code due to the use of crate names like `bevy_utils`, while the user only has `bevy::utils`. There are two affected macros. - `bevy_utils::define_label`: it may be useful in user's code for defining custom kinds of label traits (this is why I made this PR). - `bevy_asset::load_internal_asset`: not useful currently due to limitations of the debug asset server, but this may change in the future. ## Solution We can make them work by using `$crate` instead of names of their own crates, which can refer to the macro's defining crate regardless of the user's setup. Even though our objective is rather low-priority here, the solution adds no maintenance cost so it is still worthwhile.
102 lines
2.3 KiB
Rust
102 lines
2.3 KiB
Rust
//! 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);
|
|
}
|
|
}
|
|
|
|
/// Macro to define a new label trait
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```
|
|
/// # use bevy_utils::define_label;
|
|
/// define_label!(MyNewLabelTrait);
|
|
/// ```
|
|
#[macro_export]
|
|
macro_rules! define_label {
|
|
($label_trait_name:ident) => {
|
|
/// Defines a set of strongly-typed labels for a class of objects
|
|
pub trait $label_trait_name:
|
|
$crate::label::DynHash + ::std::fmt::Debug + Send + Sync + 'static
|
|
{
|
|
#[doc(hidden)]
|
|
fn dyn_clone(&self) -> Box<dyn $label_trait_name>;
|
|
}
|
|
|
|
impl PartialEq for dyn $label_trait_name {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.dyn_eq(other.as_dyn_eq())
|
|
}
|
|
}
|
|
|
|
impl Eq for dyn $label_trait_name {}
|
|
|
|
impl ::std::hash::Hash for dyn $label_trait_name {
|
|
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
|
|
self.dyn_hash(state);
|
|
}
|
|
}
|
|
|
|
impl Clone for Box<dyn $label_trait_name> {
|
|
fn clone(&self) -> Self {
|
|
self.dyn_clone()
|
|
}
|
|
}
|
|
|
|
impl $label_trait_name for ::std::borrow::Cow<'static, str> {
|
|
fn dyn_clone(&self) -> Box<dyn $label_trait_name> {
|
|
Box::new(self.clone())
|
|
}
|
|
}
|
|
|
|
impl $label_trait_name for &'static str {
|
|
fn dyn_clone(&self) -> Box<dyn $label_trait_name> {
|
|
Box::new(<&str>::clone(self))
|
|
}
|
|
}
|
|
};
|
|
}
|