-
Notifications
You must be signed in to change notification settings - Fork 41
length_delimited should allow using a custom Codec #63
Comments
Here is an example: https://github.com/carllerche/tokio-serde-json/blob/master/src/lib.rs The strategy is to "map" the |
I think that this is a lot of overhead and work to write for every possible serialization format possible. Would it be possible to offer this in a more generic way? For the read part it's easy enough to use let length_delimited = Builder::new()
.varint()
.new_framed(stream);
let (write, read) = length_delimited.split();
let read = read.and_then(decode);
fn decode(buf: BytesMut) -> io::Result<Item> { ... } But for the write part it's rather hard to wrap encoding around the sink. Instead, I need to wrap it around a stream which produces items / manually call encode / write bloaty code like it's done in tokio-serde-json. I think that from a user experience point of view the idea of EDIT: I just found the |
You probably wouldn't split, and you can use It would be easier if |
Given |
I can't find a way to use a custom Codec in conjuction with length_delimited. I'm currently working on a PR to implement varint support for length_delimeted (an untested work in progress can be found here). The usecase is to use protobuf with a varint length delimiter. With
length_delimited::Framed
only implementingStream
andSink
, but notAsyncRead
andAsyncWrite
, it's not possible to chain aCodec
withAsyncRead::framed
on it in order to deserialize the returnedBytesMut
/ serialize items intoBytesMut
before length-delimiting them.My suggestion would be to either implement
AsyncRead
andAsyncWrite
onFramed
, and respectively onFramedRead
andFramedWrite
.Another idea is to be able to add a custom
Codec
toBuilder
, which will be invoked after a full frame has been read intoBytesMut
for deserialization, and for serialization in order to create theBytesMut
which needs to be length-delimited.The text was updated successfully, but these errors were encountered: