@@ -27,6 +27,14 @@ pub enum DataStateError<E: ErrorBounds> {
27
27
FromE ( E ) ,
28
28
}
29
29
30
+ #[ derive( Debug ) ]
31
+ /// Provides a way to ensure the calling code knows if it is calling a function
32
+ /// that cannot do anything useful anymore
33
+ pub enum CanMakeProgress {
34
+ AbleToMakeProgress ,
35
+ UnableToMakeProgress ,
36
+ }
37
+
30
38
/// Used to represent data that is pending being available
31
39
#[ derive( Debug ) ]
32
40
pub struct Awaiting < T , E : ErrorBounds > ( pub oneshot:: Receiver < Result < T , E > > ) ;
@@ -84,30 +92,35 @@ impl<T, E: ErrorBounds> DataState<T, E> {
84
92
}
85
93
}
86
94
87
- /// Attempts to load the data.
95
+ /// Attempts to load the data and returns if it is able to make progress .
88
96
///
89
97
/// Note: F needs to return `AwaitingType<T>` and not T because it needs to
90
98
/// be able to be pending if T is not ready.
91
- pub fn get < F > ( & mut self , fetch_fn : F )
99
+ #[ must_use]
100
+ pub fn get < F > ( & mut self , fetch_fn : F ) -> CanMakeProgress
92
101
where
93
102
F : FnOnce ( ) -> Awaiting < T , E > ,
94
103
{
95
104
match self {
96
105
DataState :: None => {
97
106
let rx = fetch_fn ( ) ;
98
107
* self = DataState :: AwaitingResponse ( rx) ;
108
+ CanMakeProgress :: AbleToMakeProgress
99
109
}
100
110
DataState :: AwaitingResponse ( rx) => {
101
111
if let Some ( new_state) = Self :: await_data ( rx) {
102
112
* self = new_state;
103
113
}
114
+ CanMakeProgress :: AbleToMakeProgress
104
115
}
105
116
DataState :: Present ( _data) => {
106
117
// Does nothing data is already present
118
+ CanMakeProgress :: UnableToMakeProgress
107
119
}
108
120
DataState :: Failed ( _e) => {
109
121
// Have no way to let the user know there is an error nothing we
110
122
// can do here
123
+ CanMakeProgress :: UnableToMakeProgress
111
124
}
112
125
}
113
126
}
@@ -184,3 +197,21 @@ impl From<String> for DataStateError<anyhow::Error> {
184
197
anyhow ! ( value) . into ( )
185
198
}
186
199
}
200
+
201
+ impl CanMakeProgress {
202
+ /// Returns `true` if the can make progress is [`AbleToMakeProgress`].
203
+ ///
204
+ /// [`AbleToMakeProgress`]: CanMakeProgress::AbleToMakeProgress
205
+ #[ must_use]
206
+ pub fn is_able_to_make_progress ( & self ) -> bool {
207
+ matches ! ( self , Self :: AbleToMakeProgress )
208
+ }
209
+
210
+ /// Returns `true` if the can make progress is [`UnableToMakeProgress`].
211
+ ///
212
+ /// [`UnableToMakeProgress`]: CanMakeProgress::UnableToMakeProgress
213
+ #[ must_use]
214
+ pub fn is_unable_to_make_progress ( & self ) -> bool {
215
+ matches ! ( self , Self :: UnableToMakeProgress )
216
+ }
217
+ }
0 commit comments