Skip to content

Reduce copying with methods to query the inner read buffer #29

@parasyte

Description

@parasyte

I am writing a transport to encapsulate SerialPort to add framing and encoding (serde). When I want to read from the port, I need to allocate some space in my Transport but I don't know how many bytes to allocate until the read is actually performed. This means I need to keep an intermediate buffer for bytes to be read into, and then I can copy the number of bytes actually read from the port into my Transport.

However, SerialPort already has internal buffering with a default size of 128 bytes for the read buffer. So, I am effectively triple buffering and copying twice for no good reason.

On the host side with the serialport crate, I can query the number of bytes available in the OS buffer with SerialPort::bytes_to_read(). It is trivial for me to allocate space in my Transport with this API and copy only once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions