Skip to main content

goggles/
fetch_resources.rs

1use std::{any::type_name, marker::PhantomData};
2
3use crate::resources::{ResourceConflict, Resources};
4
5/// A trait for statically defining mutable and immutable resources fetched from a data source which
6/// may or may not conflict.
7///
8/// Tuples of types that implement `FetchResources` automatically themselves implement
9/// `FetchResources` and correctly find the union of the resources they use.
10pub trait FetchResources<'a> {
11    type Source;
12    type Resources: Resources;
13
14    fn check_resources() -> Result<Self::Resources, ResourceConflict>;
15    fn fetch(source: &'a Self::Source) -> Self;
16}
17
18/// An empty type useful in generic contexts that implements `FetchResources` but does not actually
19/// fetch any resources.
20pub struct FetchNone<S, R>(PhantomData<(S, R)>);
21
22impl<S, R> Default for FetchNone<S, R> {
23    fn default() -> Self {
24        Self(PhantomData)
25    }
26}
27
28impl<'a, S, R: Resources> FetchResources<'a> for FetchNone<S, R> {
29    type Source = S;
30    type Resources = R;
31
32    fn check_resources() -> Result<Self::Resources, ResourceConflict> {
33        Ok(R::default())
34    }
35
36    fn fetch(_: &'a Self::Source) -> Self {
37        FetchNone::default()
38    }
39}
40
41macro_rules! impl_data {
42    ($($ty:ident),*) => {
43        impl<'a, ST, RT, $($ty),*> FetchResources<'a> for ($($ty,)*)
44        where
45            RT: Resources,
46            $($ty: FetchResources<'a, Source = ST, Resources = RT>),*
47        {
48            type Source = ST;
49            type Resources = RT;
50
51            fn check_resources() -> Result<Self::Resources, ResourceConflict> {
52                let mut resources = Self::Resources::default();
53                $({
54                    let r = <$ty as FetchResources>::check_resources()?;
55                    if resources.conflicts_with(&r) {
56                        return Err(ResourceConflict { type_name: type_name::<Self>() });
57                    }
58                    resources.union(&r);
59                })*
60                Ok(resources)
61            }
62
63            fn fetch(source: &'a Self::Source) -> Self {
64                ($(<$ty as FetchResources<'a>>::fetch(source),)*)
65            }
66        }
67    };
68}
69
70impl_data!(A);
71impl_data!(A, B);
72impl_data!(A, B, C);
73impl_data!(A, B, C, D);
74impl_data!(A, B, C, D, E);
75impl_data!(A, B, C, D, E, F);
76impl_data!(A, B, C, D, E, F, G);
77impl_data!(A, B, C, D, E, F, G, H);
78impl_data!(A, B, C, D, E, F, G, H, I);
79impl_data!(A, B, C, D, E, F, G, H, I, J);
80impl_data!(A, B, C, D, E, F, G, H, I, J, K);
81impl_data!(A, B, C, D, E, F, G, H, I, J, K, L);
82impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M);
83impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
84impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
85impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
86impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q);
87impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R);
88impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S);
89impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
90impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U);
91impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V);
92impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W);
93impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X);
94impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y);
95impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);