@@ -20,12 +20,12 @@ type PyArg<'py> = Borrowed<'py, 'py, PyAny>;
20
20
/// will be dropped as soon as the pyfunction call ends.
21
21
///
22
22
/// There exists a trivial blanket implementation for `T: FromPyObject` with `Holder = ()`.
23
- pub trait PyFunctionArgument < ' a , ' py > : Sized + ' a {
23
+ pub trait PyFunctionArgument < ' a , ' py , const IS_OPTION : bool > : Sized + ' a {
24
24
type Holder : FunctionArgumentHolder ;
25
25
fn extract ( obj : & ' a Bound < ' py , PyAny > , holder : & ' a mut Self :: Holder ) -> PyResult < Self > ;
26
26
}
27
27
28
- impl < ' a , ' py , T > PyFunctionArgument < ' a , ' py > for T
28
+ impl < ' a , ' py , T > PyFunctionArgument < ' a , ' py , false > for T
29
29
where
30
30
T : FromPyObjectBound < ' a , ' py > + ' a ,
31
31
{
37
37
}
38
38
}
39
39
40
- impl < ' a , ' py , T : ' py > PyFunctionArgument < ' a , ' py > for & ' a Bound < ' py , T >
40
+ impl < ' a , ' py , T : ' py > PyFunctionArgument < ' a , ' py , false > for & ' a Bound < ' py , T >
41
41
where
42
42
T : PyTypeCheck ,
43
43
{
@@ -49,24 +49,24 @@ where
49
49
}
50
50
}
51
51
52
- impl < ' a , ' py , T : ' py > PyFunctionArgument < ' a , ' py > for Option < & ' a Bound < ' py , T > >
52
+ impl < ' a , ' py , T > PyFunctionArgument < ' a , ' py , true > for Option < T >
53
53
where
54
- T : PyTypeCheck ,
54
+ T : PyFunctionArgument < ' a , ' py , false > , // inner `Option`s will use `FromPyObject`
55
55
{
56
- type Holder = ( ) ;
56
+ type Holder = T :: Holder ;
57
57
58
58
#[ inline]
59
- fn extract ( obj : & ' a Bound < ' py , PyAny > , _ : & ' a mut ( ) ) -> PyResult < Self > {
59
+ fn extract ( obj : & ' a Bound < ' py , PyAny > , holder : & ' a mut T :: Holder ) -> PyResult < Self > {
60
60
if obj. is_none ( ) {
61
61
Ok ( None )
62
62
} else {
63
- Ok ( Some ( obj . downcast ( ) ?) )
63
+ Ok ( Some ( T :: extract ( obj , holder ) ?) )
64
64
}
65
65
}
66
66
}
67
67
68
68
#[ cfg( all( Py_LIMITED_API , not( Py_3_10 ) ) ) ]
69
- impl < ' a > PyFunctionArgument < ' a , ' _ > for & ' a str {
69
+ impl < ' a > PyFunctionArgument < ' a , ' _ , false > for & ' a str {
70
70
type Holder = Option < std:: borrow:: Cow < ' a , str > > ;
71
71
72
72
#[ inline]
@@ -110,13 +110,13 @@ pub fn extract_pyclass_ref_mut<'a, 'py: 'a, T: PyClass<Frozen = False>>(
110
110
111
111
/// The standard implementation of how PyO3 extracts a `#[pyfunction]` or `#[pymethod]` function argument.
112
112
#[ doc( hidden) ]
113
- pub fn extract_argument < ' a , ' py , T > (
113
+ pub fn extract_argument < ' a , ' py , T , const IS_OPTION : bool > (
114
114
obj : & ' a Bound < ' py , PyAny > ,
115
115
holder : & ' a mut T :: Holder ,
116
116
arg_name : & str ,
117
117
) -> PyResult < T >
118
118
where
119
- T : PyFunctionArgument < ' a , ' py > ,
119
+ T : PyFunctionArgument < ' a , ' py , IS_OPTION > ,
120
120
{
121
121
match PyFunctionArgument :: extract ( obj, holder) {
122
122
Ok ( value) => Ok ( value) ,
@@ -127,14 +127,14 @@ where
127
127
/// Alternative to [`extract_argument`] used for `Option<T>` arguments. This is necessary because Option<&T>
128
128
/// does not implement `PyFunctionArgument` for `T: PyClass`.
129
129
#[ doc( hidden) ]
130
- pub fn extract_optional_argument < ' a , ' py , T > (
130
+ pub fn extract_optional_argument < ' a , ' py , T , const IS_OPTION : bool > (
131
131
obj : Option < & ' a Bound < ' py , PyAny > > ,
132
132
holder : & ' a mut T :: Holder ,
133
133
arg_name : & str ,
134
134
default : fn ( ) -> Option < T > ,
135
135
) -> PyResult < Option < T > >
136
136
where
137
- T : PyFunctionArgument < ' a , ' py > ,
137
+ T : PyFunctionArgument < ' a , ' py , IS_OPTION > ,
138
138
{
139
139
match obj {
140
140
Some ( obj) => {
@@ -151,14 +151,14 @@ where
151
151
152
152
/// Alternative to [`extract_argument`] used when the argument has a default value provided by an annotation.
153
153
#[ doc( hidden) ]
154
- pub fn extract_argument_with_default < ' a , ' py , T > (
154
+ pub fn extract_argument_with_default < ' a , ' py , T , const IS_OPTION : bool > (
155
155
obj : Option < & ' a Bound < ' py , PyAny > > ,
156
156
holder : & ' a mut T :: Holder ,
157
157
arg_name : & str ,
158
158
default : fn ( ) -> T ,
159
159
) -> PyResult < T >
160
160
where
161
- T : PyFunctionArgument < ' a , ' py > ,
161
+ T : PyFunctionArgument < ' a , ' py , IS_OPTION > ,
162
162
{
163
163
match obj {
164
164
Some ( obj) => extract_argument ( obj, holder, arg_name) ,
0 commit comments