-
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
require #[pyclass]
to be Sync
#4566
Merged
+301
−131
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2f9ebf3
require `#[pyclass]` to be `Sync`
davidhewitt bbc2049
silence deprecation warning in pyo3 internals
ngoldbaum 799a7a9
make 'cargo test' pass (modulo test_getter_setter)
ngoldbaum 9d9c2e5
fix nox -s test-py
ngoldbaum 862b882
silence new deprecation warning
ngoldbaum 0b8d5fa
use `unsendable` to test cell get / set
davidhewitt 245300b
add WIP changelog entry
ngoldbaum a7381a0
fix wasm clippy
ngoldbaum 9e50a84
add a test for assert_pyclass_sync to fix coverage error
ngoldbaum eedd5b8
use a better name
ngoldbaum dc8b7e7
fix cargo doc --lib
ngoldbaum 4a26752
fix building with no default features
ngoldbaum d69df5e
apply Bruno's wording suggestions
ngoldbaum d543ed8
simplify compile-time checking for PyClass being Sync
ngoldbaum ba09a08
update discussion around the decorator example in the guide
ngoldbaum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
* The `pyclass` macro now creates a rust type that is `Sync` by default. If you | ||
would like to opt out of this, annotate your class with | ||
`pyclass(unsendable)`. See the migraiton guide entry (INSERT GUIDE LINK HERE) | ||
for more information on updating to accommadate this change. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/// Helper function that can be used at compile time to emit a diagnostic if | ||
/// the type does not implement `Sync` when it should. | ||
/// | ||
/// The mere act of invoking this function will cause the diagnostic to be | ||
/// emitted if `T` does not implement `Sync` when it should. | ||
/// | ||
/// The additional `const IS_SYNC: bool` parameter is used to allow the custom | ||
/// diagnostic to be emitted; if `PyClassSync` | ||
#[allow(unused)] | ||
pub const fn assert_pyclass_sync<T>() | ||
where | ||
T: PyClassSync + Sync, | ||
{ | ||
} | ||
|
||
#[cfg_attr( | ||
diagnostic_namespace, | ||
diagnostic::on_unimplemented( | ||
message = "the trait `Sync` is not implemented for `{Self}`", | ||
label = "required by `#[pyclass]`", | ||
note = "replace thread-unsafe fields with thread-safe alternatives", | ||
note = "see <TODO INSERT PYO3 GUIDE> for more information", | ||
) | ||
)] | ||
pub trait PyClassSync<T: Sync = Self> {} | ||
|
||
impl<T> PyClassSync for T where T: Sync {} | ||
|
||
mod tests { | ||
#[cfg(feature = "macros")] | ||
#[test] | ||
fn test_assert_pyclass_sync() { | ||
use super::assert_pyclass_sync; | ||
|
||
#[crate::pyclass(crate = "crate")] | ||
struct MyClass {} | ||
|
||
assert_pyclass_sync::<MyClass>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::marker::PhantomData; | ||
|
||
use crate::{conversion::IntoPyObject, Py}; | ||
#[allow(deprecated)] | ||
use crate::{IntoPy, ToPyObject}; | ||
|
||
/// Trait used to combine with zero-sized types to calculate at compile time | ||
/// some property of a type. | ||
/// | ||
/// The trick uses the fact that an associated constant has higher priority | ||
/// than a trait constant, so we can use the trait to define the false case. | ||
/// | ||
/// The true case is defined in the zero-sized type's impl block, which is | ||
/// gated on some property like trait bound or only being implemented | ||
/// for fixed concrete types. | ||
pub trait Probe { | ||
const VALUE: bool = false; | ||
} | ||
|
||
macro_rules! probe { | ||
($name:ident) => { | ||
pub struct $name<T>(PhantomData<T>); | ||
impl<T> Probe for $name<T> {} | ||
}; | ||
} | ||
|
||
probe!(IsPyT); | ||
|
||
impl<T> IsPyT<Py<T>> { | ||
pub const VALUE: bool = true; | ||
} | ||
|
||
probe!(IsToPyObject); | ||
|
||
#[allow(deprecated)] | ||
impl<T: ToPyObject> IsToPyObject<T> { | ||
pub const VALUE: bool = true; | ||
} | ||
|
||
probe!(IsIntoPy); | ||
|
||
#[allow(deprecated)] | ||
impl<T: IntoPy<crate::PyObject>> IsIntoPy<T> { | ||
pub const VALUE: bool = true; | ||
} | ||
|
||
probe!(IsIntoPyObjectRef); | ||
|
||
// Possible clippy beta regression, | ||
// see https://github.com/rust-lang/rust-clippy/issues/13578 | ||
#[allow(clippy::extra_unused_lifetimes)] | ||
impl<'a, 'py, T: 'a> IsIntoPyObjectRef<T> | ||
where | ||
&'a T: IntoPyObject<'py>, | ||
{ | ||
pub const VALUE: bool = true; | ||
} | ||
|
||
probe!(IsIntoPyObject); | ||
|
||
impl<'py, T> IsIntoPyObject<T> | ||
where | ||
T: IntoPyObject<'py>, | ||
{ | ||
pub const VALUE: bool = true; | ||
} | ||
|
||
probe!(IsSync); | ||
|
||
impl<T: Sync> IsSync<T> { | ||
pub const VALUE: bool = true; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this the same logic that Exclusive uses? If so, I would prefer that we use that (or our own version of it) rather than this impl.
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.
I marked this ready for review, but I think this hasn't been looked at yet. I'll leave that to David since I'm not sure if using
Exclusive
makes sense.