2015-06-25 21:58:57 +00:00
|
|
|
#![crate_type = "dylib"]
|
2016-03-05 00:16:54 +00:00
|
|
|
#![feature(const_fn)]
|
2015-06-25 21:58:57 +00:00
|
|
|
|
|
|
|
#[macro_use] extern crate cpython;
|
|
|
|
|
2016-03-05 14:57:48 +00:00
|
|
|
use cpython::{Python, PyObject, PyRustObject, PyRustType, PyResult, GILProtected};
|
2016-03-05 00:16:54 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
|
|
|
|
static MY_TYPE: GILProtected<RefCell<Option<PyRustType<i32>>>> = GILProtected::new(RefCell::new(None));
|
2015-06-25 21:58:57 +00:00
|
|
|
|
2016-03-05 22:20:53 +00:00
|
|
|
py_module_initializer!(custom_type, initcustom_type, PyInit_custom_type, |py, m| {
|
2015-10-26 22:52:18 +00:00
|
|
|
try!(m.add(py, "__doc__", "Module documentation string"));
|
2016-03-05 00:16:54 +00:00
|
|
|
*MY_TYPE.get(py).borrow_mut() = Some(try!(m.add_type::<i32>(py, "MyType")
|
2015-08-02 22:06:15 +00:00
|
|
|
.add("a", py_method!(a()))
|
2016-03-05 14:16:11 +00:00
|
|
|
.set_new(py_fn!(new(arg: i32)))
|
2016-03-05 00:16:54 +00:00
|
|
|
.finish()));
|
2015-06-25 21:58:57 +00:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
2016-03-05 14:16:11 +00:00
|
|
|
fn new(py: Python, arg: i32) -> PyResult<PyRustObject<i32>> {
|
2016-03-05 00:16:54 +00:00
|
|
|
Ok(MY_TYPE.get(py).borrow().as_ref().unwrap().create_instance(py, arg, ()))
|
|
|
|
}
|
|
|
|
|
2015-10-25 16:55:29 +00:00
|
|
|
fn a(py: Python, slf: &PyRustObject<i32>) -> PyResult<PyObject> {
|
|
|
|
println!("a() was called with self={:?}", slf.get(py));
|
|
|
|
Ok(py.None())
|
2015-06-25 21:58:57 +00:00
|
|
|
}
|
|
|
|
|