Crates.io | nanobit |
lib.rs | nanobit |
version | 0.2.0 |
created_at | 2025-07-29 14:52:55.03614+00 |
updated_at | 2025-08-29 10:12:32.64434+00 |
description | Ultra-fast binary serialization with multi-format compression and zero-copy deserialization |
homepage | https://github.com/jamesgober/nanobit |
repository | https://github.com/jamesgober/nanobit |
max_upload_size | |
id | 1772424 |
size | 117,996 |
Ultra-fast binary serialization with multi-format compression and zero-copy deserialization
nanobit is a high-performance binary serialization library designed for maximum efficiency and minimal overhead. Built with zero-copy deserialization, multi-format compression support, and seamless serde integration, it's perfect for database storage, network protocols, and high-throughput applications.
Add nanobit to your Cargo.toml
:
[dependencies]
nanobit = "0.2"
Enable specific compression formats:
[dependencies]
nanobit = { version = "0.2", features = ["compression", "multi-compression"] }
Available features:
std
- Standard library support (enabled by default)serde
- Serde integration (enabled by default)compression
- LZ4 compression support (enabled by default)multi-compression
- ZSTD and Snappy support (enabled by default)async
- Async serialization supportuse nanobit::{to_bytes, from_bytes};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct User {
id: u64,
name: String,
email: String,
active: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let user = User {
id: 12345,
name: "Alice Johnson".to_string(),
email: "alice@example.com".to_string(),
active: true,
};
// Serialize to bytes
let bytes = to_bytes(&user)?;
// Deserialize from bytes
let decoded: User = from_bytes(&bytes)?;
assert_eq!(user, decoded);
println!("Serialized {} bytes", bytes.len());
Ok(())
}
use nanobit::{serialize, deserialize};
// Serialize any serde-compatible type
let data = vec![1u32, 2, 3, 4, 5];
let bytes = serialize(&data)?;
let recovered: Vec<u32> = deserialize(&bytes)?;
use nanobit::{compress, decompress, CompressionFormat, CompressionLevel};
let data = b"Hello, world!".repeat(1000);
// Compress with specific format and level
let compressed = compress(&data, CompressionFormat::ZSTD, CompressionLevel::Best)?;
// Automatic format detection during decompression
let decompressed = decompress(&compressed)?;
use nanobit::{to_bytes, from_bytes};
let message = "Hello, nanobit!";
let serialized = to_bytes(&message)?;
// Zero-copy deserialization - borrows from serialized data
let text: &str = from_bytes(&serialized)?;
println!("Zero-copy: {}", text); // No allocation!
use nanobit::{compress, CompressionFormat, CompressionLevel};
let data = b"Compressible data".repeat(100);
// LZ4 - Fastest compression/decompression
let lz4_data = compress(&data, CompressionFormat::LZ4, CompressionLevel::Fastest)?;
// ZSTD - Best compression ratio
let zstd_data = compress(&data, CompressionFormat::ZSTD, CompressionLevel::Best)?;
// Snappy - Balanced speed and compression
let snappy_data = compress(&data, CompressionFormat::Snappy, CompressionLevel::Default)?;
use nanobit::{is_serialized, to_bytes};
let data = "test data";
let serialized = to_bytes(&data)?;
if is_serialized(&serialized) {
println!("Valid nanobit format");
}
use nanobit::{to_writer, from_reader};
use std::io::{Cursor, Write};
let data = vec![1, 2, 3, 4, 5];
let mut buffer = Vec::new();
// Serialize to writer
to_writer(&mut buffer, &data)?;
// Deserialize from reader
let cursor = Cursor::new(buffer);
let recovered: Vec<i32> = from_reader(cursor)?;
nanobit is designed for maximum performance:
Typical performance characteristics:
nanobit uses a compact binary format:
[MAGIC: 4 bytes]["NANO"] [VERSION: 1 byte] [PAYLOAD: variable]
"NANO"
for format identification0x01
for forward compatibilityuse nanobit::{Error, from_bytes};
match from_bytes::<String>(&invalid_data) {
Ok(data) => println!("Success: {}", data),
Err(Error::InvalidFormat(msg)) => eprintln!("Invalid format: {}", msg),
Err(Error::UnsupportedVersion(v)) => eprintln!("Unsupported version: {}", v),
Err(Error::Compression(msg)) => eprintln!("Compression error: {}", msg),
Err(e) => eprintln!("Other error: {}", e),
}
nanobit works in no_std
environments:
[dependencies]
nanobit = { version = "0.1", default-features = false, features = ["serde"] }
We welcome contributions! Please see our Contributing Guide for details.
git checkout -b feature/amazing-feature
)git commit -m 'Add amazing feature'
)git push origin feature/amazing-feature
)This project is licensed under the Apache-2.0 License - see the LICENSE file for details.
See benchmarks for detailed performance comparisons with other serialization libraries including bincode, postcard, and rmp-serde.
Built with ❤️ for high-performance applications