pyo3/extensions/hello.rs

33 lines
789 B
Rust
Raw Normal View History

#![crate_type = "dylib"]
2015-05-24 18:06:08 +00:00
#![feature(plugin)]
#![plugin(interpolate_idents)]
#[macro_use] extern crate cpython;
use cpython::{PyObject, PyResult, PyModule, Python, PyTuple};
2015-05-24 18:06:08 +00:00
py_module_initializer!(hello, |py, m| {
2015-04-18 20:20:19 +00:00
try!(m.add("__doc__", "Module documentation string"));
try!(m.add("run", py_func!(py, run)));
try!(add_val(py, &m));
Ok(())
});
fn run<'p>(py: Python<'p>, args: &PyTuple<'p>) -> PyResult<'p, PyObject<'p>> {
println!("Rust says: Hello Python!");
for arg in args {
println!("Rust got {}", arg);
}
Ok(py.None())
}
2015-04-18 20:20:19 +00:00
fn val<'p>(_: Python<'p>, _: &PyTuple<'p>) -> PyResult<'p, i32> {
Ok(42)
}
// Workaround for Rust #24561
fn add_val<'p>(py: Python<'p>, m: &PyModule<'p>) -> PyResult<'p, ()> {
2015-04-18 20:20:19 +00:00
m.add("val", py_func!(py, val))
}