-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to return from
__next__
/ __anext__
- Loading branch information
1 parent
0c59b05
commit a9827a4
Showing
8 changed files
with
159 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use pyo3::class::iter::{IterNextOutput, PyIterProtocol}; | ||
use pyo3::prelude::*; | ||
|
||
/// This is for demonstrating how to return a value from __next__ | ||
#[pyclass] | ||
struct PyClassIter { | ||
count: usize, | ||
} | ||
|
||
#[pymethods] | ||
impl PyClassIter { | ||
#[new] | ||
pub fn new() -> Self { | ||
PyClassIter { count: 0 } | ||
} | ||
} | ||
|
||
#[pyproto] | ||
impl PyIterProtocol for PyClassIter { | ||
fn __next__(mut slf: PyRefMut<Self>) -> IterNextOutput<usize, &'static str> { | ||
if slf.count < 5 { | ||
slf.count += 1; | ||
IterNextOutput::Yield(slf.count) | ||
} else { | ||
IterNextOutput::Return("Ended") | ||
} | ||
} | ||
} | ||
|
||
#[pymodule] | ||
pub fn pyclass_iter(_py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_class::<PyClassIter>()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pytest | ||
from rustapi_module import pyclass_iter | ||
|
||
|
||
def test_iter(): | ||
i = pyclass_iter.PyClassIter() | ||
assert next(i) == 1 | ||
assert next(i) == 2 | ||
assert next(i) == 3 | ||
assert next(i) == 4 | ||
assert next(i) == 5 | ||
|
||
with pytest.raises(StopIteration) as excinfo: | ||
next(i) | ||
assert excinfo.value.value == "Ended" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters