@@ -185,13 +185,13 @@ impl<const MQ: bool> TokenParts<true, MQ, true> {
185
185
}
186
186
187
187
/// Zero-size statement that the current code is not running in an interrupt
188
- #[ derive( Copy , Clone ) ]
188
+ #[ derive( Copy , Clone , Debug ) ]
189
189
pub struct InThread {
190
190
_not_send : PhantomData < * const ( ) > ,
191
191
}
192
192
193
193
/// Zero-size statement that the current code is running in an interrupt
194
- #[ derive( Copy , Clone ) ]
194
+ #[ derive( Copy , Clone , Debug ) ]
195
195
pub struct InIrq {
196
196
_not_send : PhantomData < * const ( ) > ,
197
197
}
@@ -213,6 +213,15 @@ impl InThread {
213
213
false => Ok ( unsafe { InThread :: new_unchecked ( ) } ) ,
214
214
}
215
215
}
216
+
217
+ /// Wrap a `value` in a [`ValueInThread`]. This makes it non-Send, but may make additional
218
+ /// (safe) methods on it, using the knowledge that it is still being used inside a thread.
219
+ pub fn promote < T > ( self , value : T ) -> ValueInThread < T > {
220
+ ValueInThread {
221
+ value,
222
+ in_thread : self ,
223
+ }
224
+ }
216
225
}
217
226
218
227
impl InIrq {
@@ -230,3 +239,36 @@ impl InIrq {
230
239
}
231
240
}
232
241
}
242
+
243
+ /// A value combined with an [`InThread`'] marker
244
+ ///
245
+ /// This does barely implement anything on its own, but the module implementing `T` might provide
246
+ /// extra methods.
247
+ pub struct ValueInThread < T > {
248
+ value : T ,
249
+ in_thread : InThread ,
250
+ }
251
+
252
+ impl < T > ValueInThread < T > {
253
+ /// Extract the wrapped value
254
+ ///
255
+ /// This does not produce the original `in_thread` value; these are easy enough to re-obtain or
256
+ /// to keep a copy of around.
257
+ pub fn into_inner ( self ) -> T {
258
+ self . value
259
+ }
260
+ }
261
+
262
+ impl < T > core:: ops:: Deref for ValueInThread < T > {
263
+ type Target = T ;
264
+
265
+ fn deref ( & self ) -> & T {
266
+ & self . value
267
+ }
268
+ }
269
+
270
+ impl < T > core:: ops:: DerefMut for ValueInThread < T > {
271
+ fn deref_mut ( & mut self ) -> & mut T {
272
+ & mut self . value
273
+ }
274
+ }
0 commit comments