2015-04-18 18:17:25 +00:00
|
|
|
#![crate_type = "dylib"]
|
2015-05-24 18:06:08 +00:00
|
|
|
#![feature(plugin)]
|
|
|
|
#![plugin(interpolate_idents)]
|
2015-04-18 18:17:25 +00:00
|
|
|
|
|
|
|
#[macro_use] extern crate cpython;
|
|
|
|
|
2015-08-02 22:06:15 +00:00
|
|
|
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};
|
2015-04-18 18:17:25 +00:00
|
|
|
|
2015-06-24 22:02:56 +00:00
|
|
|
py_module_initializer!(hello, |_py, m| {
|
2015-04-18 20:20:19 +00:00
|
|
|
try!(m.add("__doc__", "Module documentation string"));
|
2015-06-24 22:02:56 +00:00
|
|
|
try!(m.add("run", py_fn!(run)));
|
2015-08-02 22:06:15 +00:00
|
|
|
try!(m.add("val", py_fn!(val())));
|
2015-04-18 18:17:25 +00:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
2015-08-02 22:06:15 +00:00
|
|
|
fn run<'p>(py: Python<'p>, args: &PyTuple<'p>, kwargs: Option<&PyDict<'p>>) -> PyResult<'p, PyObject<'p>> {
|
2015-04-18 18:17:25 +00:00
|
|
|
println!("Rust says: Hello Python!");
|
|
|
|
for arg in args {
|
|
|
|
println!("Rust got {}", arg);
|
|
|
|
}
|
2015-08-02 22:06:15 +00:00
|
|
|
if let Some(kwargs) = kwargs {
|
|
|
|
for (key, val) in kwargs.items() {
|
|
|
|
println!("{} = {}", key, val);
|
|
|
|
}
|
|
|
|
}
|
2015-04-18 18:17:25 +00:00
|
|
|
Ok(py.None())
|
|
|
|
}
|
|
|
|
|
2015-08-02 22:06:15 +00:00
|
|
|
fn val<'p>(_: Python<'p>) -> PyResult<'p, i32> {
|
2015-04-18 18:17:25 +00:00
|
|
|
Ok(42)
|
|
|
|
}
|
|
|
|
|