Crates.io | codeq |
lib.rs | codeq |
version | 0.5.2 |
created_at | 2024-10-10 03:33:07.540134+00 |
updated_at | 2025-03-21 02:06:46.457425+00 |
description | Codec trait |
homepage | https://github.com/drmingdrmer/codeq |
repository | https://github.com/drmingdrmer/codeq |
max_upload_size | |
id | 1403412 |
size | 91,557 |
A binary codec library optimized for storage and networking, with built-in checksum support and file operation utilities.
Add this to your Cargo.toml
:
[dependencies]
codeq = "0.1"
use codeq::{Codec, Decode, Encode, WithChecksum};
use std::io;
#[derive(Debug, Clone, PartialEq)]
struct Record {
id: u32,
data: Vec<u8>,
}
impl Encode for Record {
fn encode<W: io::Write>(&self, mut writer: W) -> io::Result<usize> {
let mut n = 0;
n += self.id.encode(&mut writer)?;
n += self.data.encode(&mut writer)?;
Ok(n)
}
}
impl Decode for Record {
fn decode<R: io::Read>(mut reader: R) -> io::Result<Self> {
Ok(Self {
id: u32::decode(&mut reader)?,
data: Vec::decode(&mut reader)?,
})
}
}
// Add checksum protection
let record = Record { id: 1, data: vec![1, 2, 3] };
let protected = WithChecksum::new(&record);
let mut buf = Vec::new();
protected.encode(&mut buf).unwrap();
assert_eq!(buf, vec![ //
0, 0, 0, 1, // id
0, 0, 0, 3, 1, 2, 3, // data
0, 0, 0, 0, 31, 101, 71, 147 // checksum
]);
let decoded = Record::decode(&mut buf.as_slice()).unwrap();
assert_eq!(record, decoded);
Choose codeq when you need:
Use serde when you need: