Skip to content

Proposal for a re-worked API #246

@thomaseizinger

Description

@thomaseizinger

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_message and Writer::write_message. The latter uses the former but also includes a length-prefix. We've tripped over this multiple times.
  • There is serialize_into_vec which 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, a Writer, a WriterBackend and the MessageWrite. The relationship is not entirely clear from the naming.

My suggestions would be:

  1. Unify MessageInfo, MessageRead and MessageWrite into a single trait Message.
  2. The standard library already has an abstraction for writing bytes: std::io::Write. Can Message::write perhaps simply take a &mut std::io::Write? This would remove the layer of constructing a Writer with a WriterBackend. A std::fs::File already implements Write and so does std::vec::Vec.
  3. On a related thought, we might want to consider offering an AsyncMessage type that uses the futures abstraction AsyncRead and AsyncWrite.
  4. 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.
  5. Provide only a handful of utility functions at the module root:
  6. 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 codegen or internal which 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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions