Version 0.1.0

This commit is contained in:
Daniel Grunwald 2016-12-17 21:41:14 +01:00
parent b83a5d9bad
commit b7090799b6
2 changed files with 14 additions and 8 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "cpython" name = "cpython"
version = "0.0.6" version = "0.1.0"
description = "Bindings to Python" description = "Bindings to Python"
authors = ["Daniel Grunwald <daniel@danielgrunwald.de>"] authors = ["Daniel Grunwald <daniel@danielgrunwald.de>"]
readme = "README.md" readme = "README.md"
@ -21,6 +21,7 @@ exclude = [
".cargo/config", ".cargo/config",
"python27-sys", "python27-sys",
"python3-sys", "python3-sys",
"extensions"
] ]
build = "build.rs" build = "build.rs"

View File

@ -28,7 +28,7 @@ To use `cpython`, add this to your `Cargo.toml`:
```toml ```toml
[dependencies] [dependencies]
cpython = { git = "https://github.com/dgrunwald/rust-cpython.git" } cpython = "0.1"
``` ```
Example program displaying the value of `sys.version`: Example program displaying the value of `sys.version`:
@ -58,16 +58,19 @@ fn hello(py: Python) -> PyResult<()> {
Example library with python bindings: Example library with python bindings:
The following two files will build with `cargo build`, and will generate a python-compatible library. (On macOS, you will need to rename the output from \*.dynlib to \*.so) The following two files will build with `cargo build`, and will generate a python-compatible library.
On Mac OS, you will need to rename the output from \*.dynlib to \*.so.
On Windows, you will need to rename the output from \*.dll to \*.pyd.
**`Cargo.toml`:** **`Cargo.toml`:**
```toml ```toml
[lib] [lib]
name = "rust2py" name = "rust2py"
crate-type = ["dylib"] crate-type = ["cdylib"]
[dependencies] [dependencies.cpython]
cpython = { git = "https://github.com/dgrunwald/rust-cpython.git" } path = "../.."
features = ["extension-module"]
``` ```
**`src/lib.rs`** **`src/lib.rs`**
@ -77,7 +80,7 @@ cpython = { git = "https://github.com/dgrunwald/rust-cpython.git" }
use cpython::{PyResult, Python}; use cpython::{PyResult, Python};
// add bindings to the generated python module // add bindings to the generated python module
// N.B: names: "rust2py" must be the lib name in Cargo.toml // N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
py_module_initializer!(librust2py, initlibrust2py, PyInit_librust2py, |py, m| { py_module_initializer!(librust2py, initlibrust2py, PyInit_librust2py, |py, m| {
try!(m.add(py, "__doc__", "This module is implemented in Rust.")); try!(m.add(py, "__doc__", "This module is implemented in Rust."));
try!(m.add(py, "sum_as_string", py_fn!(py, sum_as_string_py(a: i64, b:i64)))); try!(m.add(py, "sum_as_string", py_fn!(py, sum_as_string_py(a: i64, b:i64))));
@ -90,7 +93,9 @@ fn sum_as_string(a:i64, b:i64) -> String {
} }
// rust-cpython aware function. All of our python interface could be // rust-cpython aware function. All of our python interface could be
// declared in a separate module. // declared in a separate module.
// Note that the py_fn!() macro automatically converts the arguments from
// Python objects to Rust values; and the Rust return value back into a Python object.
fn sum_as_string_py(_: Python, a:i64, b:i64) -> PyResult<String> { fn sum_as_string_py(_: Python, a:i64, b:i64) -> PyResult<String> {
let out = sum_as_string(a, b); let out = sum_as_string(a, b);
Ok(out) Ok(out)