@@ -42,7 +42,7 @@ For more, see [the constructor section](https://pyo3.rs/master/class.html#constr
42
42
PyO3 0.9 introduces [ ` PyCell ` ] ( https://pyo3.rs/master/doc/pyo3/pycell/struct.PyCell.html ) , which is
43
43
a [ ` RefCell ` ] ( https://doc.rust-lang.org/std/cell/struct.RefCell.html ) like object wrapper
44
44
for dynamically ensuring
45
- [ Rust's rules of references ] ( https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#the-rules-of-references ) .
45
+ [ The Rules of References ] ( https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#the-rules-of-references ) .
46
46
47
47
For ` #[pymethods] ` or ` #[pyfunction] ` s, your existing code should continue to work without any change.
48
48
Python exceptions will automatically be raised when your functions are used in a way which breaks Rust's
@@ -58,13 +58,13 @@ struct Names {
58
58
59
59
#[pymethods]
60
60
impl Names {
61
- #[new]
62
- fn new() -> Self {
63
- Names { names: vec![] }
64
- }
65
- fn merge(&mut self, other: &mut Names) {
66
- self.names.append(&mut other.names)
67
- }
61
+ #[new]
62
+ fn new() -> Self {
63
+ Names { names: vec![] }
64
+ }
65
+ fn merge(&mut self, other: &mut Names) {
66
+ self.names.append(&mut other.names)
67
+ }
68
68
}
69
69
# let gil = Python::acquire_gil();
70
70
# let py = gil.python();
@@ -78,16 +78,18 @@ impl Names {
78
78
# isinstance(e, borrow_mut_err)
79
79
# ");
80
80
```
81
- ` Names ` has ` merge ` method, which takes ` &mut self ` and ` &mut Self ` .
81
+ ` Names ` has a ` merge ` method, which takes ` &mut self ` and another argument of type ` &mut Self ` .
82
82
Given this ` #[pyclass] ` , calling ` names.merge(names) ` in Python raises a ` PyBorrowMutError ` exception,
83
83
since it requires two mutable borrows of ` names ` .
84
84
85
- However, for ` #[pyproto] ` and some functions, you need to manually fix codes .
85
+ However, for ` #[pyproto] ` and some functions, you need to manually fix the code .
86
86
87
87
#### Object creation
88
- We could use the older ` PyRef ` and ` PyRefMut ` for object creation, but now they are just
89
- reference wrappers for ` PyCell ` .
90
- Use ` PyCell::new ` instead.
88
+ In 0.8 object creation was done with ` PyRef::new ` and ` PyRefMut::new ` .
89
+ In 0.9 these have both been removed.
90
+ To upgrade code, please use ` PyCell::new ` instead.
91
+ If a ` PyRef ` or ` PyRefMut ` is needed, just call ` .borrow() ` or ` .borrow_mut() `
92
+ on the newly-created ` PyCell ` .
91
93
92
94
Before:
93
95
``` compile_fail
@@ -111,8 +113,8 @@ let obj_ref = obj.borrow();
111
113
```
112
114
113
115
#### Object extraction
114
- ` T: PyClass` , ` &T ` and ` &mut T ` no longer have ` FromPyObject ` implementations.
115
- Instead you should extract ` &PyCell ` , ` PyRef ` , and ` PyRefMut ` respectively.
116
+ For ` PyClass ` types ` T ` , ` &T ` and ` &mut T ` no longer have ` FromPyObject ` implementations.
117
+ Instead you should extract ` PyRef<T> ` or ` PyRefMut<T> ` , respectively. You can also extract ` &PyCell<T> ` .
116
118
117
119
Before:
118
120
``` ignore
@@ -125,7 +127,7 @@ After:
125
127
```
126
128
# use pyo3::prelude::*;
127
129
# use pyo3::types::{PyAny, IntoPyDict};
128
- # #[pyclass] struct MyClass {}
130
+ # #[derive(Clone)] #[ pyclass] struct MyClass {}
129
131
# #[pymethods] impl MyClass { #[new]fn new() -> Self { MyClass {} }}
130
132
# let gil = Python::acquire_gil();
131
133
# let py = gil.python();
@@ -134,6 +136,7 @@ After:
134
136
# let create_obj = || py.eval("c()", None, Some(d)).unwrap();
135
137
let obj: &PyAny = create_obj();
136
138
let obj_cell: &PyCell<MyClass> = obj.extract().unwrap();
139
+ let obj: MyClass = obj.extract().unwrap(); // extracted via Clone
137
140
{
138
141
let obj_ref: PyRef<MyClass> = obj.extract().unwrap();
139
142
// we need to drop obj_ref before we can extract a PyRefMut due to Rust's rules of references
@@ -143,8 +146,8 @@ let obj_ref_mut: PyRefMut<MyClass> = obj.extract().unwrap();
143
146
144
147
145
148
#### ` #[pyproto] `
146
- Most of ` #[pyproto] ` arguments requires [ ` FromPyObject ` ] implementation.
147
- So if your protocol methods take ` &T ` or ` &mut T ` (where ` T: PyClass ` ),
149
+ Most of the arguments to methods in ` #[pyproto] ` impls require a [ ` FromPyObject ` ] implementation.
150
+ So if your protocol methods take ` &T ` or ` &mut T ` (where ` T: PyClass ` ),
148
151
please use ` PyRef ` or ` PyRefMut ` instead.
149
152
150
153
Before:
0 commit comments