-
Notifications
You must be signed in to change notification settings - Fork 95
Open
Description
We are in the process of adopting quick-protobuf in rust-libp2p and a few points came up whilst using the API.
- The naming of various traits and functions is too similar: For example, there is
MessageWrite::write_messageandWriter::write_message. The latter uses the former but also includes a length-prefix. We've tripped over this multiple times. - There is
serialize_into_vecwhich writes a length-prefix but it is not documented. - There are three traits implemented for a message, which all need to be imported so you can work with them.
- There is a
BytesWriter, aWriter, aWriterBackendand theMessageWrite. The relationship is not entirely clear from the naming.
My suggestions would be:
- Unify
MessageInfo,MessageReadandMessageWriteinto a single traitMessage. - The standard library already has an abstraction for writing bytes:
std::io::Write. CanMessage::writeperhaps simply take a&mut std::io::Write? This would remove the layer of constructing aWriterwith aWriterBackend. Astd::fs::Filealready implementsWriteand so doesstd::vec::Vec. - On a related thought, we might want to consider offering an
AsyncMessagetype that uses thefuturesabstractionAsyncReadandAsyncWrite. - This might be controversial but I think writing a message with a length-prefix is out of scope for this library. Length-prefixing messages is a protocol for how to parties can interact. I think we are better off with focusing on reading and writing protobufs in this library. I am guessing protobufs themselves are not self-descriptive for this very reason: It is too opinionated to be really generally useful.
- Provide only a handful of utility functions at the module root:
quick_protobuf::from_slice::<M>(&[u8]) -> Result<M>quick_protobuf::to_vec::<M>(M) -> Vec<u8>serdeis a battle-tested library. We should be able to take some inspiration from what functions they provide: https://docs.rs/serde_json/1.0.93/serde_json/#functions
- We will need some abstractions within the library that the generated-code can use to avoid duplication. I think those should be in a separate module like
codegenorinternalwhich clearly signals to users that they shouldn't use these directly.
Let me know what you think. I am open to working on this and sending PRs :)
Metadata
Metadata
Assignees
Labels
No labels