@@ -310,19 +310,54 @@ fn cancelled(future: &PyAny) -> PyResult<bool> {
310
310
future. getattr ( "cancelled" ) ?. call0 ( ) ?. is_true ( )
311
311
}
312
312
313
+ fn done ( future : & PyAny ) -> PyResult < bool > {
314
+ future. getattr ( "done" ) ?. call0 ( ) ?. is_true ( )
315
+ }
316
+
317
+ #[ pyclass]
318
+ struct CheckedSetResult ( ) ;
319
+
320
+ #[ pymethods]
321
+ impl CheckedSetResult {
322
+ fn __call__ ( & self , future : & PyAny , value : PyObject ) -> PyResult < ( ) > {
323
+ if done ( future) ? {
324
+ return Ok ( ( ) ) ;
325
+ }
326
+
327
+ let set_result = future. getattr ( "set_result" ) ?;
328
+ set_result. call1 ( ( value, ) ) ?;
329
+
330
+ Ok ( ( ) )
331
+ }
332
+ }
333
+
334
+ #[ pyclass]
335
+ struct CheckedSetException ( ) ;
336
+
337
+ #[ pymethods]
338
+ impl CheckedSetException {
339
+ fn __call__ ( & self , future : & PyAny , exception : PyObject ) -> PyResult < ( ) > {
340
+ if done ( future) ? {
341
+ return Ok ( ( ) ) ;
342
+ }
343
+
344
+ let set_exception = future. getattr ( "set_exception" ) ?;
345
+ set_exception. call1 ( ( exception, ) ) ?;
346
+
347
+ Ok ( ( ) )
348
+ }
349
+ }
350
+
313
351
fn set_result ( event_loop : & PyAny , future : & PyAny , result : PyResult < PyObject > ) -> PyResult < ( ) > {
314
352
let py = event_loop. py ( ) ;
315
353
let none = py. None ( ) . into_ref ( py) ;
316
354
355
+ let checked_set_result = CheckedSetResult ( ) . into_py ( py) . into_ref ( py) ;
356
+ let checked_set_exception = CheckedSetException ( ) . into_py ( py) . into_ref ( py) ;
357
+
317
358
match result {
318
- Ok ( val) => {
319
- let set_result = future. getattr ( "set_result" ) ?;
320
- call_soon_threadsafe ( event_loop, none, ( set_result, val) ) ?;
321
- }
322
- Err ( err) => {
323
- let set_exception = future. getattr ( "set_exception" ) ?;
324
- call_soon_threadsafe ( event_loop, none, ( set_exception, err) ) ?;
325
- }
359
+ Ok ( val) => call_soon_threadsafe ( event_loop, none, ( checked_set_result, future, val) ) ?,
360
+ Err ( err) => call_soon_threadsafe ( event_loop, none, ( checked_set_exception, future, err) ) ?,
326
361
}
327
362
328
363
Ok ( ( ) )
0 commit comments