 684f07595f
			
		
	
	
		684f07595f
		
	
	
	
	
		
			
			# Objective Complete the first part of the migration detailed in bevyengine/rfcs#45. ## Solution Add all the new stuff. ### TODO - [x] Impl tuple methods. - [x] Impl chaining. - [x] Port ambiguity detection. - [x] Write docs. - [x] ~~Write more tests.~~(will do later) - [ ] Write changelog and examples here? - [x] ~~Replace `petgraph`.~~ (will do later) Co-authored-by: james7132 <contact@jamessliu.com> Co-authored-by: Michael Hsu <mike.hsu@gmail.com> Co-authored-by: Mike Hsu <mike.hsu@gmail.com>
		
			
				
	
	
		
			174 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			174 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! Traits used by label implementations
 | |
| 
 | |
| use std::{
 | |
|     any::Any,
 | |
|     hash::{Hash, Hasher},
 | |
| };
 | |
| 
 | |
| /// An object safe version of [`Eq`]. This trait is automatically implemented
 | |
| /// for any `'static` type that implements `Eq`.
 | |
| pub trait DynEq: Any {
 | |
|     /// Casts the type to `dyn Any`.
 | |
|     fn as_any(&self) -> &dyn Any;
 | |
| 
 | |
|     /// This method tests for `self` and `other` values to be equal.
 | |
|     ///
 | |
|     /// Implementers should avoid returning `true` when the underlying types are
 | |
|     /// not the same.
 | |
|     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
 | |
|     }
 | |
| }
 | |
| 
 | |
| /// An object safe version of [`Hash`]. This trait is automatically implemented
 | |
| /// for any `'static` type that implements `Hash`.
 | |
| pub trait DynHash: DynEq {
 | |
|     /// Casts the type to `dyn Any`.
 | |
|     fn as_dyn_eq(&self) -> &dyn DynEq;
 | |
| 
 | |
|     /// Feeds this value into the given [`Hasher`].
 | |
|     ///
 | |
|     /// [`Hash`]: std::hash::Hasher
 | |
|     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_boxed_label;
 | |
| /// define_boxed_label!(MyNewLabelTrait);
 | |
| /// ```
 | |
| #[macro_export]
 | |
| macro_rules! define_boxed_label {
 | |
|     ($label_trait_name:ident) => {
 | |
|         /// A strongly-typed label.
 | |
|         pub trait $label_trait_name:
 | |
|             'static + Send + Sync + ::std::fmt::Debug + ::bevy_utils::label::DynHash
 | |
|         {
 | |
|             #[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()
 | |
|             }
 | |
|         }
 | |
|     };
 | |
| }
 | |
| 
 | |
| /// Macro to define a new label trait
 | |
| ///
 | |
| /// # Example
 | |
| ///
 | |
| /// ```
 | |
| /// # use bevy_utils::define_label;
 | |
| /// define_label!(
 | |
| ///     /// A class of labels.
 | |
| ///     MyNewLabelTrait,
 | |
| ///     /// Identifies a value that implements `MyNewLabelTrait`.
 | |
| ///     MyNewLabelId,
 | |
| /// );
 | |
| /// ```
 | |
| #[macro_export]
 | |
| macro_rules! define_label {
 | |
|     (
 | |
|         $(#[$label_attr:meta])*
 | |
|         $label_name:ident,
 | |
| 
 | |
|         $(#[$id_attr:meta])*
 | |
|         $id_name:ident $(,)?
 | |
|     ) => {
 | |
|         $(#[$id_attr])*
 | |
|         #[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)
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         $(#[$label_attr])*
 | |
|         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
 | |
|             }
 | |
|         }
 | |
|     };
 | |
| }
 |