#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]
extern crate alloc;
use alloc::vec::Vec;
pub use crate::{
encoder::{Encoder, EncoderSlice},
error::Error,
extension::Extension,
pack::binary::{pack_bytes, pack_bytes_option},
pack::collections::{pack_array, pack_array_slice, pack_map},
unpack::binary::{
unpack_bytes, unpack_bytes_iter, unpack_bytes_option, unpack_bytes_option_iter,
unpack_bytes_option_ref,
},
unpack::collections::{
unpack_array, unpack_array_borrowed, unpack_array_iter, unpack_map, unpack_map_iter,
},
};
pub use bytes::BufMut;
#[cfg(feature = "derive")]
pub use msgpacker_derive::{MsgPacker, MsgPackerBorrowed, MsgUnpackerBorrowed};
mod borrowed;
mod encoder;
mod error;
mod extension;
mod format;
mod helpers;
mod pack;
mod unpack;
#[cfg(feature = "serde")]
pub mod serde;
pub trait Packable {
fn pack<T>(&self, buf: &mut T) -> usize
where
T: BufMut;
fn pack_to_vec(&self) -> Vec<u8> {
let mut bytes = Encoder::new();
self.pack(&mut bytes);
bytes.into_inner()
}
}
pub trait Unpackable: Sized {
type Error: From<Error>;
fn unpack_with_ofs(buf: &[u8]) -> Result<(usize, Self), Self::Error>;
fn unpack(buf: &[u8]) -> Result<Self, Self::Error> {
Self::unpack_with_ofs(buf).map(|t| t.1)
}
fn unpack_iter<I>(bytes: I) -> Result<(usize, Self), Self::Error>
where
I: IntoIterator<Item = u8>;
}
pub trait UnpackableBorrowed<'a>: Sized {
type Error: From<Error>;
fn unpack_with_ofs(buf: &'a [u8]) -> Result<(usize, Self), Self::Error>;
fn unpack(buf: &'a [u8]) -> Result<Self, Self::Error> {
Self::unpack_with_ofs(buf).map(|t| t.1)
}
}
pub fn pack_to_vec<T>(value: &T) -> Vec<u8>
where
T: Packable,
{
value.pack_to_vec()
}
impl<X: ?Sized> Packable for &X
where
X: Packable,
{
fn pack<T>(&self, buf: &mut T) -> usize
where
T: BufMut,
{
X::pack(self, buf)
}
}
impl<X: ?Sized> Packable for &mut X
where
X: Packable,
{
fn pack<T>(&self, buf: &mut T) -> usize
where
T: BufMut,
{
X::pack(self, buf)
}
}