I'm having trouble asyncifying a certain piece of code. When doing things synchronously, I would just clone
a UnixStream
, and pass around one as a Read
er, and the other as a Write
r.
Now though I need the async variants, and tokio does have an async UnixStream
, which I can split
to get an AsyncRead
er and an AsyncWrite
r. Now though, those contain references, so I can't pass them into tasks to run, because they aren't 'static
. How can I deal with that?
Here's the code in question:
pub async fn new_unix_socket<H, P: AsRef<Path> + Clone>(
path: P,
handler: H,
) -> io::Result<(Neovim<WriteHalf>, JoinHandle<Result<(), Box<LoopError>>>)>
where
H: Handler<Writer = WriteHalf> + Send + 'static,
{
let (reader, writer) = UnixStream::connect(path).await?.split();
let (neovim, io) = Neovim::<WriteHalf>::new(reader, writer, handler);
let io_handle = spawn(io);
Ok((neovim, io_handle))
}
This does not work, of course, but I wanted to make some comments on the concrete uses here. I need to construct a Neovim
, which will be sent to tasks all over the place. I contains an AsyncWrite
r (that's the type argument), which I' wrapping in an Arc<Mutex>>
until now, so I can just clone
it and send it to the tasks. The other thing is io
, a future I construct and spawn, which represents an io loop. Until now, it owns the AsyncRead
er, and also has a clone
of the Arc<Mutex<AsyncWrite>>
er.
So, I kinda think I needs owned variants here to send them around to other tasks. If I were directly using threads, I'd look for scoped threads so I can send references, but I can't do so with tasks, right? One thing I could imagine is to move the AsyncRead
er and the AsyncWrite
er into the io loop both, and the Neovim
instance just gets a channel to send things through it wants written into the AsyncWrite
. But that still doesn't solve the problem I have that I can't send references to the io-loop-task...
Ok, so any ideas, hints, am I missing something? The crate in question is here, but note it contains this functions in a broken state, because this just opens 2 streams, which won't work.
Thanks for any pointers