bevy_reflect/is.rs
1use core::any::{Any, TypeId};
2
3/// Checks if the current type "is" another type, using a [`TypeId`] equality comparison.
4pub trait Is {
5 /// Checks if the current type "is" another type, using a [`TypeId`] equality comparison.
6 /// This is most useful in the context of generic logic.
7 ///
8 /// ```
9 /// # use bevy_reflect::Is;
10 /// # use std::any::Any;
11 /// fn greet_if_u32<T: Any>() {
12 /// if T::is::<u32>() {
13 /// println!("Hello");
14 /// }
15 /// }
16 /// // this will print "Hello"
17 /// greet_if_u32::<u32>();
18 /// // this will not print "Hello"
19 /// greet_if_u32::<String>();
20 /// assert!(u32::is::<u32>());
21 /// assert!(!usize::is::<u32>());
22 /// ```
23 fn is<T: Any>() -> bool;
24}
25
26impl<A: Any> Is for A {
27 #[inline]
28 fn is<T: Any>() -> bool {
29 TypeId::of::<A>() == TypeId::of::<T>()
30 }
31}