async-bincode icon indicating copy to clipboard operation
async-bincode copied to clipboard

Need Example

Open Stargateur opened this issue 3 years ago • 7 comments

it's very hard to understand how to use this crate.

Stargateur avatar Oct 28 '20 14:10 Stargateur

I'd be happy to accept a PR that adds an example! The basic idea is that you use AsyncBincodeX to wrap a AsyncX (e.g., AsyncBincodeReader to wrap a type that implements AsyncRead), and then you use that as a Stream (for AsyncRead) or Sink (for AsyncWrite). AsyncBincodeStream wraps something that is AsyncRead + AsyncWrite and gives you something that is Stream + Sink.

jonhoo avatar Nov 09 '20 01:11 jonhoo

How could I I don't understand your crate xd, how could I use bincode with stream ?

Stargateur avatar Nov 09 '20 16:11 Stargateur

You need to have some type that implements AsyncRead, say a socket, and then you do AsyncBincodeReader::from(socket). The thing you get back you can then use as a Stream.

jonhoo avatar Nov 14 '20 21:11 jonhoo

One thing I found confusing was when I tried to use AsyncBincodeStream was the lack of bounds on Deserialize/Serialize for T. This is more discoverable when looking at AsyncBincodeWriter and AsyncBincodeReader, as the bounds are directly laid out there. The compiler just told me that my AsyncBincodeStream did not implement Stream, which was not helpful in figuring out the problem.

Perhaps adding more explicit bounds would be helpful to users (it certainly would've helped me). I'd be happy to provide a PR for that.

shahn avatar Oct 28 '23 00:10 shahn

So, this is actually kind of on purpose, as there's a general recommendation in Rust to not have bounds on datastructures, and instead have them on methods/trait impls only as necessary. It avoids polluting lots of code with repeated bounds. If you can think of a nice improvement to the docs in this regard though, that'd be great!

jonhoo avatar Nov 11 '23 12:11 jonhoo

I understand that point, but shouldn't it be sufficient to add the bounds to the From and Default impls? That doesn't pollute everything with bounds, but gives immediate feedback to what is required when you're constructing the types.

I'll be happy to provide a patch for your inspection, no worries if you then still don't like it :)

shahn avatar Nov 11 '23 19:11 shahn

I'll reply in-PR :)

jonhoo avatar Nov 12 '23 09:11 jonhoo