resman/
resource.rs

1use std::any::{Any, TypeId};
2
3use downcast_rs::DowncastSync;
4
5/// Trait to represent any type that is `Send + Sync + 'static`.
6///
7/// A resource is a data slot which lives in the `World` can only be accessed
8/// according to Rust's typical borrowing model (one writer xor multiple
9/// readers).
10#[cfg(not(feature = "debug"))]
11pub trait Resource: DowncastSync + 'static {
12    fn type_id(&self) -> TypeId;
13    fn type_name(&self) -> TypeNameLit;
14}
15
16#[cfg(not(feature = "debug"))]
17impl<T> Resource for T
18where
19    T: Any + Send + Sync,
20{
21    fn type_id(&self) -> TypeId {
22        TypeId::of::<T>()
23    }
24
25    fn type_name(&self) -> TypeNameLit {
26        TypeNameLit(std::any::type_name::<T>())
27    }
28}
29
30/// Trait to represent any type that is `Send + Sync + 'static`.
31///
32/// A resource is a data slot which lives in the `World` can only be accessed
33/// according to Rust's typical borrowing model (one writer xor multiple
34/// readers).
35#[cfg(feature = "debug")]
36pub trait Resource: DowncastSync + std::fmt::Debug + 'static {
37    fn type_id(&self) -> TypeId;
38    fn type_name(&self) -> TypeNameLit;
39}
40
41#[cfg(feature = "debug")]
42impl<T> Resource for T
43where
44    T: Any + std::fmt::Debug + Send + Sync,
45{
46    fn type_id(&self) -> TypeId {
47        TypeId::of::<T>()
48    }
49
50    fn type_name(&self) -> TypeNameLit {
51        TypeNameLit(std::any::type_name::<T>())
52    }
53}
54
55downcast_rs::impl_downcast!(sync Resource);
56
57use std::fmt;
58
59pub struct TypeNameLit(&'static str);
60
61impl fmt::Debug for TypeNameLit {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        write!(f, "{}", self.0)
64    }
65}