| Crates.io | ser-write |
| lib.rs | ser-write |
| version | 0.3.1 |
| created_at | 2024-05-11 22:09:25.919036+00 |
| updated_at | 2025-06-13 14:26:47.772882+00 |
| description | Common traits for writer-style serializers and deserializers designed for no_std targets |
| homepage | https://github.com/royaltm/rust-ser-write |
| repository | https://github.com/royaltm/rust-ser-write |
| max_upload_size | |
| id | 1237088 |
| size | 40,537 |
Writer-style serializers and deserializers for convenience designed with embedded (no_std) targets in mind.
no_std.std or alloc when enabled for code portability and testablilty.SerWrite trait for custom containers, frame builders and more.This crate provides:
SerWrite which should be used by serializers to write the serialized output,SerError - a convenient error type,SliceWriter - a convenient slice writer object implementing SerWrite,SerWrite implementations for foreign types.Depending on the enabled crate features, SerWrite is implemented for:
SliceWriter - example slice writer implementation,arrayvec::ArrayVec<u8,CAP> - arrayvec feature,heapless::Vec<u8,CAP> - heapless feature,smallvec::SmallVec<[u8; CAP]> - smallvec feature,tinyvec::ArrayVec<[u8; CAP]> - tinyvec feature,tinyvec::SliceVec<'_, u8> - tinyvec feature,tinyvec::TinyVec<[u8; CAP]> - tinyvec with alloc or std feature,Vec<u8> - alloc or std feature,VecDeque<u8> - alloc or std feature,io::Cursor<T: io::Write> - std feature,smallvec also enables alloc.
Start by adding a dependency to the serializer:
For example:
[dependencies]
ser-write-json = { version = "0.3", default-features = false }
If you want to also pull implementations of SerWrite for the foreign types add:
ser-write = { version = "0.3", default-features = false, features = ["arrayvec", "heapless"] }
In the above example implementations for: arrayvec::ArrayVec<u8;_> and heapless::Vec<u8> are selected.
Currently available serializers and deserializers are:
JSON (compact) - ser-write-json
An example SliceWriter implementation:
use ser_write::{SerWrite, SerResult, SerError};
#[derive(Debug, PartialEq)]
pub struct SliceWriter<'a> {
pub buf: &'a mut [u8],
pub len: usize
}
impl SerWrite for SliceWriter<'_> {
fn write(&mut self, buf: &[u8]) -> SerResult<()> {
let end = self.len + buf.len();
match self.buf.get_mut(self.len..end) {
Some(chunk) => {
chunk.copy_from_slice(buf);
self.len = end;
Ok(())
}
None => Err(SerError::BufferFull)
}
}
}
For alloc only:
Alternatively there's a Rust Embedded Community crate for serializeing JSONs without std:
fmt::Display trait)serde-json-core is a true no_std, no alloc alternative but rather inconvenient. One has to serialize data into intermediate slices instead just pushing data to outgoing buffers or frame builder implementations.
This crate would not be needed once something like io::Write lands in the Rust core.
ser-write requires Rustc version 1.81 or greater.