Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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" }
32 changes: 15 additions & 17 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,22 @@ impl PyRecord {
}

#[getter]
fn properties<'py>(&self, py: Python<'py>) -> PyResult<&'py PyDict> {
let properties = PyDict::new(py);
fn properties(&self) -> PyResult<HashMap<String, String>> {
let mut properties: HashMap<String, String> = HashMap::new();
for (key, value) in &self.0.properties {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems weird to do a for loop for this, can't we just return something like properties.deep_clone()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how deep/shallow is the clone trait implementation in the Hashmap code. the docs says "Returns a copy of the value."

I've test .clone(), it seem to work fine.

properties.set_item(key, value)?;
properties.insert(key.into(), value.into());
}
Ok(properties)
}

}

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 +68,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 +90,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