@@ -28,6 +28,19 @@ pub unsafe trait PyNativeType: Sized {
28
28
/// The form of this which is stored inside a `Py<T>` smart pointer.
29
29
type AsRefSource : HasPyGilRef < AsRefTarget = Self > ;
30
30
31
+ /// Cast `&self` to a `Borrowed` smart pointer.
32
+ ///
33
+ /// `Borrowed<T>` implements `Deref<Target=Bound<T>>`, so can also be used in locations
34
+ /// where `Bound<T>` is expected.
35
+ ///
36
+ /// This is available as a migration tool to adjust code from the deprecated "GIL Refs"
37
+ /// API to the `Bound` smart pointer API.
38
+ fn as_borrowed ( & self ) -> Borrowed < ' _ , ' _ , Self :: AsRefSource > {
39
+ // Safety: &'py Self is expected to be a Python pointer,
40
+ // so has the same layout as Borrowed<'py, 'py, T>
41
+ unsafe { std:: mem:: transmute ( self ) }
42
+ }
43
+
31
44
/// Returns a GIL marker constrained to the lifetime of this type.
32
45
#[ inline]
33
46
fn py ( & self ) -> Python < ' _ > {
@@ -184,16 +197,13 @@ impl<'py, T> Bound<'py, T> {
184
197
self . into_non_null ( ) . as_ptr ( )
185
198
}
186
199
187
- /// Internal helper to convert e.g. &'a &'py PyDict to &'a Bound<'py, PyDict> for
188
- /// backwards-compatibility during migration to removal of pool.
189
- #[ doc( hidden) ] // public and doc(hidden) to use in examples and tests for now
190
- pub fn borrowed_from_gil_ref < ' a , U > ( gil_ref : & ' a & ' py U ) -> & ' a Self
191
- where
192
- U : PyNativeType < AsRefSource = T > ,
193
- {
194
- // Safety: &'py T::AsRefTarget is expected to be a Python pointer,
195
- // so &'a &'py T::AsRefTarget has the same layout as &'a Bound<'py, T>
196
- unsafe { std:: mem:: transmute ( gil_ref) }
200
+ /// Casts this `Bound<T>` to a `Borrowed<T>` smart pointer.
201
+ pub fn as_borrowed < ' a > ( & ' a self ) -> Borrowed < ' a , ' py , T > {
202
+ Borrowed (
203
+ unsafe { NonNull :: new_unchecked ( self . as_ptr ( ) ) } ,
204
+ PhantomData ,
205
+ self . py ( ) ,
206
+ )
197
207
}
198
208
199
209
/// Casts this `Bound<T>` as the corresponding "GIL Ref" type.
@@ -299,24 +309,14 @@ impl<'a, 'py> Borrowed<'a, 'py, PyAny> {
299
309
impl < ' a , ' py , T > From < & ' a Bound < ' py , T > > for Borrowed < ' a , ' py , T > {
300
310
/// Create borrow on a Bound
301
311
fn from ( instance : & ' a Bound < ' py , T > ) -> Self {
302
- Self (
303
- unsafe { NonNull :: new_unchecked ( instance. as_ptr ( ) ) } ,
304
- PhantomData ,
305
- instance. py ( ) ,
306
- )
312
+ instance. as_borrowed ( )
307
313
}
308
314
}
309
315
310
316
impl < ' py , T > Borrowed < ' py , ' py , T >
311
317
where
312
318
T : HasPyGilRef ,
313
319
{
314
- pub ( crate ) fn from_gil_ref ( gil_ref : & ' py T :: AsRefTarget ) -> Self {
315
- // Safety: &'py T::AsRefTarget is expected to be a Python pointer,
316
- // so &'py T::AsRefTarget has the same layout as Self.
317
- unsafe { std:: mem:: transmute ( gil_ref) }
318
- }
319
-
320
320
// pub(crate) fn into_gil_ref(self) -> &'py T::AsRefTarget {
321
321
// // Safety: self is a borrow over `'py`.
322
322
// unsafe { self.py().from_borrowed_ptr(self.0.as_ptr()) }
@@ -1366,7 +1366,7 @@ where
1366
1366
{
1367
1367
/// Extracts `Self` from the source `PyObject`.
1368
1368
fn extract ( ob : & ' a PyAny ) -> PyResult < Self > {
1369
- Bound :: borrowed_from_gil_ref ( & ob )
1369
+ ob . as_borrowed ( )
1370
1370
. downcast ( )
1371
1371
. map ( Clone :: clone)
1372
1372
. map_err ( Into :: into)
@@ -1485,7 +1485,7 @@ impl PyObject {
1485
1485
mod tests {
1486
1486
use super :: { Bound , Py , PyObject } ;
1487
1487
use crate :: types:: { PyDict , PyString } ;
1488
- use crate :: { PyAny , PyResult , Python , ToPyObject } ;
1488
+ use crate :: { PyAny , PyNativeType , PyResult , Python , ToPyObject } ;
1489
1489
1490
1490
#[ test]
1491
1491
fn test_call0 ( ) {
@@ -1612,8 +1612,11 @@ a = A()
1612
1612
#[ test]
1613
1613
fn test_py2_into_py_object ( ) {
1614
1614
Python :: with_gil ( |py| {
1615
- let instance: Bound < ' _ , PyAny > =
1616
- Bound :: borrowed_from_gil_ref ( & py. eval ( "object()" , None , None ) . unwrap ( ) ) . clone ( ) ;
1615
+ let instance = py
1616
+ . eval ( "object()" , None , None )
1617
+ . unwrap ( )
1618
+ . as_borrowed ( )
1619
+ . to_owned ( ) ;
1617
1620
let ptr = instance. as_ptr ( ) ;
1618
1621
let instance: PyObject = instance. clone ( ) . into ( ) ;
1619
1622
assert_eq ! ( instance. as_ptr( ) , ptr) ;
0 commit comments