-
Notifications
You must be signed in to change notification settings - Fork 766
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 TryExtend
trait
#4664
base: main
Are you sure you want to change the base?
Add TryExtend
trait
#4664
Conversation
@@ -0,0 +1,23 @@ | |||
//! A trait for extending a collection with elements from an iterator, returning an error if the operation fails. |
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 could not find a obvious file to add this trait to so I created a new one. Suggestions are welcome.
1a4ce61
to
3cd380f
Compare
I'm unsure whether I'm in favor of this, but I don't want specialization. We used to rely on it but removed it a couple of years ago. The problem with specialization (even the min version) is that it has soundness issues when it comes to lifetimes and it also has no realistic path towards stabilization. Also, you can add a compatibility shim for |
Alternatively I could add 2 methods to PyList & PyDict; |
3cd380f
to
79c8296
Compare
79c8296
to
06d8289
Compare
CodSpeed Performance ReportMerging #4664 will not alter performanceComparing Summary
|
06d8289
to
bc7cff0
Compare
Sorry to have been a little slow to get around to joining the discussion on this PR, so many parallel threads at the moment 😩 Thanks very much for working on our collections! I am 100% in agreement that there are things we could do to make PyO3's wrappers of Python collections interact better with Rust iterators. I have been wondering about whether we could implement both I am a little cautious about |
I was hesitant to implement non-failable traits because this may cause unexpected panics when python throws an error back when creating a python collection. That said I do not know all cases when python fails adding an item, so maybe this is not that big of a problem. A impl<'py, T> Extend<Bound<'py, T>> for Bound<'_, PyList>
{
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = Bound<'py, T>>,
{
iter.into_iter().for_each(|item| {
self.append(item).expect("Failed to append item to list");
});
}
}
impl<'py, T> FromIterator<Bound<'py, T>> for Bound<'py, PyList> {
fn from_iter<I: IntoIterator<Item=Bound<'py, T>>>(iter: I) -> Self {
let mut iter = iter.into_iter().peekable();
let py = iter.peek().map(Bound::py)
.expect("Cannot create a list from an empty iterator");
let mut list = PyList::empty(py);
list.extend(iter);
list
}
} Furthermore the
I like this idea, itertools has an ongoing discussion on fallible iterators so |
This PR adds a
TryExtend
trait that is similar to thestd::iter::Extend
trait except it is failable. Currently implemented forPyList
&PyDict
, including some optimised specializations on nightly.