5df684fa6b
Rust requires all methods of impl Trait to be in a single file, which means we had a giant backend/mod.rs covering all exposed methods. By using separate service definitions for the separate areas, and updating the code generation, we can split it into more manageable chunks - this commit starts with the scheduling code. In the long run, we'll probably want to split up the protobuf file into multiple files as well. Also dropped want_release_gil() from rsbridge, and the associated method enum. While it allows us to skip the thread save/restore and mutex unlock/ lock, it looks to only be buying about 2.5% extra performance in the best case (tested with timeit+format_timespan), and the majority of the backend methods deal with I/O, and thus were already releasing the GIL.
74 lines
2.0 KiB
Rust
74 lines
2.0 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use anki::backend::{init_backend, Backend as RustBackend};
|
|
use pyo3::exceptions::PyException;
|
|
use pyo3::prelude::*;
|
|
use pyo3::types::PyBytes;
|
|
use pyo3::{create_exception, wrap_pyfunction};
|
|
|
|
#[pyclass(module = "rsbridge")]
|
|
struct Backend {
|
|
backend: RustBackend,
|
|
}
|
|
|
|
create_exception!(rsbridge, BackendError, PyException);
|
|
|
|
#[pyfunction]
|
|
fn buildhash() -> &'static str {
|
|
anki::version::buildhash()
|
|
}
|
|
|
|
#[pyfunction]
|
|
fn open_backend(init_msg: &PyBytes) -> PyResult<Backend> {
|
|
match init_backend(init_msg.as_bytes()) {
|
|
Ok(backend) => Ok(Backend { backend }),
|
|
Err(e) => Err(PyException::new_err(e)),
|
|
}
|
|
}
|
|
|
|
#[pymethods]
|
|
impl Backend {
|
|
fn command(
|
|
&self,
|
|
py: Python,
|
|
service: u32,
|
|
method: u32,
|
|
input: &PyBytes,
|
|
) -> PyResult<PyObject> {
|
|
let in_bytes = input.as_bytes();
|
|
py.allow_threads(|| self.backend.run_method(service, method, in_bytes))
|
|
.map(|out_bytes| {
|
|
let out_obj = PyBytes::new(py, &out_bytes);
|
|
out_obj.into()
|
|
})
|
|
.map_err(BackendError::new_err)
|
|
}
|
|
|
|
/// This takes and returns JSON, due to Python's slow protobuf
|
|
/// encoding/decoding.
|
|
fn db_command(&self, py: Python, input: &PyBytes) -> PyResult<PyObject> {
|
|
let in_bytes = input.as_bytes();
|
|
let out_res = py.allow_threads(|| {
|
|
self.backend
|
|
.run_db_command_bytes(in_bytes)
|
|
.map_err(BackendError::new_err)
|
|
});
|
|
let out_bytes = out_res?;
|
|
let out_obj = PyBytes::new(py, &out_bytes);
|
|
Ok(out_obj.into())
|
|
}
|
|
}
|
|
|
|
// Module definition
|
|
//////////////////////////////////
|
|
|
|
#[pymodule]
|
|
fn rsbridge(_py: Python, m: &PyModule) -> PyResult<()> {
|
|
m.add_class::<Backend>()?;
|
|
m.add_wrapped(wrap_pyfunction!(buildhash)).unwrap();
|
|
m.add_wrapped(wrap_pyfunction!(open_backend)).unwrap();
|
|
|
|
Ok(())
|
|
}
|