5858 }
5959}
6060
61+ #[ repr( transparent) ]
62+ pub struct PyExact < T : PyObjectPayload > {
63+ inner : Py < T > ,
64+ }
65+
66+ impl < T : PyPayload > PyExact < T > {
67+ /// # Safety
68+ /// Given reference must be exact type of payload T
69+ #[ inline( always) ]
70+ pub unsafe fn ref_unchecked ( r : & Py < T > ) -> & Self {
71+ & * ( r as * const _ as * const Self )
72+ }
73+ }
74+
75+ impl < T : PyPayload > Deref for PyExact < T > {
76+ type Target = Py < T > ;
77+ #[ inline( always) ]
78+ fn deref ( & self ) -> & Py < T > {
79+ & self . inner
80+ }
81+ }
82+
83+ impl < T : PyObjectPayload > Borrow < PyObject > for PyExact < T > {
84+ #[ inline( always) ]
85+ fn borrow ( & self ) -> & PyObject {
86+ self . inner . borrow ( )
87+ }
88+ }
89+
90+ impl < T : PyObjectPayload > AsRef < PyObject > for PyExact < T > {
91+ #[ inline( always) ]
92+ fn as_ref ( & self ) -> & PyObject {
93+ self . inner . as_ref ( )
94+ }
95+ }
96+
97+ impl < T : PyObjectPayload > Borrow < Py < T > > for PyExact < T > {
98+ #[ inline( always) ]
99+ fn borrow ( & self ) -> & Py < T > {
100+ & self . inner
101+ }
102+ }
103+
104+ impl < T : PyObjectPayload > AsRef < Py < T > > for PyExact < T > {
105+ #[ inline( always) ]
106+ fn as_ref ( & self ) -> & Py < T > {
107+ & self . inner
108+ }
109+ }
110+
111+ impl < T : PyPayload > std:: borrow:: ToOwned for PyExact < T > {
112+ type Owned = PyRefExact < T > ;
113+ fn to_owned ( & self ) -> Self :: Owned {
114+ let owned = self . inner . to_owned ( ) ;
115+ unsafe { PyRefExact :: new_unchecked ( owned) }
116+ }
117+ }
118+
61119#[ derive( Debug ) ]
120+ #[ repr( transparent) ]
62121pub struct PyRefExact < T : PyObjectPayload > {
63122 inner : PyRef < T > ,
64123}
@@ -109,10 +168,31 @@ impl<T: PyPayload> TryFromObject for PyRefExact<T> {
109168}
110169
111170impl < T : PyPayload > Deref for PyRefExact < T > {
112- type Target = PyRef < T > ;
171+ type Target = PyExact < T > ;
113172 #[ inline( always) ]
114- fn deref ( & self ) -> & PyRef < T > {
115- & self . inner
173+ fn deref ( & self ) -> & PyExact < T > {
174+ unsafe { PyExact :: ref_unchecked ( self . inner . deref ( ) ) }
175+ }
176+ }
177+
178+ impl < T : PyObjectPayload > Borrow < PyObject > for PyRefExact < T > {
179+ #[ inline( always) ]
180+ fn borrow ( & self ) -> & PyObject {
181+ self . inner . borrow ( )
182+ }
183+ }
184+
185+ impl < T : PyObjectPayload > AsRef < PyObject > for PyRefExact < T > {
186+ #[ inline( always) ]
187+ fn as_ref ( & self ) -> & PyObject {
188+ self . inner . as_ref ( )
189+ }
190+ }
191+
192+ impl < T : PyObjectPayload > Borrow < Py < T > > for PyRefExact < T > {
193+ #[ inline( always) ]
194+ fn borrow ( & self ) -> & Py < T > {
195+ self . inner . borrow ( )
116196 }
117197}
118198
@@ -123,6 +203,20 @@ impl<T: PyObjectPayload> AsRef<Py<T>> for PyRefExact<T> {
123203 }
124204}
125205
206+ impl < T : PyPayload > Borrow < PyExact < T > > for PyRefExact < T > {
207+ #[ inline( always) ]
208+ fn borrow ( & self ) -> & PyExact < T > {
209+ self
210+ }
211+ }
212+
213+ impl < T : PyPayload > AsRef < PyExact < T > > for PyRefExact < T > {
214+ #[ inline( always) ]
215+ fn as_ref ( & self ) -> & PyExact < T > {
216+ self
217+ }
218+ }
219+
126220impl < T : PyPayload > ToPyObject for PyRefExact < T > {
127221 #[ inline( always) ]
128222 fn to_pyobject ( self , _vm : & VirtualMachine ) -> PyObjectRef {
0 commit comments