Skip to content

Commit

Permalink
Use try_for_each in into_py_dict (#4647)
Browse files Browse the repository at this point in the history
* Use `try_for_each` in `into_py_dict`

* Use `try_fold` in `PyList::try_new_from_iter`

* Use `try_for_each` in `PySet::try_new_from_iter`
  • Loading branch information
bschoenmaeckers authored Oct 26, 2024
1 parent 869c4bf commit fbeb2b6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
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

0 comments on commit fbeb2b6

Please # to comment.