1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::{Resource, World};

macro_rules! fetch_panic {
    () => {{
        panic!(
            "\
            Tried to fetch resource of type `{resource_name_simple}`[^1] from the `World`, but \
            the resource does not exist.\n\
\n\
            You may ensure the resource exists through one of the following methods:\n\
\n\
            * Inserting it when the world is created: `world.insert(..)`.\n\
            * If the resource implements `Default`, include it in a system's `SystemData`, \
              and ensure the system is registered in the dispatcher.\n\
            * If the resource does not implement `Default`, insert it in the world during \
              `System::setup`.\n\
\n\
            [^1]: Full type name: `{resource_name_full}`\
            ",
            resource_name_simple = tynm::type_name::<T>(),
            resource_name_full = std::any::type_name::<T>(),
        )
    }};
}

/// A `SetupHandler` that simply uses the default implementation.
pub struct DefaultProvider;

impl<T> SetupHandler<T> for DefaultProvider
where
    T: Default + Resource,
{
    fn setup(world: &mut World) {
        world.entry().or_insert_with(T::default);
    }
}

/// A setup handler performing the fetching of `T`.
pub trait SetupHandler<T>: Sized {
    /// Sets up `World` for fetching `T`.
    fn setup(world: &mut World);
}

/// A setup handler that simply does nothing and thus will cause a panic on
/// fetching.
///
/// A typedef called `ReadExpect` exists, so you usually don't use this type
/// directly.
pub struct PanicHandler;

impl<T> SetupHandler<T> for PanicHandler
where
    T: Resource,
{
    fn setup(_: &mut World) {}
}