-
Notifications
You must be signed in to change notification settings - Fork 823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change the initializer style #344
Conversation
py.init[_mut|_ref](|| ...)
for Py::new[_ref|_mut](py, ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
guide/src/class.md
Outdated
@@ -53,7 +53,7 @@ impl MyClass { | |||
|
|||
#[new] | |||
fn __new__(obj: &PyRawObject, num: i32) -> PyResult<()> { | |||
obj.init(|| { | |||
obj.init( { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a nit, but it looks redundant spaces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed
👍 for this change. Since we don't have
Also 👍
It looks we can allow users to define |
Eventually, I'd like to allow writing idiomatic rust style new function, .e.g.: #[pyo3]
impl Coordinate {
#[pyo3(constructor)]
fn new(x: isize, y: isize) -> Self {
Coordinate { x, y }
}
} I know that this is technically possible because I've implemented it in a wrapper, but there's still a bit missing in pyo3 to properly support that. |
8553292
to
1423942
Compare
The tests are currently failing, that's why I've cherry picked two working commits into individual pull requests |
6e429ae
to
bbfefd7
Compare
The 3.5 fails due to a travis hiccup, otherwise ci is passing |
The first commit simplifies the initializers by making them take a value instead of a function. This essentially means removing
||
.The second commit is more opinionated in that it removes
py.init[_mut|_ref](...)
, meaning that you need to callPy::new[_ref|_mut](py, ...)
. The reason is that this decouples code (the gil token shouldn't be bound to whatPy<T>
does) and that it makes more apparent that we're actually instantiating aPy<T>
here.I've also discovered that
PyRawObject::init
never actually returns an error, so I made it infallible (as in it can only fail with a panic/segfault).Eventually I'd like to make calling in consturctors
Py::new
unnecessary throughinto_object
, which would also remove theOk()
wrapping introduced in commit two.