Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 30 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyreccaster/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name = "pyreccaster"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "^0.20", features = ["extension-module", "generate-import-lib", "abi3-py37"] }
pyo3-asyncio = { version = "^0.20", features = ["attributes", "tokio-runtime"] }
pyo3 = { version = "^0.25", features = ["extension-module", "generate-import-lib", "abi3-py37"] }
pyo3-async-runtimes = { version = "^0.25", features = ["attributes", "tokio-runtime"] }
tokio = { version = "^1", features = ["full"] }
reccaster = { path = "../reccaster" }
34 changes: 14 additions & 20 deletions pyreccaster/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

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

use pyo3::{prelude::*, types::PyDict};
use pyo3_asyncio::tokio::future_into_py_with_locals;
use pyo3::prelude::*;
use pyo3_async_runtimes::tokio::future_into_py_with_locals;
use reccaster::{Record, Reccaster};
use tokio::sync::Mutex;

Expand Down Expand Up @@ -39,23 +39,18 @@ impl PyRecord {
}

#[getter]
fn properties<'py>(&self, py: Python<'py>) -> PyResult<&'py PyDict> {
let properties = PyDict::new(py);
for (key, value) in &self.0.properties {
properties.set_item(key, value)?;
}
Ok(properties)
fn properties(&self) -> PyResult<HashMap<String, String>> {
Ok(self.0.properties.clone())
}

}

impl<'source> FromPyObject<'source> for PyRecord {
fn extract(ob: &'source PyAny) -> PyResult<Self> {
impl FromPyObject<'_> for PyRecord {
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
let name: String = ob.getattr("name")?.extract().unwrap_or_else(|_| "OPS no name !!!!!!!!!!!".to_string());
let r#type: String = ob.getattr("type")?.extract()?;
let alias: Option<String> = ob.getattr("alias")?.extract()?;
let properties: HashMap<String, String> = ob.getattr("properties")?.extract()?;

Ok(PyRecord (Record { name, r#type, alias, properties }))
}
}
Expand All @@ -69,21 +64,20 @@ struct PyReccaster {
impl PyReccaster {

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

fn run<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> {
fn run<'a>(&self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
let recc_arc = self.reccaster.clone();
let locals = pyo3_asyncio::tokio::get_current_locals(py)?;

future_into_py_with_locals(py, locals.clone(), async move {
let locals = pyo3_async_runtimes::tokio::get_current_locals(py)?;
future_into_py_with_locals(py, locals, async move {
let mut recc = recc_arc.lock().await;
recc.run().await;
Ok(())
Expand All @@ -92,7 +86,7 @@ impl PyReccaster {
}

#[pymodule]
fn pyreccaster(_py: Python, m: &PyModule) -> PyResult<()> {
fn pyreccaster(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyReccaster>()?;
m.add_class::<PyRecord>()?;
Ok(())
Expand Down