Skip to content

Commit 6072954

Browse files
authored
Merge pull request RustPython#647 from RustPython/joey/no-arg-functions
Allow functions that don't take any args
2 parents 010969f + cd3ca16 commit 6072954

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

vm/src/pyobject.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,20 @@ where
13791379
}
13801380
}
13811381

1382+
// For functions that accept no arguments. Implemented explicitly instead of via
1383+
// macro below to avoid unused warnings.
1384+
impl FromArgs for () {
1385+
fn from_args<I>(
1386+
_vm: &mut VirtualMachine,
1387+
_args: &mut iter::Peekable<I>,
1388+
) -> Result<Self, ArgumentError>
1389+
where
1390+
I: Iterator<Item = PyArg>,
1391+
{
1392+
Ok(())
1393+
}
1394+
}
1395+
13821396
// A tuple of types that each implement `FromArgs` represents a sequence of
13831397
// arguments that can be bound and passed to a built-in function.
13841398
//
@@ -1465,25 +1479,26 @@ impl IntoPyNativeFunc<PyFuncArgs, PyResult> for PyNativeFunc {
14651479
//
14661480
// Note that this could be done without a macro - it is simply to avoid repetition.
14671481
macro_rules! into_py_native_func_tuple {
1468-
($(($n:tt, $T:ident)),+) => {
1469-
impl<F, $($T,)+ R> IntoPyNativeFunc<($($T,)+), R> for F
1482+
($(($n:tt, $T:ident)),*) => {
1483+
impl<F, $($T,)* R> IntoPyNativeFunc<($($T,)*), R> for F
14701484
where
1471-
F: Fn($($T,)+ &mut VirtualMachine) -> R + 'static,
1472-
$($T: FromArgs,)+
1473-
($($T,)+): FromArgs,
1485+
F: Fn($($T,)* &mut VirtualMachine) -> R + 'static,
1486+
$($T: FromArgs,)*
1487+
($($T,)*): FromArgs,
14741488
R: IntoPyObject,
14751489
{
14761490
fn into_func(self) -> PyNativeFunc {
14771491
Box::new(move |vm, args| {
1478-
let ($($n,)+) = args.bind::<($($T,)+)>(vm)?;
1492+
let ($($n,)*) = args.bind::<($($T,)*)>(vm)?;
14791493

1480-
(self)($($n,)+ vm).into_pyobject(&vm.ctx)
1494+
(self)($($n,)* vm).into_pyobject(&vm.ctx)
14811495
})
14821496
}
14831497
}
14841498
};
14851499
}
14861500

1501+
into_py_native_func_tuple!();
14871502
into_py_native_func_tuple!((a, A));
14881503
into_py_native_func_tuple!((a, A), (b, B));
14891504
into_py_native_func_tuple!((a, A), (b, B), (c, C));

0 commit comments

Comments
 (0)