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