Eli5 https://docs.rs/tokio-util/latest/tokio_util/codec/struct.FramedWrite.html

Can someone please explain what FramedWrite from tokio_util does:? In particular the forward function. I understand the basics of Async, but do not understand FramedWrite. (In particular, how to rewrite a FramedWrite::forward to not use FramedWrite::forward).

Thanks!

FramedWrite is basically like a BufWriter except that instead of writing a byte array, you write some object that is then converted to a byte array by an encoder.

There's no FramedWrite::forward method, but I'm guessing that you're looking for SinkExt::send, SinkExt::feed and SinkExt::flush, as well as the other methods from SinkExt.

You're right, I was looking at StreamExt in futures_util::stream - Rust

Followup question. Looking at tokio_util::codec - Rust

why are these traits tokio specific rather than part of async ?

Rather than part of async?

They depend on tokio::io::Async{Read,Write}Ext. Std async support as of yet doesn't actually include any async interfaces other than the Future machinery itself.

For comparison, see the equivalent but incompatible futures::io - Rust

1 Like

What I meant to say, but failed to express was:

This tokio_util::codec seems to be a general AsyncWrite/AsyncRead property, and not depend on anything tokio implementation specific, and in theory should work on any async runtime. If so, why is it in tokio_util instead of some general_crate_for_async_or_future_util_stuff ?

Because it was designed and implemented by the Tokio authors. Tokio provides lots of utilities that don't require Tokio. For example, every utility in tokio::sync is an example of this.

In the specific case of the codec, it relies on the Tokio IO traits as opposed to the futures ones. You can find an implementation that uses the futures IO traits instead in the async-codec crate.

1 Like