Crates.io | packet-binser |
lib.rs | packet-binser |
version | 0.3.0 |
source | src |
created_at | 2024-09-08 19:25:32.140041 |
updated_at | 2024-10-31 06:43:41.799294 |
description | Simple binary (de)serialization library with a main usecase of network packets |
homepage | |
repository | https://git.sr.ht/~queer/packet-binser |
max_upload_size | |
id | 1368476 |
size | 25,096 |
packet-binser
is a Rust library for simple (de)serialization of network packets.
It provides default implementations for predictibly sized primitive types as well as common std types.
derive
feature.)Cargo.toml
Add the following to your Cargo.toml
file:
[dependencies]
# or latest version
packet-binser = "0.2" # or packet-binser = { version = "0.2", features = [...] }
use packet_binser::{Binser, BytesReadExt, BytesWriteExt, Error};
struct HandshakePacket(u8);
impl Binser for HandshakePacket {
fn serialize<B: BytesWriteExt>(&self, buffer: &mut B) -> Result<(), Error> {
buffer.write_u8(0x1)?; // packet id, not read in deserialize since it should be read elsewhere
self.0.serialize(buffer)?;
Ok(())
}
fn deserialize<B: BytesReadExt>(buffer: &mut B) -> Result<Self, Error> {
Ok(Self(u8::deserialize(buffer)?))
}
}
use packet_binser::proc::Binser;
#[derive(Binser)]
#[repr(u8)]
enum ClientPackets {
Handshake {
id: u8,
} = 0x1,
}
u8
, u16
, u32
, u64
, u128
i8
, i16
, i32
, i64
, i128
f32
, f64
bool
std::string::String
std::option::Option<T: Binser>
std::vec::Vec<T: Binser>
[T; N] where T: Binser
This project is dual licensed under both the MIT License and Apache License 2.0.
Feel free to open an issue if you encounter any problems or have suggestions.