Skip to content

Commit e4e1ab1

Browse files
author
Andrew J Westlake
committed
Merged race condition fix, cut down on some repetition
1 parent 498a5e5 commit e4e1ab1

File tree

1 file changed

+9
-29
lines changed

1 file changed

+9
-29
lines changed

src/generic.rs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -315,34 +315,16 @@ fn done(future: &PyAny) -> PyResult<bool> {
315315
}
316316

317317
#[pyclass]
318-
struct CheckedSetResult();
318+
struct CheckedCompletor;
319319

320320
#[pymethods]
321-
impl CheckedSetResult {
322-
fn __call__(&self, future: &PyAny, value: PyObject) -> PyResult<()> {
321+
impl CheckedCompletor {
322+
fn __call__(&self, future: &PyAny, complete: &PyAny, value: &PyAny) -> PyResult<()> {
323323
if done(future)? {
324324
return Ok(());
325325
}
326326

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,))?;
327+
complete.call1((value,))?;
346328

347329
Ok(())
348330
}
@@ -352,13 +334,11 @@ fn set_result(event_loop: &PyAny, future: &PyAny, result: PyResult<PyObject>) ->
352334
let py = event_loop.py();
353335
let none = py.None().into_ref(py);
354336

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-
358-
match result {
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))?,
361-
}
337+
let (complete, val) = match result {
338+
Ok(val) => (future.getattr("set_result")?, val.into_py(py)),
339+
Err(err) => (future.getattr("set_exception")?, err.into_py(py)),
340+
};
341+
call_soon_threadsafe(event_loop, none, (CheckedCompletor, future, complete, val))?;
362342

363343
Ok(())
364344
}

0 commit comments

Comments
 (0)