From a650193eec19cf3a80a2d6af01ab4a40735a0423 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Wed, 6 Feb 2019 08:57:13 -0500 Subject: [PATCH] Wire up to run function as Python code. --- Cargo.toml | 5 ++++ Pipfile | 13 ++++++++ Pipfile.lock | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 8 ++++- src/lib.rs | 15 ++++++++++ test.py | 2 ++ 6 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 test.py diff --git a/Cargo.toml b/Cargo.toml index bb856d4..804dccb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,9 @@ version = "0.1.0" authors = ["Dorian Pula "] edition = "2018" +[lib] +name = "unit_converter" +crate-type = ["cdylib"] + [dependencies] +pyo3 = { version = "0.6.0-alpha.2", features = ["extension-module"] } diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..7ca8a99 --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +pyo3-pack = "*" +pytest = "*" + +[requires] +python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..9a5d283 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,83 @@ +{ + "_meta": { + "hash": { + "sha256": "0630251fe5bf105722ab858c5ac0549f000628fbbfa4255c36a7fcfef9ed3559" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.6" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "atomicwrites": { + "hashes": [ + "sha256:03472c30eb2c5d1ba9227e4c2ca66ab8287fbfbbda3888aa93dc2e28fc6811b4", + "sha256:75a9445bac02d8d058d5e1fe689654ba5a6556a1dfd8ce6ec55a0ed79866cfa6" + ], + "version": "==1.3.0" + }, + "attrs": { + "hashes": [ + "sha256:10cbf6e27dbce8c30807caf056c8eb50917e0eaafe86347671b57254006c3e69", + "sha256:ca4be454458f9dec299268d472aaa5a11f67a4ff70093396e1ceae9c76cf4bbb" + ], + "version": "==18.2.0" + }, + "more-itertools": { + "hashes": [ + "sha256:38a936c0a6d98a38bcc2d03fdaaedaba9f412879461dd2ceff8d37564d6522e4", + "sha256:c0a5785b1109a6bd7fac76d6837fd1feca158e54e521ccd2ae8bfe393cc9d4fc", + "sha256:fe7a7cae1ccb57d33952113ff4fa1bc5f879963600ed74918f1236e212ee50b9" + ], + "version": "==5.0.0" + }, + "pluggy": { + "hashes": [ + "sha256:8ddc32f03971bfdf900a81961a48ccf2fb677cf7715108f85295c67405798616", + "sha256:980710797ff6a041e9a73a5787804f848996ecaa6f8a1b1e08224a5894f2074a" + ], + "version": "==0.8.1" + }, + "py": { + "hashes": [ + "sha256:bf92637198836372b520efcba9e020c330123be8ce527e535d185ed4b6f45694", + "sha256:e76826342cefe3c3d5f7e8ee4316b80d1dd8a300781612ddbc765c17ba25a6c6" + ], + "version": "==1.7.0" + }, + "pyo3-pack": { + "hashes": [ + "sha256:19250198b465b2b872ec3ef362b925ae8af1963119783b1d38f7d61a4c56faf8", + "sha256:623a1f8159a26385d24ec63653ed21bf73d49f1ecdbd141314e799b905e35f49", + "sha256:756987d0de960d33ac23e409134821d142cbba4cae6ffd6ec4fb63a67cef6305", + "sha256:768d883a36dad98dbac6f7b44730b33686f3b0f71d4ebf2b6beb3884fbb70015", + "sha256:c14d82499896804b49c4d7175402db7e0dff165575d9797370ae75cf7221b62a" + ], + "index": "pypi", + "version": "==0.4.2" + }, + "pytest": { + "hashes": [ + "sha256:65aeaa77ae87c7fc95de56285282546cfa9c886dc8e5dc78313db1c25e21bc07", + "sha256:6ac6d467d9f053e95aaacd79f831dbecfe730f419c6c7022cb316b365cd9199d" + ], + "index": "pypi", + "version": "==4.2.0" + }, + "six": { + "hashes": [ + "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c", + "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73" + ], + "version": "==1.12.0" + } + }, + "develop": {} +} diff --git a/README.md b/README.md index a61e9f0..72c0213 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,15 @@ An example of a Rust library that is embeddable in Python and Web Assembly. Insure the following dependencies are in place: -* Rust v1.32+ +* Rust v1.32+ (nightly) + * Install using `rustup toolchain install nightly` + * Set default toolchain `rustup default nightly` * Python 3 Development headers (python3-dev in Ubuntu) +## Running the Python example + +1. Build the rust crate first: `cargo build` + ## Documentation * [PyO3 Python - Rust bindings](https://pyo3.rs/master/get_started.html) diff --git a/src/lib.rs b/src/lib.rs index 44b7f27..3dfee57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,22 @@ +#![feature(specialization)] + +#[macro_use] extern crate pyo3; + +use pyo3::prelude::*; + + +#[pyfunction] fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 { celsius * 1.8 + 32.0 } +#[pymodule] +fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_wrapped(wrap_pyfunction!(convert_celsius_to_fahrenheit))?; + + Ok(()) +} + #[cfg(test)] mod tests { diff --git a/test.py b/test.py new file mode 100644 index 0000000..793b05c --- /dev/null +++ b/test.py @@ -0,0 +1,2 @@ +import unit_converter +