From 54db20a4fe57a442cd65384cae35865988185572 Mon Sep 17 00:00:00 2001 From: arcnmx Date: Mon, 31 Jul 2017 14:37:34 -0400 Subject: [PATCH] Expand macro for multiple functions --- src/lib.rs | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1c39afb..c077a85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -287,15 +287,34 @@ pub fn handler(py: Python, f: F, py_event: PyObject, py_context: PyObject) -> /// # } /// ``` macro_rules! lambda { - ($f:expr) => { - py_module_initializer!(liblambda, initliblambda, PyInit_liblambda, |py, m| { - try!(m.add(py, "handler", - py_fn!(py, x(event: $crate::PyObject, - context: $crate::PyObject) - -> $crate::PyResult<$crate::PyObject> { - $crate::handler(py, $f, event, context) - }))); + (@module ($module:ident, $py2:ident, $py3:ident) @handlers ($($handler:expr => $target:expr),*)) => { + py_module_initializer!($module, $py2, $py3, |py, m| { + $( + m.add(py, $handler, py_fn!(py, x(event: $crate::PyObject, context: $crate::PyObject) -> $crate::PyResult<$crate::PyObject> { + $crate::handler(py, $target, event, context) + }))?; + )* Ok(()) }); - } + }; + + (crate $module:tt { $($handler:expr => $target:expr),* }) => { + lambda! { @module $module @handlers ($($handler => $target),*) } + }; + + (crate $module:tt { $($handler:expr => $target:expr,)* }) => { + lambda! { @module $module @handlers ($($handler => $target),*) } + }; + + ($($handler:expr => $target:expr),*) => { + lambda! { @module (liblambda, initliblambda, PyInit_liblambda) @handlers ($($handler => $target),*) } + }; + + ($($handler:expr => $target:expr,)*) => { + lambda! { $($handler => $target),* } + }; + + ($f:expr) => { + lambda! { "handler" => $f, } + }; }