Skip to content

Commit 7ead7ff

Browse files
committed
migrate PyDictMethods trait bounds
1 parent b2a2a1d commit 7ead7ff

File tree

4 files changed

+58
-56
lines changed

4 files changed

+58
-56
lines changed

src/conversions/hashbrown.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ where
6767
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
6868
let dict = PyDict::new(py);
6969
for (k, v) in self {
70-
dict.set_item(
71-
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
72-
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
73-
)?;
70+
dict.set_item(k, v)?;
7471
}
7572
Ok(dict)
7673
}
@@ -89,10 +86,7 @@ where
8986
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
9087
let dict = PyDict::new(py);
9188
for (k, v) in self {
92-
dict.set_item(
93-
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
94-
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
95-
)?;
89+
dict.set_item(k, v)?;
9690
}
9791
Ok(dict)
9892
}

src/conversions/indexmap.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
9090
use crate::conversion::IntoPyObject;
9191
use crate::types::*;
92-
use crate::{Bound, BoundObject, FromPyObject, IntoPy, PyErr, PyObject, Python, ToPyObject};
92+
use crate::{Bound, FromPyObject, IntoPy, PyErr, PyObject, Python, ToPyObject};
9393
use std::{cmp, hash};
9494

9595
impl<K, V, H> ToPyObject for indexmap::IndexMap<K, V, H>
@@ -130,10 +130,7 @@ where
130130
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
131131
let dict = PyDict::new(py);
132132
for (k, v) in self {
133-
dict.set_item(
134-
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
135-
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
136-
)?;
133+
dict.set_item(k, v)?;
137134
}
138135
Ok(dict)
139136
}
@@ -152,10 +149,7 @@ where
152149
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
153150
let dict = PyDict::new(py);
154151
for (k, v) in self {
155-
dict.set_item(
156-
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
157-
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
158-
)?;
152+
dict.set_item(k, v)?;
159153
}
160154
Ok(dict)
161155
}

src/conversions/std/map.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
conversion::IntoPyObject,
77
instance::Bound,
88
types::{any::PyAnyMethods, dict::PyDictMethods, IntoPyDict, PyDict},
9-
BoundObject, FromPyObject, IntoPy, PyAny, PyErr, PyObject, Python, ToPyObject,
9+
FromPyObject, IntoPy, PyAny, PyErr, PyObject, Python, ToPyObject,
1010
};
1111

1212
impl<K, V, H> ToPyObject for collections::HashMap<K, V, H>
@@ -62,10 +62,7 @@ where
6262
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
6363
let dict = PyDict::new(py);
6464
for (k, v) in self {
65-
dict.set_item(
66-
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
67-
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
68-
)?;
65+
dict.set_item(k, v)?;
6966
}
7067
Ok(dict)
7168
}
@@ -84,10 +81,7 @@ where
8481
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
8582
let dict = PyDict::new(py);
8683
for (k, v) in self {
87-
dict.set_item(
88-
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
89-
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
90-
)?;
84+
dict.set_item(k, v)?;
9185
}
9286
Ok(dict)
9387
}
@@ -123,10 +117,7 @@ where
123117
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
124118
let dict = PyDict::new(py);
125119
for (k, v) in self {
126-
dict.set_item(
127-
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
128-
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
129-
)?;
120+
dict.set_item(k, v)?;
130121
}
131122
Ok(dict)
132123
}
@@ -144,10 +135,7 @@ where
144135
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
145136
let dict = PyDict::new(py);
146137
for (k, v) in self {
147-
dict.set_item(
148-
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
149-
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
150-
)?;
138+
dict.set_item(k, v)?;
151139
}
152140
Ok(dict)
153141
}

src/types/dict.rs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::instance::{Borrowed, Bound};
66
use crate::py_result_ext::PyResultExt;
77
use crate::types::any::PyAnyMethods;
88
use crate::types::{PyAny, PyList};
9-
use crate::{ffi, Python, ToPyObject};
9+
use crate::{ffi, BoundObject, Python, ToPyObject};
1010

1111
/// Represents a Python `dict`.
1212
///
@@ -128,7 +128,7 @@ pub trait PyDictMethods<'py>: crate::sealed::Sealed {
128128
/// This is equivalent to the Python expression `key in self`.
129129
fn contains<K>(&self, key: K) -> PyResult<bool>
130130
where
131-
K: ToPyObject;
131+
K: IntoPyObject<'py>;
132132

133133
/// Gets an item from the dictionary.
134134
///
@@ -137,22 +137,22 @@ pub trait PyDictMethods<'py>: crate::sealed::Sealed {
137137
/// To get a `KeyError` for non-existing keys, use `PyAny::get_item`.
138138
fn get_item<K>(&self, key: K) -> PyResult<Option<Bound<'py, PyAny>>>
139139
where
140-
K: ToPyObject;
140+
K: IntoPyObject<'py>;
141141

142142
/// Sets an item value.
143143
///
144144
/// This is equivalent to the Python statement `self[key] = value`.
145145
fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>
146146
where
147-
K: ToPyObject,
148-
V: ToPyObject;
147+
K: IntoPyObject<'py>,
148+
V: IntoPyObject<'py>;
149149

