From 8558d41cd8d589a881a9b85a582831e7a5f66768 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 18 Dec 2020 10:09:35 +0800 Subject: [PATCH] pyo3 demo --- Cargo.lock | 142 +++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + nac3embedded/Cargo.toml | 13 ++++ nac3embedded/nac3embedded.so | 1 + nac3embedded/src/lib.rs | 14 ++++ 5 files changed, 171 insertions(+) create mode 100644 nac3embedded/Cargo.toml create mode 120000 nac3embedded/nac3embedded.so create mode 100644 nac3embedded/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index f24e0a8a..19236dd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -147,6 +147,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "ctor" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fbaabec2c953050352311293be5c6aba8e141ba19d6811862b232d6fd020484" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "diff" version = "0.1.12" @@ -232,6 +242,17 @@ dependencies = [ "wasi", ] +[[package]] +name = "ghost" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a5bcf1bbeab73aa4cf2fde60a846858dc036163c7c33bec309f8d17de785479" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "hashbrown" version = "0.9.1" @@ -257,6 +278,29 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "indoc" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47741a8bc60fb26eb8d6e0238bbb26d8575ff623fdc97b1a2c00c050b9684ed8" +dependencies = [ + "indoc-impl", + "proc-macro-hack", +] + +[[package]] +name = "indoc-impl" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce046d161f000fffde5f432a0d034d0341dc152643b2598ed5bfce44c4f3a8f0" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", + "unindent", +] + [[package]] name = "inkwell" version = "0.1.0" @@ -290,6 +334,28 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "inventory" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f0f7efb804ec95e33db9ad49e4252f049e37e8b0a4652e3cd61f7999f2eff7f" +dependencies = [ + "ctor", + "ghost", + "inventory-impl", +] + +[[package]] +name = "inventory-impl" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75c094e94816723ab936484666968f5b58060492e880f3c8d00489a1e244fa51" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "itertools" version = "0.9.0" @@ -389,6 +455,13 @@ version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +[[package]] +name = "nac3embedded" +version = "0.1.0" +dependencies = [ + "pyo3", +] + [[package]] name = "nac3standalone" version = "0.1.0" @@ -471,6 +544,25 @@ dependencies = [ "winapi", ] +[[package]] +name = "paste" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" +dependencies = [ + "paste-impl", + "proc-macro-hack", +] + +[[package]] +name = "paste-impl" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" +dependencies = [ + "proc-macro-hack", +] + [[package]] name = "petgraph" version = "0.5.1" @@ -496,6 +588,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + [[package]] name = "proc-macro2" version = "1.0.24" @@ -505,6 +603,44 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "pyo3" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf6bbbe8f70d179260b3728e5d04eb012f4f0c7988e58c11433dd689cecaa72e" +dependencies = [ + "ctor", + "indoc", + "inventory", + "libc", + "parking_lot", + "paste", + "pyo3cls", + "unindent", +] + +[[package]] +name = "pyo3-derive-backend" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10ecd0eb6ed7b3d9965b4f4370b5b9e99e3e5e8742000e1c452c018f8c2a322f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pyo3cls" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d344fdaa6a834a06dd1720ff104ea12fe101dad2e8db89345af9db74c0bb11a0" +dependencies = [ + "pyo3-derive-backend", + "quote", + "syn", +] + [[package]] name = "quote" version = "1.0.7" @@ -770,6 +906,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87d6678d7916394abad0d4b19df4d3802e1fd84abd7d701f39b75ee71b9e8cf1" +[[package]] +name = "unindent" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f14ee04d9415b52b3aeab06258a3f07093182b88ba0f9b8d203f211a7a7d41c7" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 8e5c931d..5e2d91d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,5 @@ members = [ "libnac3", "nac3standalone", + "nac3embedded", ] diff --git a/nac3embedded/Cargo.toml b/nac3embedded/Cargo.toml new file mode 100644 index 00000000..230d0d9e --- /dev/null +++ b/nac3embedded/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "nac3embedded" +version = "0.1.0" +authors = ["M-Labs"] +edition = "2018" + +[lib] +name = "nac3embedded" +crate-type = ["cdylib"] + +[dependencies.pyo3] +version = "0.12.4" +features = ["extension-module"] diff --git a/nac3embedded/nac3embedded.so b/nac3embedded/nac3embedded.so new file mode 120000 index 00000000..9459220f --- /dev/null +++ b/nac3embedded/nac3embedded.so @@ -0,0 +1 @@ +../target/release/libnac3embedded.so \ No newline at end of file diff --git a/nac3embedded/src/lib.rs b/nac3embedded/src/lib.rs new file mode 100644 index 00000000..c0e864d8 --- /dev/null +++ b/nac3embedded/src/lib.rs @@ -0,0 +1,14 @@ +use pyo3::prelude::*; +use pyo3::wrap_pyfunction; + +#[pyfunction] +fn sum_as_string(a: usize, b: usize) -> PyResult { + Ok((a + b).to_string()) +} + +#[pymodule] +fn nac3embedded(py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + + Ok(()) +}