chore(python): added name to buffer controller, small cleanup

This commit is contained in:
cschen 2024-08-23 20:41:07 +02:00
parent 88f4ace04a
commit 076128e1db
3 changed files with 15 additions and 21 deletions

5
dist/py/codemp.pyi vendored
View file

@ -42,12 +42,13 @@ class BufferController:
Handle to the controller for a specific buffer, which manages the back and forth Handle to the controller for a specific buffer, which manages the back and forth
of operations to and from other peers. of operations to and from other peers.
""" """
def name(self) -> str: ...
def content(self) -> Promise[str]: ... def content(self) -> Promise[str]: ...
def send(self, def send(self,
start: int, start: int,
end: int, end: int,
txt: str) -> Promise[None]: ... txt: str) -> Promise[None]: ...
def try_recv(self) -> Optional[TextChange]: ... def try_recv(self) -> Promise[Optional[TextChange]]: ...
def recv(self) -> Promise[TextChange]: ... def recv(self) -> Promise[TextChange]: ...
def poll(self) -> Promise[None]: ... def poll(self) -> Promise[None]: ...
def callback(self, def callback(self,
@ -76,7 +77,7 @@ class CursorController:
path: str, path: str,
start: Tuple[int, int], start: Tuple[int, int],
end: Tuple[int, int]) -> Promise[None]: ... end: Tuple[int, int]) -> Promise[None]: ...
def try_recv(self) -> Optional[Cursor]: ... def try_recv(self) -> Promise[Optional[Cursor]]: ...
def recv(self) -> Promise[Cursor]: ... def recv(self) -> Promise[Cursor]: ...
def poll(self) -> Promise[None]: ... def poll(self) -> Promise[None]: ...
def callback(self, def callback(self,

View file

@ -31,22 +31,9 @@ impl CursorController {
} }
#[pyo3(name = "try_recv")] #[pyo3(name = "try_recv")]
fn pytry_recv(&self, py: Python) -> PyResult<PyObject> { fn pytry_recv(&self, py: Python) -> PyResult<Promise> {
// why? I want try-recv to have that 'blocking' flavour, for the "cool guy async" approach there's
// 'recv'...
let this = self.clone(); let this = self.clone();
let prom: crate::Result<Promise> = a_sync_allow_threads!(py, this.try_recv().await); a_sync_allow_threads!(py, this.try_recv().await)
prom?._await(py)
// // bad situation, here we either return an opaque PyResult<PyObject>
// // or if we want to return exacly a Result<Option<Cursor>> we would need to extract it back
// // into a rust object... which is expensive.
// // This is stupid isn't it?
// // the PyResult<Option<Cursor>> will become a PyObject anyway to be returned back... lmao
// let this = self.clone();
// let prom: crate::Result<Promise> = a_sync_allow_threads!(py, this.try_recv().await);
// let pyobj = prom?._await(py)?;
// let opt = pyobj.extract::<Option<Cursor>>(py)?;
// Ok(opt)
} }
#[pyo3(name = "recv")] #[pyo3(name = "recv")]
@ -87,6 +74,11 @@ impl CursorController {
// need to do manually since Controller is a trait implementation // need to do manually since Controller is a trait implementation
#[pymethods] #[pymethods]
impl BufferController { impl BufferController {
#[pyo3(name = "name")]
fn pyname(&self) -> String {
self.name().to_string()
}
#[pyo3(name = "content")] #[pyo3(name = "content")]
fn pycontent(&self, py: Python) -> PyResult<Promise> { fn pycontent(&self, py: Python) -> PyResult<Promise> {
let this = self.clone(); let this = self.clone();
@ -106,10 +98,9 @@ impl BufferController {
} }
#[pyo3(name = "try_recv")] #[pyo3(name = "try_recv")]
fn pytry_recv(&self, py: Python) -> crate::Result<Option<TextChange>> { fn pytry_recv(&self, py: Python) -> PyResult<Promise> {
py.allow_threads(|| super::tokio().block_on(self.try_recv())) let this = self.clone();
// let this = self.clone(); a_sync_allow_threads!(py, this.try_recv().await)
// a_sync_allow_threads!(py, this.try_recv().await)
} }
#[pyo3(name = "recv")] #[pyo3(name = "recv")]

View file

@ -53,6 +53,8 @@ pub struct Promise(Option<tokio::task::JoinHandle<PyResult<PyObject>>>);
#[pymethods] #[pymethods]
impl Promise { impl Promise {
// Can't use this in callbacks since tokio will complain about running
// a runtime inside another runtime.
#[pyo3(name = "wait")] #[pyo3(name = "wait")]
fn _await(&mut self, py: Python<'_>) -> PyResult<PyObject> { fn _await(&mut self, py: Python<'_>) -> PyResult<PyObject> {
py.allow_threads(move || match self.0.take() { py.allow_threads(move || match self.0.take() {