@@ -6,7 +6,7 @@ use crate::instance::{Borrowed, Bound};
66use crate :: py_result_ext:: PyResultExt ;
77use crate :: types:: any:: PyAnyMethods ;
88use crate :: types:: { PyAny , PyList } ;
9- use crate :: { ffi, BoundObject , Python , ToPyObject } ;
9+ use crate :: { ffi, BoundObject , Python } ;
1010
1111/// Represents a Python `dict`.
1212///
@@ -557,76 +557,73 @@ pub(crate) use borrowed_iter::BorrowedDictIter;
557557
558558/// Conversion trait that allows a sequence of tuples to be converted into `PyDict`
559559/// Primary use case for this trait is `call` and `call_method` methods as keywords argument.
560- pub trait IntoPyDict : Sized {
560+ pub trait IntoPyDict < ' py > : Sized {
561561 /// Converts self into a `PyDict` object pointer. Whether pointer owned or borrowed
562562 /// depends on implementation.
563- fn into_py_dict ( self , py : Python < ' _ > ) -> Bound < ' _ , PyDict > ;
563+ fn into_py_dict ( self , py : Python < ' py > ) -> Bound < ' _ , PyDict > ;
564564
565565 /// Deprecated name for [`IntoPyDict::into_py_dict`].
566566 #[ deprecated( since = "0.23.0" , note = "renamed to `IntoPyDict::into_py_dict`" ) ]
567567 #[ inline]
568- fn into_py_dict_bound ( self , py : Python < ' _ > ) -> Bound < ' _ , PyDict > {
568+ fn into_py_dict_bound ( self , py : Python < ' py > ) -> Bound < ' _ , PyDict > {
569569 self . into_py_dict ( py)
570570 }
571571}
572572
573- impl < T , I > IntoPyDict for I
573+ impl < ' py , T , I > IntoPyDict < ' py > for I
574574where
575- T : PyDictItem ,
575+ T : PyDictItem < ' py > ,
576576 I : IntoIterator < Item = T > ,
577577{
578- fn into_py_dict ( self , py : Python < ' _ > ) -> Bound < ' _ , PyDict > {
578+ fn into_py_dict ( self , py : Python < ' py > ) -> Bound < ' _ , PyDict > {
579579 let dict = PyDict :: new ( py) ;
580580 for item in self {
581- dict. set_item ( item. key ( ) . to_object ( py) , item. value ( ) . to_object ( py) )
581+ let ( key, value) = item. unpack ( ) ;
582+ dict. set_item ( key, value)
582583 . expect ( "Failed to set_item on dict" ) ;
583584 }
584585 dict
585586 }
586587}
587588
588589/// Represents a tuple which can be used as a PyDict item.
589- pub trait PyDictItem {
590- type K : ToPyObject ;
591- type V : ToPyObject ;
592- fn key ( & self ) -> & Self :: K ;
593- fn value ( & self ) -> & Self :: V ;
590+ pub trait PyDictItem < ' py > {
591+ type K : IntoPyObject < ' py > ;
592+ type V : IntoPyObject < ' py > ;
593+ fn unpack ( self ) -> ( Self :: K , Self :: V ) ;
594594}
595595
596- impl < K , V > PyDictItem for ( K , V )
596+ impl < ' py , K , V > PyDictItem < ' py > for ( K , V )
597597where
598- K : ToPyObject ,
599- V : ToPyObject ,
598+ K : IntoPyObject < ' py > ,
599+ V : IntoPyObject < ' py > ,
600600{
601601 type K = K ;
602602 type V = V ;
603- fn key ( & self ) -> & Self :: K {
604- & self . 0
605- }
606- fn value ( & self ) -> & Self :: V {
607- & self . 1
603+
604+ fn unpack ( self ) -> ( Self :: K , Self :: V ) {
605+ ( self . 0 , self . 1 )
608606 }
609607}
610608
611- impl < K , V > PyDictItem for & ( K , V )
609+ impl < ' a , ' py , K , V > PyDictItem < ' py > for & ' a ( K , V )
612610where
613- K : ToPyObject ,
614- V : ToPyObject ,
611+ & ' a K : IntoPyObject < ' py > ,
612+ & ' a V : IntoPyObject < ' py > ,
615613{
616- type K = K ;
617- type V = V ;
618- fn key ( & self ) -> & Self :: K {
619- & self . 0
620- }
621- fn value ( & self ) -> & Self :: V {
622- & self . 1
614+ type K = & ' a K ;
615+ type V = & ' a V ;
616+
617+ fn unpack ( self ) -> ( Self :: K , Self :: V ) {
618+ ( & self . 0 , & self . 1 )
623619 }
624620}
625621
626622#[ cfg( test) ]
627623mod tests {
628624 use super :: * ;
629625 use crate :: types:: PyTuple ;
626+ use crate :: ToPyObject ;
630627 use std:: collections:: { BTreeMap , HashMap } ;
631628
632629 #[ test]
0 commit comments