#send-receive #length-prefixed #binary #packet #stream #send-packet #async-write #async-read #async-io #tcp-stream

packetio

A simple Rust crate for sending and receiving length-prefixed binary packets over any stream

7 releases

Uses new Rust 2024

0.2.1 Aug 26, 2025
0.2.0 Aug 15, 2025
0.1.33 Jul 9, 2025

#2 in #length-prefixed


Used in rustacean-db

BSD-3-Clause

8KB
116 lines

packetio

A simple, efficient Rust crate for sending and receiving length-prefixed binary packets over any Read + Write or AsyncRead + AsyncWrite stream using bincode serialization.

Example (Sync)

writer.send_packet(&test_struct).unwrap();

let mut size_buf = [0u8; 4];
reader.read_exact(&mut size_buf).unwrap();
let size = parsing::parse_length(size_buf);

let mut packet = vec![0u8; size];
reader.read_exact(&mut packet).unwrap();

let result: TestStruct = parsing::parse_packet(packet).unwrap();

Example (Async)

use tokio::net::TcpStream;
use packetio::AsyncPacketSender;

let mut stream = TcpStream::connect("127.0.0.1:8080").await?;
stream.send_packet_async(&my_packet).await?;

packetio

Crates.io Size

A simple Rust crate for sending and receiving length-prefixed binary packets over any stream that implements Read or Write. Uses bincode for serialization.

It lets you send structs and rust types over the network!


Features

  • Sends and receives length-prefixed packets to keep boundaries clear
  • Generic over any stream implementing Read or Write)(TcpStream, BufReader, etc.)
  • Minimal API with a handy PacketSender and PacketReceiver traits for easy use
  • Uses bincode for compact, efficient binary encoding
  • Lightweight and just straight forward!

Installation

Add this to your Cargo.toml:

[dependencies]
packetio = "0.1"
bincode = "2"

Dependencies

~3–12MB
~117K SLoC