How to use write_vectored method from async-std::io::Write Trait without blocking a full thread?

The write_vectored() method from async-std::io::Write Trait takes a slice of async-std::io::IoSlice as parameter. As async-std::io::IoSlice is !Send the Future returned by write_vectored() is !Send as well and so cannot be run without using Task::block_on() blocking a full thread and all the tasks mapped to this thread.

Is there a way of using async-std write_vectored() without blocking a full thread through a Task::block_on() call ?

I'm not too familiar with async-std, but the answer appears to be no. I mean you can probably still do things concurrently in that task you're running in block_on with join! and similar tools, but it does seem like you need to use block_on.

It's possible there's some magical solution I'm not aware of, as I know a lot more about Tokio than I do about async-std.

async_std::io::IoSlice[Mut] is a re-export of std::io::IoSlice[Mut], which is a cross platform abstraction of platform native vectorized io buffer. Normally you should hold and send buffers in a form of Vec<u8>, Box<[u8]>, or even [u8; N] or Box<[u8; N]> as the IoSlice<'a> can be made from &'a [u8] in zero cost.

Thanks Alice.
Unfortunately, Future::join nor Future::race are suitable as I cannot afford to wait for all the futures to terminate nor cancel some of the futures. A Future::peek (return when the first future completes but does not cancel the other futures) would be very useful but this is for another topic.

Thanks Hyeonu!
write_vectored seems to be the only way to send several memory chunks in a single system call which allows better performances. But if there are other solutions to do the same, I'd be happy to learn them.

It doesn't seem like using write_vectored in async-std is practical at all. I recommend just using multiple system calls, or perhaps concatenating the buffers into one if you have many small buffers. As for your "peek" thing, the futures crate provides a collection called FuturesUnordered which can do that.

The lack of Sync + Send on IoSlice and IoSliceMut is a bug - I just filed an issue for it: https://github.com/rust-lang/rust/issues/70308

1 Like

Excellent news. I'll wait for this fix.
And FuturesUnordered is also very interesting for other parts of the code. I'll have a deeper look into it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.