Skip to main content

pebble/assets/
deps.rs

1use crate::{ecs::resources::Resources, ecs::system::Res};
2
3/// A set of resources required by an [`Asset`](crate::assets::upload::Asset)
4/// implementation before it can be uploaded to the backend.
5///
6/// Implement this trait for `()` (no deps), a single `Res<T>`, or a tuple of
7/// `Res<T>` values. The sync system calls [`try_gather`] each tick and skips
8/// the upload if any dependency is not yet present.
9pub trait Dependencies<'a>: Sized {
10    /// Attempt to gather all required resources from the world.
11    ///
12    /// Returns `None` if any dependency is missing, deferring the upload to
13    /// the next tick.
14    fn try_gather(world: &'a hecs::World, resources: &'a Resources) -> Option<Self>;
15}
16
17impl<'a> Dependencies<'a> for () {
18    fn try_gather(_world: &'a hecs::World, _resources: &'a Resources) -> Option<Self> {
19        Some(())
20    }
21}
22
23impl<'a, A: 'static + Send + Sync> Dependencies<'a> for Res<'a, A> {
24    fn try_gather(world: &'a hecs::World, resources: &'a Resources) -> Option<Self> {
25        if !resources.has_resource::<A>(world) {
26            return None;
27        }
28
29        Some(Res {
30            data: resources.get_resource(world),
31        })
32    }
33}
34
35impl<'a, A, B> Dependencies<'a> for (Res<'a, A>, Res<'a, B>)
36where
37    A: 'static + Send + Sync,
38    B: 'static + Send + Sync,
39{
40    fn try_gather(world: &'a hecs::World, resources: &'a Resources) -> Option<Self> {
41        if !resources.has_resource::<A>(world) || !resources.has_resource::<B>(world) {
42            return None;
43        }
44
45        Some((
46            Res {
47                data: resources.get_resource(world),
48            },
49            Res {
50                data: resources.get_resource(world),
51            },
52        ))
53    }
54}