pyo3/impl_/pyclass/
probes.rs

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