-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement IntoIterator for PyDictRef. #810
Conversation
@@ -247,6 +234,54 @@ impl ItemProtocol for PyDictRef { | |||
} | |||
} | |||
|
|||
// Implement IntoIterator so that we can easily iterate dictionaries from rust code. | |||
impl IntoIterator for PyDictRef { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than implementing this for the specific type, could this be implemented for &PyDict
, and then a blanket impl for PyRef<T>
(i.e. something like impl<T> IntoIterator for PyRef<T> where &T: IntoIterator
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good tip. I think we want the other way around: ie. impl<T> IntoIterator for &PyRef<T>
, otherwise you always clone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe, not... you mean PyDict not PyDictRef.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But, I don't think you implement it for &PyDict
because you need the PyRef in order to clone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to clone? Why not:
struct DictIter<'a> {
dict: &'a PyDict,
...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give that a go. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get as far as this
impl<T> IntoIterator for PyRef<T>
where
T: IntoIterator + PyValue,
{
type Item = <T as IntoIterator>::Item;
type IntoIter = <T as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.deref().into_iter()
}
}
which gives:
266 | self.deref().into_iter()
| ^^^^^^^^^^^^ cannot move out of borrowed content
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'll merge what I've got here and open a separate PR for this improvement.
Simplify iteration of dictionaries from rust code.