-
Notifications
You must be signed in to change notification settings - Fork 792
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
Introduce #[pyclass(unsendable)] #1009
Conversation
292c3ef
to
3a07e8d
Compare
|
@programmerjake |
3a07e8d
to
1566058
Compare
I think it's safe in that outer class cannot implement |
8addc6b
to
94eb102
Compare
I think the problem is that this "safe" code is effectively equivalent to
I'm using |
Why does the stub have the type argument btw? (For the "real" checker it's used to format the panic message) |
Ah, I understand, thanks. Then it have to be
For checking |
94eb102
to
990a23d
Compare
Having read through this implementation, I think it's very elegant. I had reservations about whether this was necessary for the complexity it adds. But the implementation is rather neat and should be zero-cost for sendable pyclasses after optimisations, so I have warmed to this idea.
You might be interested in this example for https://docs.rs/quote/1.0.7/quote/macro.quote_spanned.html#example We could then just skip generating the check when Though I think doing that trick might mean that users who are manually implementing |
For the documentation: I'm thinking that there's a new chapter I'd like to write explaining with better examples the GIL, threading, Send and So if you want, instead of writing the documentation in this PR, open an issue once this PR is merged and assign it to me. |
Well, it would be good |
990a23d
to
e5cda5f
Compare
I changed the trait declaration to pub trait PyClassSend {
type ThreadChecker: PyClassThreadChecker<Self>;
} for inhibiting invalid |
I think the I think this would still be possible for a user to write:
And then the "safe" code above can just use |
It's prevented by |
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.
This is great, thank you! 💯
Very true. Thanks, I'm satisfied! |
e5cda5f
to
d76fe78
Compare
Now
PyClass
requiresSend
, but what should we do when we really don't want it sent to another thread?Then we can use
#[pyclass(unsendable)]
.Inspired by send-wrapper, a class with
unsendable
panics when it is accessed by another thread.To enable this, I introduced the
PyClassSend
trait.Without
unsendable
, we useThreadCheckerStub
as a ThreadChecker, which requiresSend
. Thus notunsendable
class has to beSend
able.