From 0ec36a7ef6d4bdb8e4bf637c2c8512e5f097d4c0 Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Mon, 7 Oct 2024 10:32:47 +0200 Subject: [PATCH 1/3] Use `try_for_each` in `into_py_dict` --- src/types/dict.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/types/dict.rs b/src/types/dict.rs index 6987e4525ec..129f32dc9e1 100644 --- a/src/types/dict.rs +++ b/src/types/dict.rs @@ -771,10 +771,10 @@ where { fn into_py_dict(self, py: Python<'py>) -> PyResult> { 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) } } From dfe469d37da2ade01c810a0c59e3940839a2b934 Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Fri, 25 Oct 2024 11:37:56 +0200 Subject: [PATCH 2/3] Use `try_fold` in `PyList::try_new_from_iter` --- src/types/list.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/types/list.rs b/src/types/list.rs index ae5eda63a11..f00c194739f 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -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; @@ -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) } From 57f7815d26ebe740467d4a6db47000948aec0ce4 Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Fri, 25 Oct 2024 11:38:30 +0200 Subject: [PATCH 3/3] Use `try_for_each` in `PySet::try_new_from_iter` --- src/types/set.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/types/set.rs b/src/types/set.rs index 622020dfb11..e7c24f5b1ea 100644 --- a/src/types/set.rs +++ b/src/types/set.rs @@ -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) }