fn_graph/
data_access.rs

1use crate::TypeIds;
2
3pub use self::{r::R, w::W};
4
5mod r;
6mod w;
7
8/// Data accessed by this type.
9pub trait DataAccess {
10    /// Returns the [`TypeId`]s of borrowed arguments.
11    ///
12    /// [`TypeId`]: core::any::TypeId
13    fn borrows() -> TypeIds
14    where
15        Self: Sized;
16
17    /// Returns the [`TypeId`]s of mutably borrowed arguments.
18    ///
19    /// [`TypeId`]: core::any::TypeId
20    fn borrow_muts() -> TypeIds
21    where
22        Self: Sized;
23}
24
25/// Data accessed by this type.
26pub trait DataAccessDyn {
27    /// Returns the [`TypeId`]s of borrowed arguments.
28    ///
29    /// [`TypeId`]: core::any::TypeId
30    fn borrows(&self) -> TypeIds;
31    /// Returns the [`TypeId`]s of mutably borrowed arguments.
32    ///
33    /// [`TypeId`]: core::any::TypeId
34    fn borrow_muts(&self) -> TypeIds;
35}
36
37#[cfg(not(feature = "fn_meta"))]
38impl DataAccess for () {
39    fn borrows() -> TypeIds
40    where
41        Self: Sized,
42    {
43        TypeIds::new()
44    }
45
46    fn borrow_muts() -> TypeIds
47    where
48        Self: Sized,
49    {
50        TypeIds::new()
51    }
52}
53
54#[cfg(not(feature = "fn_meta"))]
55impl DataAccessDyn for () {
56    fn borrows(&self) -> TypeIds
57    where
58        Self: Sized,
59    {
60        TypeIds::new()
61    }
62
63    fn borrow_muts(&self) -> TypeIds
64    where
65        Self: Sized,
66    {
67        TypeIds::new()
68    }
69}
70
71#[cfg(not(feature = "fn_meta"))]
72impl DataAccess for &() {
73    fn borrows() -> TypeIds
74    where
75        Self: Sized,
76    {
77        TypeIds::new()
78    }
79
80    fn borrow_muts() -> TypeIds
81    where
82        Self: Sized,
83    {
84        TypeIds::new()
85    }
86}
87
88#[cfg(not(feature = "fn_meta"))]
89impl DataAccessDyn for &() {
90    fn borrows(&self) -> TypeIds
91    where
92        Self: Sized,
93    {
94        TypeIds::new()
95    }
96
97    fn borrow_muts(&self) -> TypeIds
98    where
99        Self: Sized,
100    {
101        TypeIds::new()
102    }
103}
104
105#[cfg(feature = "fn_meta")]
106use fn_meta::{FnMeta, FnMetaDyn};
107
108#[cfg(feature = "fn_meta")]
109impl<T> DataAccess for T
110where
111    T: FnMeta,
112{
113    fn borrows() -> TypeIds {
114        <T as FnMeta>::borrows()
115    }
116
117    fn borrow_muts() -> TypeIds {
118        <T as FnMeta>::borrow_muts()
119    }
120}
121
122#[cfg(feature = "fn_meta")]
123impl<T> DataAccessDyn for T
124where
125    T: FnMetaDyn,
126{
127    fn borrows(&self) -> TypeIds {
128        <T as FnMetaDyn>::borrows(self)
129    }
130
131    fn borrow_muts(&self) -> TypeIds {
132        <T as FnMetaDyn>::borrow_muts(self)
133    }
134}
135
136/// Borrows data from `Resources`.
137#[cfg(feature = "resman")]
138pub trait DataBorrow<'borrow> {
139    /// Borrows `Self`'s underlying data type from the provided [`Resources`].
140    ///
141    /// [`Resources`]: resman::Resources
142    fn borrow(resources: &'borrow resman::Resources) -> Self;
143}
144
145#[cfg(test)]
146mod tests {
147    use std::any::TypeId;
148
149    use super::{DataAccess, DataAccessDyn};
150
151    #[test]
152    fn unit() {
153        assert_eq!([] as [TypeId; 0], <() as DataAccess>::borrows().as_slice());
154        assert_eq!(
155            [] as [TypeId; 0],
156            <() as DataAccess>::borrow_muts().as_slice()
157        );
158        assert_eq!([] as [TypeId; 0], ().borrows().as_slice());
159        assert_eq!([] as [TypeId; 0], ().borrow_muts().as_slice());
160
161        assert_eq!([] as [TypeId; 0], <&() as DataAccess>::borrows().as_slice());
162        assert_eq!(
163            [] as [TypeId; 0],
164            <&() as DataAccess>::borrow_muts().as_slice()
165        );
166        assert_eq!([] as [TypeId; 0], DataAccessDyn::borrows(&&()).as_slice());
167        assert_eq!(
168            [] as [TypeId; 0],
169            DataAccessDyn::borrow_muts(&&()).as_slice()
170        );
171    }
172}