Skip to content

Commit 8c6cbb6

Browse files
committed
Better docs for new unchecked_downcast and borrowed objects
1 parent 823b8e7 commit 8c6cbb6

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
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/instance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pub unsafe trait PyNativeType: Sized {
2525
///
2626
/// # Safety
2727
///
28-
/// Unless obj is not an instance of a type corresponding to Self,
29-
/// this method causes undefined behavior.
28+
/// `obj` must have the same layout as `*const ffi::PyObject` and must be
29+
/// an instance of a type corresponding to `Self`.
3030
unsafe fn unchecked_downcast(obj: &PyAny) -> &Self {
3131
&*(obj.as_ptr() as *const Self)
3232
}

0 commit comments

Comments
 (0)