Skip to content

Commit 498a5e5

Browse files
author
Andrew J Westlake
committed
Merge branch 'master' of https://github.com/decathorpe/pyo3-asyncio into release-0.17
2 parents 51e89a4 + 22f32e8 commit 498a5e5

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

src/generic.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,19 +310,54 @@ fn cancelled(future: &PyAny) -> PyResult<bool> {
310310
future.getattr("cancelled")?.call0()?.is_true()
311311
}
312312

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+
313351
fn set_result(event_loop: &PyAny, future: &PyAny, result: PyResult<PyObject>) -> PyResult<()> {
314352
let py = event_loop.py();
315353
let none = py.None().into_ref(py);
316354

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+
317358
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))?,
326361
}
327362

328363
Ok(())

0 commit comments

Comments
 (0)