33*/
44use crate :: {
55 convert:: ToPyResult ,
6- function:: { OwnedParam , RefParam } ,
6+ function:: { BorrowedParam , OwnedParam , RefParam } ,
77 object:: PyThreadingConstraint ,
8- PyObjectRef , PyPayload , PyRef , PyResult , TryFromObject , VirtualMachine ,
8+ Py , PyObjectRef , PyPayload , PyRef , PyResult , TryFromObject , VirtualMachine ,
99} ;
1010
1111#[ derive( result_like:: OptionLike , is_macro:: Is , Debug ) ]
7474 }
7575}
7676
77+ impl < F , S , R > IntoPyGetterFunc < ( BorrowedParam < S > , R , VirtualMachine ) > for F
78+ where
79+ F : Fn ( & Py < S > , & VirtualMachine ) -> R + ' static + Send + Sync ,
80+ S : PyPayload ,
81+ R : ToPyResult ,
82+ {
83+ fn get ( & self , obj : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
84+ let zelf = PyRef :: < S > :: try_from_object ( vm, obj) ?;
85+ ( self ) ( & zelf, vm) . to_pyresult ( vm)
86+ }
87+ }
88+
7789impl < F , S , R > IntoPyGetterFunc < ( RefParam < S > , R , VirtualMachine ) > for F
7890where
7991 F : Fn ( & S , & VirtualMachine ) -> R + ' static + Send + Sync ,
@@ -98,6 +110,18 @@ where
98110 }
99111}
100112
113+ impl < F , S , R > IntoPyGetterFunc < ( BorrowedParam < S > , R ) > for F
114+ where
115+ F : Fn ( & Py < S > ) -> R + ' static + Send + Sync ,
116+ S : PyPayload ,
117+ R : ToPyResult ,
118+ {
119+ fn get ( & self , obj : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
120+ let zelf = PyRef :: < S > :: try_from_object ( vm, obj) ?;
121+ ( self ) ( & zelf) . to_pyresult ( vm)
122+ }
123+ }
124+
101125impl < F , S , R > IntoPyGetterFunc < ( RefParam < S > , R ) > for F
102126where
103127 F : Fn ( & S ) -> R + ' static + Send + Sync ,
@@ -149,6 +173,20 @@ where
149173 }
150174}
151175
176+ impl < F , S , V , R > IntoPySetterFunc < ( BorrowedParam < S > , V , R , VirtualMachine ) > for F
177+ where
178+ F : Fn ( & Py < S > , V , & VirtualMachine ) -> R + ' static + Send + Sync ,
179+ S : PyPayload ,
180+ V : FromPySetterValue ,
181+ R : IntoPyNoResult ,
182+ {
183+ fn set ( & self , obj : PyObjectRef , value : PySetterValue , vm : & VirtualMachine ) -> PyResult < ( ) > {
184+ let zelf = PyRef :: < S > :: try_from_object ( vm, obj) ?;
185+ let value = V :: from_setter_value ( vm, value) ?;
186+ ( self ) ( & zelf, value, vm) . into_noresult ( )
187+ }
188+ }
189+
152190impl < F , S , V , R > IntoPySetterFunc < ( RefParam < S > , V , R , VirtualMachine ) > for F
153191where
154192 F : Fn ( & S , V , & VirtualMachine ) -> R + ' static + Send + Sync ,
@@ -177,6 +215,20 @@ where
177215 }
178216}
179217
218+ impl < F , S , V , R > IntoPySetterFunc < ( BorrowedParam < S > , V , R ) > for F
219+ where
220+ F : Fn ( & Py < S > , V ) -> R + ' static + Send + Sync ,
221+ S : PyPayload ,
222+ V : FromPySetterValue ,
223+ R : IntoPyNoResult ,
224+ {
225+ fn set ( & self , obj : PyObjectRef , value : PySetterValue , vm : & VirtualMachine ) -> PyResult < ( ) > {
226+ let zelf = PyRef :: < S > :: try_from_object ( vm, obj) ?;
227+ let value = V :: from_setter_value ( vm, value) ?;
228+ ( self ) ( & zelf, value) . into_noresult ( )
229+ }
230+ }
231+
180232impl < F , S , V , R > IntoPySetterFunc < ( RefParam < S > , V , R ) > for F
181233where
182234 F : Fn ( & S , V ) -> R + ' static + Send + Sync ,
0 commit comments