Better docs for new unchecked_downcast and borrowed objects

This commit is contained in:
kngwyu 2020-05-02 13:00:12 +09:00
parent 823b8e7f8a
commit 8c6cbb605a
2 changed files with 6 additions and 4 deletions

View File

@ -47,7 +47,8 @@ impl MyClass {
## Ownership and lifetimes
All objects are owned by the PyO3 library and all APIs available with references, while in rust-cpython, you own python objects.
While in rust-cpython you always own python objects, PyO3 allows efficient *borrowed objects*
and most APIs are available with references.
Here is an example of the PyList API:
@ -73,7 +74,8 @@ impl PyList {
}
```
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> {}`.
In PyO3, all object references are bounded by 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> {}`.
## Error handling

View File

@ -25,8 +25,8 @@ pub unsafe trait PyNativeType: Sized {
///
/// # Safety
///
/// Unless obj is not an instance of a type corresponding to Self,
/// this method causes undefined behavior.
/// `obj` must have the same layout as `*const ffi::PyObject` and must be
/// an instance of a type corresponding to `Self`.
unsafe fn unchecked_downcast(obj: &PyAny) -> &Self {
&*(obj.as_ptr() as *const Self)
}