Skip to content

Commit 53aab40

Browse files
authored
Merge pull request #4 from ChannelFinder/pyo3-refactoring
PyO3 v0.25 refactoring
2 parents b13c97e + 7ff3363 commit 53aab40

File tree

3 files changed

+46
-64
lines changed

3 files changed

+46
-64
lines changed

Cargo.lock

Lines changed: 30 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyreccaster/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ name = "pyreccaster"
1111
crate-type = ["cdylib"]
1212

1313
[dependencies]
14-
pyo3 = { version = "^0.20", features = ["extension-module", "generate-import-lib", "abi3-py37"] }
15-
pyo3-asyncio = { version = "^0.20", features = ["attributes", "tokio-runtime"] }
14+
pyo3 = { version = "^0.25", features = ["extension-module", "generate-import-lib", "abi3-py37"] }
15+
pyo3-async-runtimes = { version = "^0.25", features = ["attributes", "tokio-runtime"] }
1616
tokio = { version = "^1", features = ["full"] }
1717
reccaster = { path = "../reccaster" }

pyreccaster/src/lib.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
use std::{collections::HashMap, sync::Arc};
99

10-
use pyo3::{prelude::*, types::PyDict};
11-
use pyo3_asyncio::tokio::future_into_py_with_locals;
10+
use pyo3::prelude::*;
11+
use pyo3_async_runtimes::tokio::future_into_py_with_locals;
1212
use reccaster::{Record, Reccaster};
1313
use tokio::sync::Mutex;
1414

@@ -39,23 +39,18 @@ impl PyRecord {
3939
}
4040

4141
#[getter]
42-
fn properties<'py>(&self, py: Python<'py>) -> PyResult<&'py PyDict> {
43-
let properties = PyDict::new(py);
44-
for (key, value) in &self.0.properties {
45-
properties.set_item(key, value)?;
46-
}
47-
Ok(properties)
42+
fn properties(&self) -> PyResult<HashMap<String, String>> {
43+
Ok(self.0.properties.clone())
4844
}
4945

5046
}
5147

52-
impl<'source> FromPyObject<'source> for PyRecord {
53-
fn extract(ob: &'source PyAny) -> PyResult<Self> {
48+
impl FromPyObject<'_> for PyRecord {
49+
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
5450
let name: String = ob.getattr("name")?.extract().unwrap_or_else(|_| "OPS no name !!!!!!!!!!!".to_string());
5551
let r#type: String = ob.getattr("type")?.extract()?;
5652
let alias: Option<String> = ob.getattr("alias")?.extract()?;
5753
let properties: HashMap<String, String> = ob.getattr("properties")?.extract()?;
58-
5954
Ok(PyRecord (Record { name, r#type, alias, properties }))
6055
}
6156
}
@@ -69,21 +64,20 @@ struct PyReccaster {
6964
impl PyReccaster {
7065

7166
#[staticmethod]
72-
fn setup(py: Python, records: Vec<PyRecord>, props: Option<HashMap<String, String>>) -> PyResult<&PyAny> {
73-
let locals = pyo3_asyncio::tokio::get_current_locals(py)?;
67+
fn setup(py: Python, records: Vec<PyRecord>, props: Option<HashMap<String, String>>) -> PyResult<Bound<'_, pyo3::PyAny>> {
68+
let locals = pyo3_async_runtimes::tokio::get_current_locals(py)?;
7469
let pvs = records.iter().map(|record: &PyRecord| record.0.clone()).collect::<Vec<Record>>();
75-
future_into_py_with_locals(py, locals.clone(), async move {
70+
future_into_py_with_locals(py, locals, async move {
7671
let recc = Reccaster::new(pvs, props).await;
7772
let pyrecc = PyReccaster { reccaster: Arc::new(Mutex::new(recc)) };
78-
Python::with_gil(|py| Ok(pyrecc.into_py(py)))
73+
Python::with_gil(|_py| Ok(pyrecc))
7974
})
8075
}
8176

82-
fn run<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> {
77+
fn run<'a>(&self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
8378
let recc_arc = self.reccaster.clone();
84-
let locals = pyo3_asyncio::tokio::get_current_locals(py)?;
85-
86-
future_into_py_with_locals(py, locals.clone(), async move {
79+
let locals = pyo3_async_runtimes::tokio::get_current_locals(py)?;
80+
future_into_py_with_locals(py, locals, async move {
8781
let mut recc = recc_arc.lock().await;
8882
recc.run().await;
8983
Ok(())
@@ -92,7 +86,7 @@ impl PyReccaster {
9286
}
9387

9488
#[pymodule]
95-
fn pyreccaster(_py: Python, m: &PyModule) -> PyResult<()> {
89+
fn pyreccaster(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
9690
m.add_class::<PyReccaster>()?;
9791
m.add_class::<PyRecord>()?;
9892
Ok(())

0 commit comments

Comments
 (0)