-
Notifications
You must be signed in to change notification settings - Fork 770
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
Add PyClass borrow methods to Py #976
Conversation
What I meant is |
Ah, ok. Shall I close this PR? EDIT: I think perhaps there's more cases where If |
At least I don't think we need |
:( I would have loved this change; most of the time I can't meaningfully interact with a |
Tbh me too which was one of the reasons I proposed these methods :D. I'd definitely in favour of reopening this PR if @kngwyu is willing to accept these despite the slight duplication they introduce. |
Thank you for the feedback.
|
I think if we think
I think we probably should recommend
I agree with you that |
Though I think |
Thanks for the feedback - I have simplified this PR to just add the new methods. |
Nice! |
@@ -73,7 +73,11 @@ pub unsafe fn tp_free_fallback(obj: *mut ffi::PyObject) { | |||
/// The `#[pyclass]` attribute automatically implements this trait for your Rust struct, | |||
/// so you don't have to use this trait directly. | |||
pub trait PyClass: | |||
PyTypeInfo<Layout = PyCell<Self>> + Sized + PyClassAlloc + PyMethods + Send | |||
PyTypeInfo<Layout = PyCell<Self>, AsRefTarget = PyCell<Self>> |
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.
Alternatively I can add AsRefTarget = PyCell<Self>
bound just to Py<T>
implementation, but I think it always applies.
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.
👍
Thank you! |
This is a bit of an experimental follow-up to #974 .
As
Py::new
is more efficient thanPyCell::new
, maybe we can help encourage use ofPy::new
in most situations by adding.borrow()
,.try_borrow()
,.borrow_mut()
and.try_borrow_mut()
toPy
.It seems quite nice to me and leads to no breaking changes. If others also like this then I'd like to update the guide a bit to recommend use of
Py<T>
a bit clearer before merging.