@@ -6,7 +6,7 @@ use crate::instance::{Borrowed, Bound};
6
6
use crate :: py_result_ext:: PyResultExt ;
7
7
use crate :: types:: any:: PyAnyMethods ;
8
8
use crate :: types:: { PyAny , PyList } ;
9
- use crate :: { ffi, Python , ToPyObject } ;
9
+ use crate :: { ffi, BoundObject , Python , ToPyObject } ;
10
10
11
11
/// Represents a Python `dict`.
12
12
///
@@ -128,7 +128,7 @@ pub trait PyDictMethods<'py>: crate::sealed::Sealed {
128
128
/// This is equivalent to the Python expression `key in self`.
129
129
fn contains < K > ( & self , key : K ) -> PyResult < bool >
130
130
where
131
- K : ToPyObject ;
131
+ K : IntoPyObject < ' py > ;
132
132
133
133
/// Gets an item from the dictionary.
134
134
///
@@ -137,22 +137,22 @@ pub trait PyDictMethods<'py>: crate::sealed::Sealed {
137
137
/// To get a `KeyError` for non-existing keys, use `PyAny::get_item`.
138
138
fn get_item < K > ( & self , key : K ) -> PyResult < Option < Bound < ' py , PyAny > > >
139
139
where
140
- K : ToPyObject ;
140
+ K : IntoPyObject < ' py > ;
141
141
142
142
/// Sets an item value.
143
143
///
144
144
/// This is equivalent to the Python statement `self[key] = value`.
145
145
fn set_item < K , V > ( & self , key : K , value : V ) -> PyResult < ( ) >
146
146
where
147
- K : ToPyObject ,
148
- V : ToPyObject ;
147
+ K : IntoPyObject < ' py > ,
148
+ V : IntoPyObject < ' py > ;
149
149
150
150
/// Deletes an item.
151
151
///
152
152
/// This is equivalent to the Python statement `del self[key]`.
153
153
fn del_item < K > ( & self , key : K ) -> PyResult < ( ) >
154
154
where
155
- K : ToPyObject ;
155
+ K : IntoPyObject < ' py > ;
156
156
157
157
/// Returns a list of dict keys.
158
158
///
@@ -224,9 +224,9 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
224
224
225
225
fn contains < K > ( & self , key : K ) -> PyResult < bool >
226
226
where
227
- K : ToPyObject ,
227
+ K : IntoPyObject < ' py > ,
228
228
{
229
- fn inner ( dict : & Bound < ' _ , PyDict > , key : Bound < ' _ , PyAny > ) -> PyResult < bool > {
229
+ fn inner ( dict : & Bound < ' _ , PyDict > , key : & Bound < ' _ , PyAny > ) -> PyResult < bool > {
230
230
match unsafe { ffi:: PyDict_Contains ( dict. as_ptr ( ) , key. as_ptr ( ) ) } {
231
231
1 => Ok ( true ) ,
232
232
0 => Ok ( false ) ,
@@ -235,16 +235,22 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
235
235
}
236
236
237
237
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
+ )
239
245
}
240
246
241
247
fn get_item < K > ( & self , key : K ) -> PyResult < Option < Bound < ' py , PyAny > > >
242
248
where
243
- K : ToPyObject ,
249
+ K : IntoPyObject < ' py > ,
244
250
{
245
251
fn inner < ' py > (
246
252
dict : & Bound < ' py , PyDict > ,
247
- key : Bound < ' _ , PyAny > ,
253
+ key : & Bound < ' _ , PyAny > ,
248
254
) -> PyResult < Option < Bound < ' py , PyAny > > > {
249
255
let py = dict. py ( ) ;
250
256
let mut result: * mut ffi:: PyObject = std:: ptr:: null_mut ( ) ;
@@ -258,18 +264,24 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
258
264
}
259
265
260
266
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
+ )
262
274
}
263
275
264
276
fn set_item < K , V > ( & self , key : K , value : V ) -> PyResult < ( ) >
265
277
where
266
- K : ToPyObject ,
267
- V : ToPyObject ,
278
+ K : IntoPyObject < ' py > ,
279
+ V : IntoPyObject < ' py > ,
268
280
{
269
281
fn inner (
270
282
dict : & Bound < ' _ , PyDict > ,
271
- key : Bound < ' _ , PyAny > ,
272
- value : Bound < ' _ , PyAny > ,
283
+ key : & Bound < ' _ , PyAny > ,
284
+ value : & Bound < ' _ , PyAny > ,
273
285
) -> PyResult < ( ) > {
274
286
err:: error_on_minusone ( dict. py ( ) , unsafe {
275
287
ffi:: PyDict_SetItem ( dict. as_ptr ( ) , key. as_ptr ( ) , value. as_ptr ( ) )
@@ -279,23 +291,36 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
279
291
let py = self . py ( ) ;
280
292
inner (
281
293
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 ( ) ,
284
303
)
285
304
}
286
305
287
306
fn del_item < K > ( & self , key : K ) -> PyResult < ( ) >
288
307
where
289
- K : ToPyObject ,
308
+ K : IntoPyObject < ' py > ,
290
309
{
291
- fn inner ( dict : & Bound < ' _ , PyDict > , key : Bound < ' _ , PyAny > ) -> PyResult < ( ) > {
310
+ fn inner ( dict : & Bound < ' _ , PyDict > , key : & Bound < ' _ , PyAny > ) -> PyResult < ( ) > {
292
311
err:: error_on_minusone ( dict. py ( ) , unsafe {
293
312
ffi:: PyDict_DelItem ( dict. as_ptr ( ) , key. as_ptr ( ) )
294
313
} )
295
314
}
296
315
297
316
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
+ )
299
324
}
300
325
301
326
fn keys ( & self ) -> Bound < ' py , PyList > {
@@ -527,6 +552,7 @@ mod borrowed_iter {
527
552
}
528
553
}
529
554
555
+ use crate :: prelude:: IntoPyObject ;
530
556
pub ( crate ) use borrowed_iter:: BorrowedDictIter ;
531
557
532
558
/// Conversion trait that allows a sequence of tuples to be converted into `PyDict`
@@ -552,7 +578,7 @@ where
552
578
fn into_py_dict ( self , py : Python < ' _ > ) -> Bound < ' _ , PyDict > {
553
579
let dict = PyDict :: new ( py) ;
554
580
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 ) )
556
582
. expect ( "Failed to set_item on dict" ) ;
557
583
}
558
584
dict
0 commit comments