async_ecs/resource/
mod.rs

1pub mod cell;
2pub mod entry;
3pub mod resources;
4
5pub use cell::Cell;
6pub use resources::{Ref, RefMut, Resources};
7
8use std::any::TypeId;
9
10use mopa::Any;
11
12/// A resource is a data slot which lives in the `World` can only be accessed
13/// according to Rust's typical borrowing model (one writer xor multiple
14/// readers).
15pub trait Resource: Any + Send + Sync + 'static {}
16
17/// The id of a [`Resource`], which simply wraps a type id and a "dynamic ID".
18/// The "dynamic ID" is usually just left `0`, and, unless such documentation
19/// says otherwise, other libraries will assume that it is always `0`; non-zero
20/// IDs are only used for special resource types that are specifically defined
21/// in a more dynamic way, such that resource types can essentially be created
22/// at run time, without having different static types.
23///
24/// [`Resource`]: trait.Resource.html
25#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
26pub struct ResourceId(TypeId);
27
28impl ResourceId {
29    /// Creates a new resource id from a given type.
30    pub fn new<R>() -> Self
31    where
32        R: Resource,
33    {
34        Self(TypeId::of::<R>())
35    }
36}
37
38impl From<TypeId> for ResourceId {
39    fn from(id: TypeId) -> Self {
40        Self(id)
41    }
42}