Skip to content

Commit e9bec07

Browse files
authored
Merge pull request #887 from kngwyu/new-nativetypes
New Native Types and Lighter GILPool
2 parents 17cf97d + 75c807f commit e9bec07

27 files changed

+224
-403
lines changed

guide/src/rust_cpython.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ impl MyClass {
4747

4848
## Ownership and lifetimes
4949

50-
All objects are owned by the PyO3 library and all APIs available with references, while in rust-cpython, you own python objects.
50+
While in rust-cpython you always own python objects, PyO3 allows efficient *borrowed objects*
51+
and most APIs are available with references.
5152

5253
Here is an example of the PyList API:
5354

@@ -73,7 +74,8 @@ impl PyList {
7374
}
7475
```
7576

76-
Because PyO3 allows only references to Python objects, all references have the GIL lifetime. So the owned Python object is not required, and it is safe to have functions like `fn py<'p>(&'p self) -> Python<'p> {}`.
77+
In PyO3, all object references are bounded by the GIL lifetime.
78+
So the owned Python object is not required, and it is safe to have functions like `fn py<'p>(&'p self) -> Python<'p> {}`.
7779

7880
## Error handling
7981

src/conversion.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Conversions between various states of Rust and Python types and their wrappers.
44
use crate::err::{self, PyDowncastError, PyResult};
55
use crate::object::PyObject;
6-
use crate::type_object::{PyDowncastImpl, PyTypeInfo};
6+
use crate::type_object::PyTypeInfo;
77
use crate::types::PyTuple;
88
use crate::{ffi, gil, Py, PyAny, PyCell, PyClass, PyNativeType, PyRef, PyRefMut, Python};
99
use std::ptr::NonNull;
@@ -311,7 +311,7 @@ where
311311
/// If `T` implements `PyTryFrom`, we can convert `&PyAny` to `&T`.
312312
///
313313
/// This trait is similar to `std::convert::TryFrom`
314-
pub trait PyTryFrom<'v>: Sized + PyDowncastImpl {
314+
pub trait PyTryFrom<'v>: Sized + PyNativeType {
315315
/// Cast from a concrete Python object type to PyObject.
316316
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError>;
317317

@@ -348,7 +348,7 @@ where
348348

349349
impl<'v, T> PyTryFrom<'v> for T
350350
where
351-
T: PyDowncastImpl + PyTypeInfo + PyNativeType,
351+
T: PyTypeInfo + PyNativeType,
352352
{
353353
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError> {
354354
let value = value.into();
@@ -460,28 +460,14 @@ where
460460
T: 'p + crate::PyNativeType,
461461
{
462462
unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self> {
463-
NonNull::new(ptr).map(|p| Self::unchecked_downcast(gil::register_owned(py, p)))
463+
gil::register_owned(py, NonNull::new(ptr)?);
464+
Some(&*(ptr as *mut Self))
464465
}
465466
unsafe fn from_borrowed_ptr_or_opt(
466-
py: Python<'p>,
467-
ptr: *mut ffi::PyObject,
468-
) -> Option<&'p Self> {
469-
NonNull::new(ptr).map(|p| Self::unchecked_downcast(gil::register_borrowed(py, p)))
470-
}
471-
}
472-
473-
unsafe impl<'p, T> FromPyPointer<'p> for PyCell<T>
474-
where
475-
T: PyClass,
476-
{
477-
unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self> {
478-
NonNull::new(ptr).map(|p| &*(gil::register_owned(py, p).as_ptr() as *const PyCell<T>))
479-
}
480-
unsafe fn from_borrowed_ptr_or_opt(
481-
py: Python<'p>,
467+
_py: Python<'p>,
482468
ptr: *mut ffi::PyObject,
483469
) -> Option<&'p Self> {
484-
NonNull::new(ptr).map(|p| &*(gil::register_borrowed(py, p).as_ptr() as *const PyCell<T>))
470+
NonNull::new(ptr as *mut Self).map(|p| &*p.as_ptr())
485471
}
486472
}
487473

0 commit comments

Comments
 (0)