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
use {Resource, Resources};

#[cfg(feature = "nightly")]
macro_rules! fetch_panic {
    () => {{
        panic!(
            "Tried to fetch a resource of type {:?}, but the resource does not exist.\n\
             Try adding the resource by inserting it manually or using the `setup` method.",
            unsafe { ::std::intrinsics::type_name::<T>() },
        )
    }};
}

#[cfg(not(feature = "nightly"))]
macro_rules! fetch_panic {
    () => {{
        panic!(
            "Tried to fetch a resource, but the resource does not exist.\n\
             Try adding the resource by inserting it manually or using the `setup` method.\n\
             You can get the type name of the missing resource by enabling `shred`'s `nightly` \
             feature"
        )
    }};
}

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

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

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

/// A setup handler that simply does nothing and thus will cause a panic on fetching.
/// The panic will provide the type name if the `nightly` feature of shred is enabled.
///
/// 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 Resources) {}
}