1 | use std::marker::PhantomData; |
2 | |
3 | use crate::{conversion::IntoPyObject, Py}; |
4 | #[allow (deprecated)] |
5 | use 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. |
16 | pub trait Probe { |
17 | const VALUE: bool = false; |
18 | } |
19 | |
20 | macro_rules! probe { |
21 | ($name:ident) => { |
22 | pub struct $name<T>(PhantomData<T>); |
23 | impl<T> Probe for $name<T> {} |
24 | }; |
25 | } |
26 | |
27 | probe!(IsPyT); |
28 | |
29 | impl<T> IsPyT<Py<T>> { |
30 | pub const VALUE: bool = true; |
31 | } |
32 | |
33 | probe!(IsToPyObject); |
34 | |
35 | #[allow (deprecated)] |
36 | impl<T: ToPyObject> IsToPyObject<T> { |
37 | pub const VALUE: bool = true; |
38 | } |
39 | |
40 | probe!(IsIntoPy); |
41 | |
42 | #[allow (deprecated)] |
43 | impl<T: IntoPy<crate::PyObject>> IsIntoPy<T> { |
44 | pub const VALUE: bool = true; |
45 | } |
46 | |
47 | probe!(IsIntoPyObjectRef); |
48 | |
49 | // Possible clippy beta regression, |
50 | // see https://github.com/rust-lang/rust-clippy/issues/13578 |
51 | #[allow (clippy::extra_unused_lifetimes)] |
52 | impl<'a, 'py, T: 'a> IsIntoPyObjectRef<T> |
53 | where |
54 | &'a T: IntoPyObject<'py>, |
55 | { |
56 | pub const VALUE: bool = true; |
57 | } |
58 | |
59 | probe!(IsIntoPyObject); |
60 | |
61 | impl<'py, T> IsIntoPyObject<T> |
62 | where |
63 | T: IntoPyObject<'py>, |
64 | { |
65 | pub const VALUE: bool = true; |
66 | } |
67 | |
68 | probe!(IsSync); |
69 | |
70 | impl<T: Sync> IsSync<T> { |
71 | pub const VALUE: bool = true; |
72 | } |
73 | |
74 | probe!(IsOption); |
75 | |
76 | impl<T> IsOption<Option<T>> { |
77 | pub const VALUE: bool = true; |
78 | } |
79 | |