Code Monkey home page Code Monkey logo

Comments (4)

hchataing avatar hchataing commented on July 28, 2024

I have planned changes for the Packet trait, let me know if that's good for your use case.

/// Trait implemented for all toplevel packet declarations.
pub trait Packet: Sized {
    type Parent;

    /// Try parsing an instance of Packet from the its parent representation.
    /// On success, returns the parsed object and the remaining unparsed slice.
    /// On failure, returns an error with the reason for the parsing failure.
    /// If Packet::Self is a root packet (i.e. Packet::Parent is [u8]) then the
    /// behavior of this method is the same as Packet::decode.
    fn decode_partial(buf: &Self::Parent) -> Result<Self, DecodeError>;

    /// Try parsing an instance of Packet from the input slice.
    /// On success, returns the parsed object and the remaining unparsed slice.
    /// On failure, returns an error with the reason for the parsing failure.
    fn decode_full(buf: &[u8]) -> Result<(Self, &[u8]), DecodeError>;

    /// Try parsing an instance of Packet from the input slice.
    /// Returns an error if unparsed bytes remain at the end of the input slice.
    fn decode(buf: &[u8]) -> Result<Self, DecodeError>;

    /// Return the length of the encoded packet.
    fn encoded_len(&self) -> usize;

    /// Write the packet to an output buffer.
    fn encode<T: BufMut>(&self, buf: &mut T) -> Result<(), EncodeError>;

    /// Encode the packet to a byte vector.
    fn encode_to_vec(&self) -> Result<Vec<u8>, EncodeError>;

    /// Encode the packet to a Bytes object.
    fn encode_to_bytes(&self) -> Result<Bytes, EncodeError>;
}

from pdl.

whitevegagabriel avatar whitevegagabriel commented on July 28, 2024

Thanks!
Looks like fn encode<T: BufMut> fits my use case perfectly.

I personally prefer Write over BufMut to save myself an import statement, but that's just me being picky!

from pdl.

marshallpierce avatar marshallpierce commented on July 28, 2024

BufMut is different in that it implies the write cannot fail, where as Write is for fallible I/O operations. So, I think BufMut is the right choice here since it's targeting a buffer rather than a socket, etc.

from pdl.

whitevegagabriel avatar whitevegagabriel commented on July 28, 2024

Ah! I have overlooked the fallibility of Write.

With that context, I definitely prefer the infallible BufMut.

from pdl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.