Skip to content

Commit 0dcc1d4

Browse files
author
Guillaume Fraux
committed
Use macro to create the register function
1 parent 42d5594 commit 0dcc1d4

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ extern crate cpython;
33
extern crate lumol;
44

55
#[macro_use]
6-
mod testing;
6+
mod macros;
77
mod systems;
88

99
py_module_initializer!(lumol, initlumol, PyInit_lumol, |py, m| {
1010
try!(m.add(py, "__doc__", "Modern and extensible molecular simulation engine"));
1111

12-
try!(systems::particle::register(py, m));
12+
try!(systems::register(py, m));
1313

1414
Ok(())
1515
});

src/testing.rs renamed to src/macros.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ macro_rules! py_run {
2424
py_run!($py, $dict, $($tail),+);
2525
});
2626
}
27+
28+
macro_rules! register {
29+
(|$py: ident, $m: ident| $closure: expr) => (
30+
pub fn register($py: ::cpython::Python, $m: &::cpython::PyModule) -> ::cpython::PyResult<()> {
31+
return $closure;
32+
}
33+
);
34+
}

src/systems/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
pub mod particle;
1+
mod particle;
2+
3+
register!(|py, m| {
4+
try!(particle::register(py, m));
5+
Ok(())
6+
});

src/systems/particle.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use cpython::{PyObject, PyResult, PyModule, Python};
1+
use cpython::{PyObject, PyResult};
22
use lumol;
33
use std::cell::RefCell;
44

5-
pub fn register(py: Python, m: &PyModule) -> PyResult<()> {
5+
register!(|py, m| {
66
try!(m.add_class::<Particle>(py));
77
Ok(())
8-
}
9-
8+
});
109

1110
py_class!(class Particle |py| {
1211
data particle: RefCell<lumol::Particle>;

0 commit comments

Comments
 (0)