|
1 |
| -use std::collections::HashMap; |
2 |
| -use std::marker::PhantomData; |
3 |
| -use std::mem; |
4 |
| -use std::ops::RangeInclusive; |
5 |
| - |
6 |
| -use indexmap::IndexMap; |
7 |
| -use result_like::impl_option_like; |
8 |
| - |
| 1 | +use self::OptionalArg::*; |
9 | 2 | use crate::exceptions::PyBaseExceptionRef;
|
10 | 3 | use crate::obj::objtuple::PyTupleRef;
|
11 | 4 | use crate::obj::objtype::{isinstance, PyTypeRef};
|
12 | 5 | use crate::pyobject::{
|
13 |
| - BorrowValue, IntoPyResult, PyObjectRef, PyRef, PyResult, PyThreadingConstraint, PyValue, |
14 |
| - TryFromObject, TypeProtocol, |
| 6 | + BorrowValue, IntoPyObject, IntoPyResult, PyObjectRef, PyRef, PyResult, PyThreadingConstraint, |
| 7 | + PyValue, TryFromObject, TypeProtocol, |
15 | 8 | };
|
16 | 9 | use crate::vm::VirtualMachine;
|
| 10 | +use indexmap::IndexMap; |
| 11 | +use result_like::impl_option_like; |
| 12 | +use std::collections::HashMap; |
| 13 | +use std::marker::PhantomData; |
| 14 | +use std::ops::RangeInclusive; |
17 | 15 |
|
18 |
| -use self::OptionalArg::*; |
| 16 | +pub trait IntoFuncArgs { |
| 17 | + fn into_args(self, vm: &VirtualMachine) -> PyFuncArgs; |
| 18 | +} |
| 19 | + |
| 20 | +impl<T> IntoFuncArgs for T |
| 21 | +where |
| 22 | + T: Into<PyFuncArgs>, |
| 23 | +{ |
| 24 | + fn into_args(self, _vm: &VirtualMachine) -> PyFuncArgs { |
| 25 | + self.into() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// A tuple of values that each implement `IntoPyObject` represents a sequence of |
| 30 | +// arguments that can be bound and passed to a built-in function. |
| 31 | +macro_rules! into_func_args_from_tuple { |
| 32 | + ($(($n:tt, $T:ident)),*) => { |
| 33 | + impl<$($T,)*> IntoFuncArgs for ($($T,)*) |
| 34 | + where |
| 35 | + $($T: IntoPyObject,)* |
| 36 | + { |
| 37 | + fn into_args(self, vm: &VirtualMachine) -> PyFuncArgs { |
| 38 | + let ($($n,)*) = self; |
| 39 | + vec![$($n.into_pyobject(vm),)*].into() |
| 40 | + } |
| 41 | + } |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +into_func_args_from_tuple!((v1, T1)); |
| 46 | +into_func_args_from_tuple!((v1, T1), (v2, T2)); |
| 47 | +into_func_args_from_tuple!((v1, T1), (v2, T2), (v3, T3)); |
| 48 | +into_func_args_from_tuple!((v1, T1), (v2, T2), (v3, T3), (v4, T4)); |
| 49 | +into_func_args_from_tuple!((v1, T1), (v2, T2), (v3, T3), (v4, T4), (v5, T5)); |
19 | 50 |
|
20 | 51 | /// The `PyFuncArgs` struct is one of the most used structs then creating
|
21 | 52 | /// a rust function that can be called from python. It holds both positional
|
@@ -72,7 +103,7 @@ impl From<KwArgs> for PyFuncArgs {
|
72 | 103 |
|
73 | 104 | impl FromArgs for PyFuncArgs {
|
74 | 105 | fn from_args(_vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError> {
|
75 |
| - Ok(mem::take(args)) |
| 106 | + Ok(std::mem::take(args)) |
76 | 107 | }
|
77 | 108 | }
|
78 | 109 |
|
@@ -343,12 +374,19 @@ impl<T> Args<T> {
|
343 | 374 | self.0
|
344 | 375 | }
|
345 | 376 | }
|
| 377 | + |
346 | 378 | impl<T> From<Vec<T>> for Args<T> {
|
347 | 379 | fn from(v: Vec<T>) -> Self {
|
348 | 380 | Args(v)
|
349 | 381 | }
|
350 | 382 | }
|
351 | 383 |
|
| 384 | +impl From<()> for Args<PyObjectRef> { |
| 385 | + fn from(_args: ()) -> Self { |
| 386 | + Args(Vec::new()) |
| 387 | + } |
| 388 | +} |
| 389 | + |
352 | 390 | impl<T> AsRef<[T]> for Args<T> {
|
353 | 391 | fn as_ref(&self) -> &[T] {
|
354 | 392 | &self.0
|
|
0 commit comments