wip(python): moving the rest of the glue to promises (empty promises)

This commit is contained in:
cschen 2024-08-21 15:02:44 +02:00
parent 4a575c587f
commit a4eb58cd4b
5 changed files with 107 additions and 64 deletions

48
dist/py/codemp.pyi vendored
View file

@ -3,21 +3,24 @@ from typing import Tuple, Optional, Callable
class Driver:
"""
this is akin to a big red button with a white "STOP" on top of it.
it is used to stop the runtime
it is used to stop the runtime.
"""
def stop(self) -> None: ...
def init(logger_cb: Callable, debug: bool) -> Driver: ...
class RustPromise[T]:
class Promise[T]:
"""
This is a class akin to a future, which wraps a join handle from a spawned
task on the rust side. you may call .pyawait() on this promise to block
until we have a result, or return immediately if we already have one.
This only goes one way rust -> python.
It can either be used directly or you can wrap it inside a future python side.
"""
def pyawait(self) -> T: ...
def wait(self) -> T: ...
def is_done(self) -> bool: ...
class TextChange:
"""
@ -39,11 +42,15 @@ class BufferController:
Handle to the controller for a specific buffer, which manages the back and forth
of operations to and from other peers.
"""
async def content(self) -> str: ...
async def send(self, start: int, end: int, txt: str) -> None: ...
async def try_recv(self) -> Optional[TextChange]: ...
async def recv(self) -> TextChange: ...
async def poll(self) -> None: ...
def content(self) -> Promise[str]: ...
def send(self,
start: int,
end: int,
txt: str) -> Promise[None]: ...
def try_recv(self) -> Optional[TextChange]: ...
def recv(self) -> Promise[TextChange]: ...
def poll(self) -> Promise[None]: ...
def stop(self) -> bool: ...
@ -62,10 +69,13 @@ class CursorController:
Handle to the controller for a workspace, which manages the back and forth of
cursor movements to and from other peers
"""
async def send(self, path: str, start: Tuple[int, int], end: Tuple[int, int]) -> None: ...
async def try_recv(self) -> Optional[Cursor]: ...
async def recv(self) -> Cursor: ...
async def poll(self) -> None: ...
def send(self,
path: str,
start: Tuple[int, int],
end: Tuple[int, int]) -> Promise[None]: ...
def try_recv(self) -> Optional[Cursor]: ...
def recv(self) -> Promise[Cursor]: ...
def poll(self) -> Promise[None]: ...
def stop(self) -> bool: ...
@ -74,13 +84,13 @@ class Workspace:
Handle to a workspace inside codemp. It manages buffers.
A cursor is tied to the single workspace.
"""
async def create(self, path: str) -> None: ...
async def attach(self, path: str) -> BufferController: ...
def create(self, path: str) -> Promise[None]: ...
def attach(self, path: str) -> Promise[BufferController]: ...
def detach(self, path: str) -> bool: ...
async def fetch_buffers(self) -> None: ...
async def fetch_users(self) -> None: ...
async def list_buffer_users(self, path: str) -> list[str]: ...
async def delete(self, path: str) -> None: ...
def fetch_buffers(self) -> Promise[None]: ...
def fetch_users(self) -> Promise[None]: ...
def list_buffer_users(self, path: str) -> Promise[list[str]]: ...
def delete(self, path: str) -> Promise[None]: ...
def id(self) -> str: ...
def cursor(self) -> CursorController: ...
def buffer_by_name(self, path: str) -> Optional[BufferController]: ...
@ -94,7 +104,7 @@ class Client:
to a server and joining/creating new workspaces
"""
def __new__(cls, host: str, username: str, password: str) -> None: ...
def join_workspace(self, workspace: str) -> RustPromise: ...
def join_workspace(self, workspace: str) -> Promise[Workspace]: ...
def leave_workspace(self, workspace: str) -> bool: ...
def get_workspace(self, id: str) -> Workspace: ...
def active_workspaces(self) -> list[str]: ...

View file

@ -11,15 +11,27 @@ impl Client {
tokio().block_on(Client::new(host, username, password))
}
// #[pyo3(name = "join_workspace")]
// async fn pyjoin_workspace(&self, workspace: String) -> JoinHandle<crate::Result<Workspace>> {
// tracing::info!("attempting to join the workspace {}", workspace);
// let this = self.clone();
// async {
// tokio()
// .spawn(async move { this.join_workspace(workspace).await })
// .await
// }
// }
#[pyo3(name = "join_workspace")]
fn pyjoin_workspace(&self, workspace: String) -> PyResult<super::RustPromise> {
fn pyjoin_workspace(&self, workspace: String) -> PyResult<super::Promise> {
tracing::info!("attempting to join the workspace {}", workspace);
let this = self.clone();
crate::a_sync!(this.join_workspace(workspace).await)
// let rc = self.clone();
// Ok(super::RustPromise(Some(tokio().spawn(async move {
// Ok(rc
// let this = self.clone();
// Ok(super::Promise(Some(tokio().spawn(async move {
// Ok(this
// .join_workspace(workspace)
// .await
// .map(|f| Python::with_gil(|py| f.into_py(py)))?)

View file

@ -5,39 +5,41 @@ use crate::buffer::Controller as BufferController;
use crate::cursor::Controller as CursorController;
use pyo3::prelude::*;
use crate::spawn_future;
use super::Promise;
use crate::a_sync;
// need to do manually since Controller is a trait implementation
#[pymethods]
impl CursorController {
#[pyo3(name = "send")]
async fn pysend(&self, path: String, start: (i32, i32), end: (i32, i32)) -> crate::Result<()> {
fn pysend(&self, path: String, start: (i32, i32), end: (i32, i32)) -> PyResult<Promise> {
let pos = Cursor {
start,
end,
buffer: path,
user: None,
};
let rc = self.clone();
spawn_future!(rc.send(pos)).await.unwrap()
let this = self.clone();
a_sync!(this.send(pos).await)
}
#[pyo3(name = "try_recv")]
async fn pytry_recv(&self) -> crate::Result<Option<Cursor>> {
let rc = self.clone();
spawn_future!(rc.try_recv()).await.unwrap()
fn pytry_recv(&self) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.try_recv().await)
}
#[pyo3(name = "recv")]
async fn pyrecv(&self) -> crate::Result<Cursor> {
let rc = self.clone();
spawn_future!(rc.recv()).await.unwrap()
fn pyrecv(&self) -> crate::Result<Option<Cursor>> {
Ok(super::tokio().block_on(self.try_recv())?)
// let this = self.clone();
// a_sync!(this.recv().await)
}
#[pyo3(name = "poll")]
async fn pypoll(&self) -> crate::Result<()> {
let rc = self.clone();
spawn_future!(rc.poll()).await.unwrap()
fn pypoll(&self) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.poll().await)
}
#[pyo3(name = "stop")]
@ -50,39 +52,45 @@ impl CursorController {
#[pymethods]
impl BufferController {
#[pyo3(name = "content")]
async fn pycontent(&self) -> crate::Result<String> {
let rc = self.clone();
spawn_future!(rc.content()).await.unwrap()
async fn pycontent(&self) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.content().await)
}
#[pyo3(name = "send")]
async fn pysend(&self, start: u32, end: u32, txt: String) -> crate::Result<()> {
async fn pysend(&self, start: u32, end: u32, txt: String) -> PyResult<Promise> {
let op = TextChange {
start,
end,
content: txt,
hash: None,
};
let rc = self.clone();
spawn_future!(rc.send(op)).await.unwrap()
let this = self.clone();
a_sync!(this.send(op).await)
}
#[pyo3(name = "try_recv")]
async fn pytry_recv(&self) -> crate::Result<Option<TextChange>> {
let rc = self.clone();
spawn_future!(rc.try_recv()).await.unwrap()
fn pytry_recv(&self) -> crate::Result<Option<TextChange>> {
Ok(super::tokio().block_on(self.try_recv())?)
// let this = self.clone();
// a_sync!(this.try_recv().await)
}
#[pyo3(name = "recv")]
async fn pyrecv(&self) -> crate::Result<TextChange> {
let rc = self.clone();
spawn_future!(rc.recv()).await.unwrap()
async fn pyrecv(&self) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.recv().await)
}
#[pyo3(name = "poll")]
async fn pypoll(&self) -> crate::Result<()> {
let rc = self.clone();
spawn_future!(rc.poll()).await.unwrap()
async fn pypoll(&self) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.poll().await)
}
#[pyo3(name = "stop")]
fn pystop(&self) -> bool {
self.stop()
}
}

View file

@ -2,12 +2,18 @@ pub mod client;
pub mod controllers;
pub mod workspace;
use std::{
future::{poll_fn, Future},
task::Poll,
};
use crate::{
api::{Cursor, TextChange},
buffer::Controller as BufferController,
cursor::Controller as CursorController,
Client, Workspace,
};
use pyo3::prelude::*;
use pyo3::{
exceptions::{PyConnectionError, PyRuntimeError, PySystemError},
@ -41,7 +47,7 @@ pub fn tokio() -> &'static tokio::runtime::Runtime {
// {
// type Output = F::Output;
// fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// let waker = cx.waker();
// let fut = unsafe { self.map_unchecked_mut(|e| &mut e.0) };
// Python::with_gil(|py| py.allow_threads(|| fut.poll(&mut Context::from_waker(waker))))
@ -62,7 +68,7 @@ pub fn tokio() -> &'static tokio::runtime::Runtime {
#[macro_export]
macro_rules! a_sync {
($x:expr) => {{
Ok($crate::ffi::python::RustPromise(Some(
Ok($crate::ffi::python::Promise(Some(
$crate::ffi::python::tokio()
.spawn(async move { Ok($x.map(|f| Python::with_gil(|py| f.into_py(py)))?) }),
)))
@ -148,11 +154,11 @@ fn init(logging_cb: Py<PyFunction>, debug: bool) -> PyResult<PyObject> {
}
#[pyclass]
pub struct RustPromise(Option<tokio::task::JoinHandle<PyResult<PyObject>>>);
pub struct Promise(Option<tokio::task::JoinHandle<PyResult<PyObject>>>);
#[pymethods]
impl RustPromise {
#[pyo3(name = "pyawait")]
impl Promise {
#[pyo3(name = "wait")]
fn _await(&mut self) -> PyResult<PyObject> {
match self.0.take() {
None => Err(PySystemError::new_err(
@ -166,6 +172,13 @@ impl RustPromise {
},
}
}
fn is_done(&self) -> bool {
if let Some(handle) = self.0 {
return handle.is_finished();
}
false
}
}
impl From<crate::Error> for PyErr {

View file

@ -3,7 +3,7 @@ use crate::cursor::Controller as CursorController;
use crate::workspace::Workspace;
use pyo3::prelude::*;
use super::RustPromise;
use super::Promise;
use crate::a_sync;
// use super::Promise;
@ -11,13 +11,13 @@ use crate::a_sync;
impl Workspace {
// join a workspace
#[pyo3(name = "create")]
fn pycreate(&self, path: String) -> PyResult<RustPromise> {
fn pycreate(&self, path: String) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.create(path.as_str()).await)
}
#[pyo3(name = "attach")]
fn pyattach(&self, path: String) -> PyResult<RustPromise> {
fn pyattach(&self, path: String) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.attach(path.as_str()).await)
}
@ -32,32 +32,32 @@ impl Workspace {
}
#[pyo3(name = "event")]
fn pyevent(&self) -> PyResult<RustPromise> {
fn pyevent(&self) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.event().await)
}
#[pyo3(name = "fetch_buffers")]
fn pyfetch_buffers(&self) -> PyResult<RustPromise> {
fn pyfetch_buffers(&self) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.fetch_buffers().await)
}
#[pyo3(name = "fetch_users")]
fn pyfetch_users(&self) -> PyResult<RustPromise> {
fn pyfetch_users(&self) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.fetch_users().await)
}
#[pyo3(name = "list_buffer_users")]
fn pylist_buffer_users(&self, path: String) -> PyResult<RustPromise> {
fn pylist_buffer_users(&self, path: String) -> PyResult<Promise> {
// crate::Result<Vec<crate::api::User>> {
let this = self.clone();
a_sync!(this.list_buffer_users(path.as_str()).await)
}
#[pyo3(name = "delete")]
fn pydelete(&self, path: String) -> PyResult<RustPromise> {
fn pydelete(&self, path: String) -> PyResult<Promise> {
let this = self.clone();
a_sync!(this.delete(path.as_str()).await)
}