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

Use try_for_each in into_py_dict #4647

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,10 @@ where
{
fn into_py_dict(self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new(py);
for item in self {
self.into_iter().try_for_each(|item| {
let (key, value) = item.unpack();
dict.set_item(key, value)?;
}
dict.set_item(key, value)
})?;
Ok(dict)
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ffi::{self, Py_ssize_t};
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::internal_tricks::get_ssize_index;
use crate::types::{PySequence, PyTuple};
use crate::{Borrowed, Bound, BoundObject, IntoPyObject, PyAny, PyObject, Python};
use crate::{Borrowed, Bound, BoundObject, IntoPyObject, PyAny, PyErr, PyObject, Python};

use crate::types::any::PyAnyMethods;
use crate::types::sequence::PySequenceMethods;
Expand Down Expand Up @@ -51,18 +51,18 @@ pub(crate) fn try_new_from_iter<'py>(
// - its Drop cleans up the list if user code or the asserts panic.
let list = ptr.assume_owned(py).downcast_into_unchecked();

let mut counter: Py_ssize_t = 0;

for obj in (&mut elements).take(len as usize) {
#[cfg(not(Py_LIMITED_API))]
ffi::PyList_SET_ITEM(ptr, counter, obj?.into_ptr());
#[cfg(Py_LIMITED_API)]
ffi::PyList_SetItem(ptr, counter, obj?.into_ptr());
counter += 1;
}
let count = (&mut elements)
.take(len as usize)
.try_fold(0, |count, item| {
#[cfg(not(Py_LIMITED_API))]
ffi::PyList_SET_ITEM(ptr, count, item?.into_ptr());
#[cfg(Py_LIMITED_API)]
ffi::PyList_SetItem(ptr, count, item?.into_ptr());
Ok::<_, PyErr>(count + 1)
})?;

assert!(elements.next().is_none(), "Attempted to create PyList but `elements` was larger than reported by its `ExactSizeIterator` implementation.");
assert_eq!(len, counter, "Attempted to create PyList but `elements` was smaller than reported by its `ExactSizeIterator` implementation.");
assert_eq!(len, count, "Attempted to create PyList but `elements` was smaller than reported by its `ExactSizeIterator` implementation.");

Ok(list)
}
Expand Down
8 changes: 4 additions & 4 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ where
};
let ptr = set.as_ptr();

for e in elements {
let obj = e.into_pyobject(py).map_err(Into::into)?;
err::error_on_minusone(py, unsafe { ffi::PySet_Add(ptr, obj.as_ptr()) })?;
}
elements.into_iter().try_for_each(|element| {
let obj = element.into_pyobject(py).map_err(Into::into)?;
err::error_on_minusone(py, unsafe { ffi::PySet_Add(ptr, obj.as_ptr()) })
})?;

Ok(set)
}
Expand Down
Loading