Skip to content

Commit 96cb7d0

Browse files
committed
migrate PyDictMethods trait bounds
1 parent ce18f79 commit 96cb7d0

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
///
@@ -130,7 +130,7 @@ pub trait PyDictMethods<'py>: crate::sealed::Sealed {
130130
/// This is equivalent to the Python expression `key in self`.
131131
fn contains<K>(&self, key: K) -> PyResult<bool>
132132
where
133-
K: ToPyObject;
133+
K: IntoPyObject<'py>;
134134

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

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

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

159159
/// Returns a list of dict keys.
160160
///
@@ -226,9 +226,9 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
226226

227227
fn contains<K>(&self, key: K) -> PyResult<bool>
228228
where
229-
K: ToPyObject,
229+
K: IntoPyObject<'py>,
230230
{
231-
fn inner(dict: &Bound<'_, PyDict>, key: Bound<'_, PyAny>) -> PyResult<bool> {
231+
fn inner(dict: &Bound<'_, PyDict>, key: &Bound<'_, PyAny>) -> PyResult<bool> {
232232
match unsafe { ffi::PyDict_Contains(dict.as_ptr(), key.as_ptr()) } {
233233
1 => Ok(true),
234234
0 => Ok(false),
@@ -237,16 +237,22 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
237237
}
238238

239239
let py = self.py();
240-
inner(self, key.to_object(py).into_bound(py))
240+
inner(
241+
self,
242+
&key.into_pyobject(py)
243+
.map_err(Into::into)?
244+
.into_any()
245+
.as_borrowed(),
246+
)
241247
}
242248

243249
fn get_item<K>(&self, key: K) -> PyResult<Option<Bound<'py, PyAny>>>
244250
where
245-
K: ToPyObject,
251+
K: IntoPyObject<'py>,
246252
{
247253
fn inner<'py>(
248254
dict: &Bound<'py, PyDict>,
249-
key: Bound<'_, PyAny>,
255+
key: &Bound<'_, PyAny>,
250256
) -> PyResult<Option<Bound<'py, PyAny>>> {
251257
let py = dict.py();
252258
let mut result: *mut ffi::PyObject = std::ptr::null_mut();
@@ -260,18 +266,24 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
260266
}
261267

262268
let py = self.py();
263-
inner(self, key.to_object(py).into_bound(py))
269+
inner(
270+
self,
271+
&key.into_pyobject(py)
272+
.map_err(Into::into)?
273+
.into_any()
274+
.as_borrowed(),
275+
)
264276
}
265277

266278
fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>
267279
where
268-
K: ToPyObject,
269-
V: ToPyObject,
280+
K: IntoPyObject<'py>,
281+
V: IntoPyObject<'py>,
270282
{
271283
fn inner(
272284
dict: &Bound<'_, PyDict>,
273-
key: Bound<'_, PyAny>,
274-
value: Bound<'_, PyAny>,
285+
key: &Bound<'_, PyAny>,
286+
value: &Bound<'_, PyAny>,
275287
) -> PyResult<()> {
276288
err::error_on_minusone(dict.py(), unsafe {
277289
ffi::PyDict_SetItem(dict.as_ptr(), key.as_ptr(), value.as_ptr())
@@ -281,23 +293,36 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
281293
let py = self.py();
282294
inner(
283295
self,
284-
key.to_object(py).into_bound(py),
285-
value.to_object(py).into_bound(py),
296+
&key.into_pyobject(py)
297+
.map_err(Into::into)?
298+
.into_any()
299+
.as_borrowed(),
300+
&value
301+
.into_pyobject(py)
302+
.map_err(Into::into)?
303+
.into_any()
304+
.as_borrowed(),
286305
)
287306
}
288307

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

299318
let py = self.py();
300-
inner(self, key.to_object(py).into_bound(py))
319+
inner(
320+
self,
321+
&key.into_pyobject(py)
322+
.map_err(Into::into)?
323+
.into_any()
324+
.as_borrowed(),
325+
)
301326
}
302327

303328
fn keys(&self) -> Bound<'py, PyList> {
@@ -529,6 +554,7 @@ mod borrowed_iter {
529554
}
530555
}
531556

557+
use crate::prelude::IntoPyObject;
532558
pub(crate) use borrowed_iter::BorrowedDictIter;
533559

534560
/// Conversion trait that allows a sequence of tuples to be converted into `PyDict`
@@ -554,7 +580,7 @@ where
554580
fn into_py_dict(self, py: Python<'_>) -> Bound<'_, PyDict> {
555581
let dict = PyDict::new(py);
556582
for item in self {
557-
dict.set_item(item.key(), item.value())
583+
dict.set_item(item.key().to_object(py), item.value().to_object(py))
558584
.expect("Failed to set_item on dict");
559585
}
560586
dict

0 commit comments

Comments
 (0)