pyo3/impl_/pyclass/
probes.rs

1use std::marker::PhantomData;
2
3use crate::{conversion::IntoPyObject, Py};
4#[allow(deprecated)]
5use crate::{IntoPy, ToPyObject};
6
7/// Trait used to combine with zero-sized types to calculate at compile time
8/// some property of a type.
9///
10/// The trick uses the fact that an associated constant has higher priority
11/// than a trait constant, so we can use the trait to define the false case.
12///
13/// The true case is defined in the zero-sized type's impl block, which is
14/// gated on some property like trait bound or only being implemented
15/// for fixed concrete types.
16pub trait Probe {
17    const VALUE: bool = false;
18}
19
20macro_rules! probe {
21    ($name:ident) => {
22        pub struct $name<T>(PhantomData<T>);
23        impl<T> Probe for $name<T> {}
24    };
25}
26
27probe!(IsPyT);
28
29impl<T> IsPyT<Py<T>> {
30    pub const VALUE: bool = true;
31}
32
33probe!(IsToPyObject);
34
35#[allow(deprecated)]
36impl<T: ToPyObject> IsToPyObject<T> {
37    pub const VALUE: bool = true;
38}
39
40probe!(IsIntoPy);
41
42#[allow(deprecated)]
43impl<T: IntoPy<crate::PyObject>> IsIntoPy<T> {
44    pub const VALUE: bool = true;
45}
46
47probe!(IsIntoPyObjectRef);
48
49// Possible clippy beta regression,
50// see https://github.com/rust-lang/rust-clippy/issues/13578
51#[allow(clippy::extra_unused_lifetimes)]
52impl<'a, 'py, T: 'a> IsIntoPyObjectRef<T>
53where
54    &'a T: IntoPyObject<'py>,
55{
56    pub const VALUE: bool = true;
57}
58
59probe!(IsIntoPyObject);
60
61impl<'py, T> IsIntoPyObject<T>
62where
63    T: IntoPyObject<'py>,
64{
65    pub const VALUE: bool = true;
66}
67
68probe!(IsSync);
69
70impl<T: Sync> IsSync<T> {
71    pub const VALUE: bool = true;
72}
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here