-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* require `#[pyclass]` to be `Sync` * silence deprecation warning in pyo3 internals * make 'cargo test' pass (modulo test_getter_setter) * fix nox -s test-py * silence new deprecation warning * use `unsendable` to test cell get / set * add WIP changelog entry * fix wasm clippy * add a test for assert_pyclass_sync to fix coverage error * use a better name * fix cargo doc --lib * fix building with no default features * apply Bruno's wording suggestions * simplify compile-time checking for PyClass being Sync * update discussion around the decorator example in the guide --------- Co-authored-by: Nathan Goldbaum <[email protected]>
- Loading branch information
1 parent
76f4503
commit 1befe1d
Showing
15 changed files
with
301 additions
and
131 deletions.
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.