150150
/// Deletes an item.
151151
///
152152
/// This is equivalent to the Python statement `del self[key]`.
153153
fn del_item<K>(&self, key: K) -> PyResult<()>
154154
where
155-
K: ToPyObject;
155+
K: IntoPyObject<'py>;
156156

157157
/// Returns a list of dict keys.
158158
///
@@ -224,9 +224,9 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
224224

225225
fn contains<K>(&self, key: K) -> PyResult<bool>
226226
where
227-
K: ToPyObject,
227+
K: IntoPyObject<'py>,
228228
{
229-
fn inner(dict: &Bound<'_, PyDict>, key: Bound<'_, PyAny>) -> PyResult<bool> {
229+
fn inner(dict: &Bound<'_, PyDict>, key: &Bound<'_, PyAny>) -> PyResult<bool> {
230230
match unsafe { ffi::PyDict_Contains(dict.as_ptr(), key.as_ptr()) } {
231231
1 => Ok(true),
232232
0 => Ok(false),
@@ -235,16 +235,22 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
235235
}
236236

237237
let py = self.py();
238-
inner(self, key.to_object(py).into_bound(py))
238+
inner(
239+
self,
240+
&key.into_pyobject(py)
241+
.map_err(Into::into)?
242+
.into_any()
243+
.as_borrowed(),
244+
)
239245
}
240246

241247
fn get_item<K>(&self, key: K) -> PyResult<Option<Bound<'py, PyAny>>>
242248
where
243-
K: ToPyObject,
249+
K: IntoPyObject<'py>,
244250
{
245251
fn inner<'py>(
246252
dict: &Bound<'py, PyDict>,
247-
key: Bound<'_, PyAny>,
253+
key: &Bound<'_, PyAny>,
248254
) -> PyResult<Option<Bound<'py, PyAny>>> {
249255
let py = dict.py();
250256
let mut result: *mut ffi::PyObject = std::ptr::null_mut();
@@ -258,18 +264,24 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
258264
}
259265

260266
let py = self.py();
261-
inner(self, key.to_object(py).into_bound(py))
267+
inner(
268+
self,
269+
&key.into_pyobject(py)
270+
.map_err(Into::into)?
271+
.into_any()
272+
.as_borrowed(),
273+
)
262274
}
263275

264276
fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>
265277
where
266-
K: ToPyObject,
267-
V: ToPyObject,
278+
K: IntoPyObject<'py>,
279+
V: IntoPyObject<'py>,
268280
{
269281
fn inner(
270282
dict: &Bound<'_, PyDict>,
271-
key: Bound<'_, PyAny>,
272-
value: Bound<'_, PyAny>,
283+
key: &Bound<'_, PyAny>,
284+
value: &Bound<'_, PyAny>,
273285
) -> PyResult<()> {
274286
err::error_on_minusone(dict.py(), unsafe {
275287
ffi::PyDict_SetItem(dict.as_ptr(), key.as_ptr(), value.as_ptr())
@@ -279,23 +291,36 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
279291
let py = self.py();
280292
inner(
281293
self,
282-
key.to_object(py).into_bound(py),
283-
value.to_object(py).into_bound(py),
294+
&key.into_pyobject(py)
295+
.map_err(Into::into)?
296+
.into_any()
297+
.as_borrowed(),
298+
&value
299+
.into_pyobject(py)
300+
.map_err(Into::into)?
301+
.into_any()
302+
.as_borrowed(),
284303
)
285304
}
286305

287306
fn del_item<K>(&self, key: K) -> PyResult<()>
288307
where
289-
K: ToPyObject,
308+
K: IntoPyObject<'py>,
290309
{
291-
fn inner(dict: &Bound<'_, PyDict>, key: Bound<'_, PyAny>) -> PyResult<()> {
310+
fn inner(dict: &Bound<'_, PyDict>, key: &Bound<'_, PyAny>) -> PyResult<()> {
292311
err::error_on_minusone(dict.py(), unsafe {
293312
ffi::PyDict_DelItem(dict.as_ptr(), key.as_ptr())
294313
})
295314
}
296315

297316
let py = self.py();
298-
inner(self, key.to_object(py).into_bound(py))
317+
inner(
318+
self,
319+
&key.into_pyobject(py)
320+
.map_err(Into::into)?
321+
.into_any()
322+
.as_borrowed(),
323+
)
299324
}
300325

301326
fn keys(&self) -> Bound<'py, PyList> {
@@ -527,6 +552,7 @@ mod borrowed_iter {
527552
}
528553
}
529554

555+
use crate::prelude::IntoPyObject;
530556
pub(crate) use borrowed_iter::BorrowedDictIter;
531557

532558
/// Conversion trait that allows a sequence of tuples to be converted into `PyDict`
@@ -552,7 +578,7 @@ where
552578
fn into_py_dict(self, py: Python<'_>) -> Bound<'_, PyDict> {
553579
let dict = PyDict::new(py);
554580
for item in self {
555-
dict.set_item(item.key(), item.value())
581+
dict.set_item(item.key().to_object(py), item.value().to_object(py))
556582
.expect("Failed to set_item on dict");
557583
}
558584
dict

0 commit comments

Comments
 (0)