zpp-2022-rust-seastar / seastar-rs

Idiomatic Rust bindings to the Seastar framework
6 stars 3 forks source link

Add support for seastar::promise<> and seastar::future<> in cxx #44

Open piodul opened 1 year ago

piodul commented 1 year ago

Future and promise pairs are the fundamental synchronization primitive in seastar. Unfortunately, cxx doesn't support passing them to rust as they are templates. However, having futures and promises + ability to spawn tasks would be sufficient to replace cxx-async completely.

We have some experience with adding support for lw_shared_ptr in our cxx fork, so let's try the same for futures and promises.

Futures and promises have nontrivial move constructors so Rust musn't manipulate them directly by value but rather through some pointer (reference, std::unique_ptr, Box, etc.)

I imagine the following interface:

pub struct SeastarFuture<T> {
    pub async fn wait(&mut self) -> Result<T>;
}

pub struct SeastarPromise<T> {
    pub fn set_value(&mut self, t: T);
    pub fn set_exception(&mut self, e: rust::Error);
}

It's fine to split it in two parts, e.g. implement promise first and then future. I think implementing support for promises should be simpler.