|
5 | 5 | //! when awaited, see guide examples related to pyo3-async-runtimes for ways |
6 | 6 | //! to suspend tasks and await results. |
7 | 7 |
|
8 | | -use pyo3::exceptions::PyStopIteration; |
9 | 8 | use pyo3::prelude::*; |
10 | 9 |
|
11 | | -#[pyclass] |
12 | | -#[derive(Debug)] |
13 | | -pub(crate) struct IterAwaitable { |
14 | | - result: Option<PyResult<Py<PyAny>>>, |
15 | | -} |
16 | | - |
17 | | -#[pymethods] |
18 | | -impl IterAwaitable { |
19 | | - #[new] |
20 | | - fn new(result: Py<PyAny>) -> Self { |
21 | | - IterAwaitable { |
22 | | - result: Some(Ok(result)), |
23 | | - } |
24 | | - } |
| 10 | +#[pymodule] |
| 11 | +pub mod awaitable { |
| 12 | + use pyo3::exceptions::PyStopIteration; |
| 13 | + use pyo3::prelude::*; |
25 | 14 |
|
26 | | - fn __await__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { |
27 | | - pyself |
| 15 | + #[pyclass] |
| 16 | + #[derive(Debug)] |
| 17 | + pub(crate) struct IterAwaitable { |
| 18 | + result: Option<PyResult<Py<PyAny>>>, |
28 | 19 | } |
29 | 20 |
|
30 | | - fn __iter__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { |
31 | | - pyself |
32 | | - } |
| 21 | + #[pymethods] |
| 22 | + impl IterAwaitable { |
| 23 | + #[new] |
| 24 | + fn new(result: Py<PyAny>) -> Self { |
| 25 | + IterAwaitable { |
| 26 | + result: Some(Ok(result)), |
| 27 | + } |
| 28 | + } |
33 | 29 |
|
34 | | - fn __next__(&mut self, py: Python<'_>) -> PyResult<Py<PyAny>> { |
35 | | - match self.result.take() { |
36 | | - Some(res) => match res { |
37 | | - Ok(v) => Err(PyStopIteration::new_err(v)), |
38 | | - Err(err) => Err(err), |
39 | | - }, |
40 | | - _ => Ok(py.None()), |
| 30 | + fn __await__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { |
| 31 | + pyself |
41 | 32 | } |
42 | | - } |
43 | | -} |
44 | 33 |
|
45 | | -#[pyclass] |
46 | | -pub(crate) struct FutureAwaitable { |
47 | | - #[pyo3(get, set, name = "_asyncio_future_blocking")] |
48 | | - py_block: bool, |
49 | | - result: Option<PyResult<Py<PyAny>>>, |
50 | | -} |
| 34 | + fn __iter__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { |
| 35 | + pyself |
| 36 | + } |
51 | 37 |
|
52 | | -#[pymethods] |
53 | | -impl FutureAwaitable { |
54 | | - #[new] |
55 | | - fn new(result: Py<PyAny>) -> Self { |
56 | | - FutureAwaitable { |
57 | | - py_block: false, |
58 | | - result: Some(Ok(result)), |
| 38 | + fn __next__(&mut self, py: Python<'_>) -> PyResult<Py<PyAny>> { |
| 39 | + match self.result.take() { |
| 40 | + Some(res) => match res { |
| 41 | + Ok(v) => Err(PyStopIteration::new_err(v)), |
| 42 | + Err(err) => Err(err), |
| 43 | + }, |
| 44 | + _ => Ok(py.None()), |
| 45 | + } |
59 | 46 | } |
60 | 47 | } |
61 | 48 |
|
62 | | - fn __await__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { |
63 | | - pyself |
| 49 | + #[pyclass] |
| 50 | + pub(crate) struct FutureAwaitable { |
| 51 | + #[pyo3(get, set, name = "_asyncio_future_blocking")] |
| 52 | + py_block: bool, |
| 53 | + result: Option<PyResult<Py<PyAny>>>, |
64 | 54 | } |
65 | 55 |
|
66 | | - fn __iter__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { |
67 | | - pyself |
68 | | - } |
| 56 | + #[pymethods] |
| 57 | + impl FutureAwaitable { |
| 58 | + #[new] |
| 59 | + fn new(result: Py<PyAny>) -> Self { |
| 60 | + FutureAwaitable { |
| 61 | + py_block: false, |
| 62 | + result: Some(Ok(result)), |
| 63 | + } |
| 64 | + } |
69 | 65 |
|
70 | | - fn __next__(mut pyself: PyRefMut<'_, Self>) -> PyResult<PyRefMut<'_, Self>> { |
71 | | - match pyself.result { |
72 | | - Some(_) => match pyself.result.take().unwrap() { |
73 | | - Ok(v) => Err(PyStopIteration::new_err(v)), |
74 | | - Err(err) => Err(err), |
75 | | - }, |
76 | | - _ => Ok(pyself), |
| 66 | + fn __await__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { |
| 67 | + pyself |
77 | 68 | } |
78 | | - } |
79 | | -} |
80 | 69 |
|
81 | | -#[pymodule] |
82 | | -pub fn awaitable(m: &Bound<'_, PyModule>) -> PyResult<()> { |
83 | | - m.add_class::<IterAwaitable>()?; |
84 | | - m.add_class::<FutureAwaitable>()?; |
85 | | - Ok(()) |
| 70 | + fn __iter__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { |
| 71 | + pyself |
| 72 | + } |
| 73 | + |
| 74 | + fn __next__(mut pyself: PyRefMut<'_, Self>) -> PyResult<PyRefMut<'_, Self>> { |
| 75 | + match pyself.result { |
| 76 | + Some(_) => match pyself.result.take().unwrap() { |
| 77 | + Ok(v) => Err(PyStopIteration::new_err(v)), |
| 78 | + Err(err) => Err(err), |
| 79 | + }, |
| 80 | + _ => Ok(pyself), |
| 81 | + } |
| 82 | + } |
| 83 | + } |
86 | 84 | } |
0 commit comments