chore(python): cleaned up the leftovers

This commit is contained in:
cschen 2024-08-21 15:45:24 +02:00
parent 6d01e8aa24
commit fe2f2a3ae0
2 changed files with 30 additions and 62 deletions

View file

@ -2,11 +2,6 @@ pub mod client;
pub mod controllers; pub mod controllers;
pub mod workspace; pub mod workspace;
use std::{
future::{poll_fn, Future},
task::Poll,
};
use crate::{ use crate::{
api::{Cursor, TextChange}, api::{Cursor, TextChange},
buffer::Controller as BufferController, buffer::Controller as BufferController,
@ -36,34 +31,34 @@ pub fn tokio() -> &'static tokio::runtime::Runtime {
}) })
} }
// // workaround to allow the GIL to be released across awaits, waiting on #[pyclass]
// // https://github.com/PyO3/pyo3/pull/3610 pub struct Promise(Option<tokio::task::JoinHandle<PyResult<PyObject>>>);
// struct AllowThreads<F>(F);
// impl<F> Future for AllowThreads<F> #[pymethods]
// where impl Promise {
// F: Future + Unpin + Send, #[pyo3(name = "wait")]
// F::Output: Send, fn _await(&mut self) -> PyResult<PyObject> {
// { match self.0.take() {
// type Output = F::Output; None => Err(PyRuntimeError::new_err(
"promise can't be awaited multiple times!",
)),
Some(x) => match tokio().block_on(x) {
Err(e) => Err(PyRuntimeError::new_err(format!(
"error awaiting promise: {e}"
))),
Ok(res) => res,
},
}
}
// fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn done(&self) -> PyResult<bool> {
// let waker = cx.waker(); if let Some(handle) = &self.0 {
// let fut = unsafe { self.map_unchecked_mut(|e| &mut e.0) }; Ok(handle.is_finished())
// Python::with_gil(|py| py.allow_threads(|| fut.poll(&mut Context::from_waker(waker)))) } else {
// } Err(PyRuntimeError::new_err("promise was already awaited."))
// } }
// #[macro_export] }
// macro_rules! spawn_future_allow_threads { }
// ($fut:expr) => {
// $crate::ffi::python::tokio().spawn($crate::ffi::python::AllowThreads(Box::pin(
// async move {
// tracing::info!("running future from rust.");
// $fut.await
// },
// )))
// };
// }
#[macro_export] #[macro_export]
macro_rules! a_sync { macro_rules! a_sync {
@ -80,7 +75,7 @@ struct LoggerProducer(mpsc::UnboundedSender<String>);
impl std::io::Write for LoggerProducer { impl std::io::Write for LoggerProducer {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let _ = self.0.send(String::from_utf8_lossy(buf).to_string()); // ignore: logger disconnected or with full buffer let _ = self.0.send(String::from_utf8_lossy(buf).to_string());
Ok(buf.len()) Ok(buf.len())
} }
@ -140,10 +135,10 @@ fn init(logging_cb: Py<PyFunction>, debug: bool) -> PyResult<PyObject> {
tokio().block_on(async move { tokio().block_on(async move {
tokio::select! { tokio::select! {
biased; biased;
_ = rt_stop_rx => { todo!() },
Some(msg) = rx.recv() => { Some(msg) = rx.recv() => {
let _ = Python::with_gil(|py| logging_cb.call1(py, (msg,))); let _ = Python::with_gil(|py| logging_cb.call1(py, (msg,)));
}, },
_ = rt_stop_rx => { todo!() },
} }
}) })
}); });
@ -153,34 +148,6 @@ fn init(logging_cb: Py<PyFunction>, debug: bool) -> PyResult<PyObject> {
} }
} }
#[pyclass]
pub struct Promise(Option<tokio::task::JoinHandle<PyResult<PyObject>>>);
#[pymethods]
impl Promise {
#[pyo3(name = "wait")]
fn _await(&mut self) -> PyResult<PyObject> {
match self.0.take() {
None => Err(PySystemError::new_err(
"promise can't be awaited multiple times!",
)),
Some(x) => match tokio().block_on(x) {
Err(e) => Err(PySystemError::new_err(format!(
"error awaiting promise: {e}"
))),
Ok(res) => res,
},
}
}
fn is_done(&self) -> bool {
if let Some(handle) = self.0 {
return handle.is_finished();
}
false
}
}
impl From<crate::Error> for PyErr { impl From<crate::Error> for PyErr {
fn from(value: crate::Error) -> Self { fn from(value: crate::Error) -> Self {
match value { match value {

View file

@ -51,7 +51,7 @@ impl Workspace {
#[pyo3(name = "list_buffer_users")] #[pyo3(name = "list_buffer_users")]
fn pylist_buffer_users(&self, path: String) -> PyResult<Promise> { fn pylist_buffer_users(&self, path: String) -> PyResult<Promise> {
// crate::Result<Vec<crate::api::User>> { // crate::Result<Vec<crate::api::User>>
let this = self.clone(); let this = self.clone();
a_sync!(this.list_buffer_users(path.as_str()).await) a_sync!(this.list_buffer_users(path.as_str()).await)
} }
@ -83,6 +83,7 @@ impl Workspace {
} }
#[pyo3(name = "filetree")] #[pyo3(name = "filetree")]
#[pyo3(signature = (filter=None))]
fn pyfiletree(&self, filter: Option<&str>) -> Vec<String> { fn pyfiletree(&self, filter: Option<&str>) -> Vec<String> {
self.filetree(filter) self.filetree(filter)
} }