Created
September 22, 2020 22:35
-
-
Save derrickturk/950a0a77cdeffa910ec3084913733d7c to your computer and use it in GitHub Desktop.
A minimal Python module in Rust (move lib.rs to src/lib.rs); rename target/{profile}/rustpy.dll to rustpy.pyd.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rustpy" | |
version = "0.1.0" | |
authors = ["Derrick W. Turk <dwt@terminusdatascience.com>"] | |
edition = "2018" | |
[lib] | |
crate-type = ["cdylib"] | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
python3-sys = "0.5.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use python3_sys::{ | |
PyArg_ParseTuple, | |
PyMethodDef, | |
PyModule_Create, | |
PyModuleDef, | |
PyModuleDef_HEAD_INIT, | |
PyNumber_Add, | |
PyObject, | |
METH_VARARGS, | |
}; | |
use std::{ | |
ptr, | |
os::raw::c_char, | |
}; | |
macro_rules! cstr { | |
($s:expr) => ( | |
concat!($s, "\0") as *const str as *const [c_char] as *const c_char | |
); | |
} | |
unsafe extern fn add(_module: *mut PyObject, args: *mut PyObject | |
) -> *mut PyObject { | |
let mut lhs: *mut PyObject = ptr::null_mut(); | |
let mut rhs: *mut PyObject = ptr::null_mut(); | |
if PyArg_ParseTuple(args, cstr!("OO"), | |
&mut lhs as *mut _, &mut rhs as *mut _) == 0 { | |
ptr::null_mut() | |
} else { | |
PyNumber_Add(lhs, rhs) | |
} | |
} | |
static mut RUSTPY_METHODS: [PyMethodDef; 2] = [ | |
PyMethodDef { | |
ml_name: cstr!("add"), | |
ml_meth: Some(add), | |
ml_flags: METH_VARARGS, | |
ml_doc: cstr!("adds some stuff"), | |
}, | |
PyMethodDef { | |
ml_name: ptr::null(), | |
ml_meth: None, | |
ml_flags: 0, | |
ml_doc: ptr::null(), | |
}, | |
]; | |
static mut RUSTPY: PyModuleDef = PyModuleDef { | |
m_base: PyModuleDef_HEAD_INIT, | |
m_name: cstr!("rustpy"), | |
m_doc: cstr!("it's Rust, but Py"), | |
m_size: -1, | |
m_methods: unsafe { &mut RUSTPY_METHODS } as *const _ as *mut _, | |
m_slots: ptr::null_mut(), | |
m_traverse: None, | |
m_clear: None, | |
m_free: None, | |
}; | |
#[no_mangle] | |
unsafe extern fn PyInit_rustpy() -> *mut PyObject { | |
PyModule_Create(&mut RUSTPY as *mut _) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment