Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IntoIterator for &Bound types #3923

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,15 @@ impl<'py> IntoIterator for Bound<'py, PyList> {
}
}

impl<'py> IntoIterator for &Bound<'py, PyList> {
type Item = Bound<'py, PyAny>;
type IntoIter = BoundListIterator<'py>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

#[cfg(test)]
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
mod tests {
Expand Down Expand Up @@ -911,6 +920,20 @@ mod tests {
});
}

#[test]
fn test_into_iter_bound() {
use crate::types::any::PyAnyMethods;

Python::with_gil(|py| {
let list = PyList::new_bound(py, [1, 2, 3, 4]);
let mut items = vec![];
for item in &list {
items.push(item.extract::<i32>().unwrap());
}
assert_eq!(items, vec![1, 2, 3, 4]);
});
}

#[test]
fn test_extract() {
Python::with_gil(|py| {
Expand Down
Loading