2018-08-19 18:42:17 +00:00
|
|
|
#![feature(specialization)]
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-09-08 22:19:55 +00:00
|
|
|
#[macro_use]
|
2018-05-02 18:49:40 +00:00
|
|
|
extern crate pyo3;
|
|
|
|
|
|
|
|
use pyo3::prelude::*;
|
2018-09-21 21:32:48 +00:00
|
|
|
use pyo3::types::PyDict;
|
2018-05-02 18:49:40 +00:00
|
|
|
use std::isize;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod common;
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct MutRefArg {
|
|
|
|
n: i32,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl MutRefArg {
|
|
|
|
fn get(&self) -> PyResult<i32> {
|
|
|
|
Ok(self.n)
|
|
|
|
}
|
|
|
|
fn set_other(&self, other: &mut MutRefArg) -> PyResult<()> {
|
|
|
|
other.n = 100;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_ref_arg() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2018-11-12 13:15:11 +00:00
|
|
|
let inst1 = py.init(|| MutRefArg { n: 0 }).unwrap();
|
|
|
|
let inst2 = py.init(|| MutRefArg { n: 0 }).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
let d = PyDict::new(py);
|
|
|
|
d.set_item("inst1", &inst1).unwrap();
|
|
|
|
d.set_item("inst2", &inst2).unwrap();
|
|
|
|
|
|
|
|
py.run("inst1.set_other(inst2)", None, Some(d)).unwrap();
|
|
|
|
assert_eq!(inst2.as_ref(py).n, 100);
|
|
|
|
}
|
2018-09-08 22:19:55 +00:00
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct PyUsize {
|
|
|
|
#[prop(get)]
|
|
|
|
pub value: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn get_zero() -> PyResult<PyUsize> {
|
|
|
|
Ok(PyUsize { value: 0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Checks that we can use return a custom class in arbitrary function and use those functions
|
|
|
|
/// both in rust and python
|
|
|
|
fn return_custom_class() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
// Using from rust
|
|
|
|
assert_eq!(get_zero().unwrap().value, 0);
|
|
|
|
|
|
|
|
// Using from python
|
|
|
|
let get_zero = wrap_function!(get_zero)(py);
|
|
|
|
py_assert!(py, get_zero, "get_zero().value == 0");
|
|
|
|
}
|