reorganize de/enc modules
This commit is contained in:
parent
e160c73acb
commit
b7c0d1fac0
|
|
@ -0,0 +1,8 @@
|
||||||
|
use super::{Decode, Decodable};
|
||||||
|
use crate::error::Error;
|
||||||
|
|
||||||
|
impl Decodable for u32 {
|
||||||
|
fn decode<D: Decode>(mut decoder: D) -> Result<Self, Error> {
|
||||||
|
decoder.decode_u32()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
config::{Config, Endian},
|
||||||
|
error::Error,
|
||||||
|
};
|
||||||
|
use read::Reader;
|
||||||
|
|
||||||
|
mod impls;
|
||||||
|
pub mod read;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pub trait Decodable: Sized {
|
||||||
|
fn decode<D: Decode>(decoder: D) -> Result<Self, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Decode {
|
||||||
|
fn decode_u32(&mut self) -> Result<u32, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Decoder<R, C: Config> {
|
||||||
|
reader: R,
|
||||||
|
config: PhantomData<C>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, R: Reader<'de>, C: Config> Decoder<R, C> {
|
||||||
|
pub fn new(reader: R) -> Decoder<R, C> {
|
||||||
|
Decoder {
|
||||||
|
reader,
|
||||||
|
config: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'de, R: Reader<'de>, C: Config> Decode for &'a mut Decoder<R, C> {
|
||||||
|
fn decode_u32(&mut self) -> Result<u32, Error> {
|
||||||
|
let mut bytes = [0u8; 4];
|
||||||
|
|
||||||
|
self.reader.read(bytes.as_mut())?;
|
||||||
|
Ok(match C::ENDIAN {
|
||||||
|
Endian::Little => u32::from_le_bytes(bytes),
|
||||||
|
Endian::Big => u32::from_be_bytes(bytes),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,9 +1,4 @@
|
||||||
use core::marker::PhantomData;
|
use crate::error::Error;
|
||||||
|
|
||||||
use crate::{
|
|
||||||
config::{Config, Endian},
|
|
||||||
error::Error,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub trait Reader<'storage> {
|
pub trait Reader<'storage> {
|
||||||
fn read(&mut self, bytes: &mut [u8]) -> Result<(), Error>;
|
fn read(&mut self, bytes: &mut [u8]) -> Result<(), Error>;
|
||||||
|
|
@ -12,46 +7,6 @@ pub trait Reader<'storage> {
|
||||||
F: Fn(&'storage [u8]) -> R;
|
F: Fn(&'storage [u8]) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Decodable: Sized {
|
|
||||||
fn decode<D: Decode>(decoder: D) -> Result<Self, Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Decode {
|
|
||||||
fn decode_u32(&mut self) -> Result<u32, Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Decoder<R, C: Config> {
|
|
||||||
reader: R,
|
|
||||||
config: PhantomData<C>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de, R: Reader<'de>, C: Config> Decoder<R, C> {
|
|
||||||
pub fn new(reader: R) -> Decoder<R, C> {
|
|
||||||
Decoder {
|
|
||||||
reader,
|
|
||||||
config: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, 'de, R: Reader<'de>, C: Config> Decode for &'a mut Decoder<R, C> {
|
|
||||||
fn decode_u32(&mut self) -> Result<u32, Error> {
|
|
||||||
let mut bytes = [0u8; 4];
|
|
||||||
|
|
||||||
self.reader.read(bytes.as_mut())?;
|
|
||||||
Ok(match C::ENDIAN {
|
|
||||||
Endian::Little => u32::from_le_bytes(bytes),
|
|
||||||
Endian::Big => u32::from_be_bytes(bytes),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decodable for u32 {
|
|
||||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, Error> {
|
|
||||||
decoder.decode_u32()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SliceReader<'storage> {
|
pub struct SliceReader<'storage> {
|
||||||
slice: &'storage [u8],
|
slice: &'storage [u8],
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +30,7 @@ impl<'storage> SliceReader<'storage> {
|
||||||
|
|
||||||
impl<'storage> Reader<'storage> for SliceReader<'storage> {
|
impl<'storage> Reader<'storage> for SliceReader<'storage> {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn read(&mut self, bytes: &mut [u8]) -> Result<(), Error> {
|
fn read<'a>(&'a mut self, bytes: &mut [u8]) -> Result<(), Error> {
|
||||||
if bytes.len() > self.slice.len() {
|
if bytes.len() > self.slice.len() {
|
||||||
return Err(Error::UnexpectedEnd);
|
return Err(Error::UnexpectedEnd);
|
||||||
}
|
}
|
||||||
|
|
@ -93,4 +48,4 @@ impl<'storage> Reader<'storage> for SliceReader<'storage> {
|
||||||
{
|
{
|
||||||
Ok(visitor(self.get_byte_slice(length)?))
|
Ok(visitor(self.get_byte_slice(length)?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
use super::{Encode, Encodeable};
|
||||||
|
use crate::error::Error;
|
||||||
|
|
||||||
|
impl Encodeable for u32 {
|
||||||
|
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), Error> {
|
||||||
|
encoder.encode_u32(*self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
config::{Config, Endian},
|
||||||
|
error::Error,
|
||||||
|
};
|
||||||
|
use write::Writer;
|
||||||
|
|
||||||
|
mod impls;
|
||||||
|
pub mod write;
|
||||||
|
|
||||||
|
|
||||||
|
pub trait Encodeable {
|
||||||
|
fn encode<E: Encode>(&self, encoder: E) -> Result<(), Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Encode {
|
||||||
|
fn encode_u32(&mut self, val: u32) -> Result<(), Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Encoder<W: Writer, C: Config> {
|
||||||
|
writer: W,
|
||||||
|
config: PhantomData<C>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: Writer, C: Config> Encoder<W, C> {
|
||||||
|
pub fn new(writer: W) -> Encoder<W, C> {
|
||||||
|
Encoder {
|
||||||
|
writer,
|
||||||
|
config: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, W: Writer, C: Config> Encode for &'a mut Encoder<W, C> {
|
||||||
|
fn encode_u32(&mut self, val: u32) -> Result<(), Error> {
|
||||||
|
let bytes = match C::ENDIAN {
|
||||||
|
Endian::Little => val.to_le_bytes(),
|
||||||
|
Endian::Big => val.to_be_bytes(),
|
||||||
|
};
|
||||||
|
|
||||||
|
self.writer.write(&bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
use crate::error::Error;
|
||||||
|
|
||||||
|
pub trait Writer {
|
||||||
|
fn write(&mut self, bytes: &[u8]) -> Result<(), Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SliceWriter<'storage> {
|
||||||
|
slice: &'storage mut [u8],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'storage> SliceWriter<'storage> {
|
||||||
|
pub(crate) fn new(bytes: &'storage mut [u8]) -> SliceWriter<'storage> {
|
||||||
|
SliceWriter {
|
||||||
|
slice: bytes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'storage> Writer for SliceWriter<'storage> {
|
||||||
|
fn write(&mut self, bytes: &[u8]) -> Result<(), Error> {
|
||||||
|
if bytes.len() > self.slice.len() {
|
||||||
|
return Err(Error::UnexpectedEnd);
|
||||||
|
}
|
||||||
|
let data = core::mem::take(&mut self.slice);
|
||||||
|
let (write_slice, remaining) = data.split_at_mut(bytes.len());
|
||||||
|
write_slice.copy_from_slice(bytes);
|
||||||
|
self.slice = remaining;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{error::Error, ser::Writer};
|
use crate::{error::Error, enc::write::Writer};
|
||||||
|
|
||||||
pub trait IntEncoding {
|
pub trait IntEncoding {
|
||||||
fn encode_u32<W: Writer>(writer: &mut W, val: u32) -> Result<(), Error>;
|
fn encode_u32<W: Writer>(writer: &mut W, val: u32) -> Result<(), Error>;
|
||||||
|
|
|
||||||
10
src/lib.rs
10
src/lib.rs
|
|
@ -18,18 +18,18 @@ extern crate std;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod de;
|
pub mod de;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod ser;
|
pub mod enc;
|
||||||
|
|
||||||
pub(crate) mod int_encoding;
|
pub(crate) mod int_encoding;
|
||||||
|
|
||||||
pub fn encode_into_slice<E: ser::Encodeable>(val: E, dst: &mut [u8]) -> Result<(), error::Error> {
|
pub fn encode_into_slice<E: enc::Encodeable>(val: E, dst: &mut [u8]) -> Result<(), error::Error> {
|
||||||
let writer = ser::SliceWriter::new(dst);
|
let writer = enc::write::SliceWriter::new(dst);
|
||||||
let mut encoder = ser::Encoder::<_, config::Default>::new(writer);
|
let mut encoder = enc::Encoder::<_, config::Default>::new(writer);
|
||||||
val.encode(&mut encoder)
|
val.encode(&mut encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decode<D: de::Decodable>(src: &mut [u8]) -> Result<D, error::Error> {
|
pub fn decode<D: de::Decodable>(src: &mut [u8]) -> Result<D, error::Error> {
|
||||||
let reader = de::SliceReader::new(src);
|
let reader = de::read::SliceReader::new(src);
|
||||||
let mut decoder = de::Decoder::<_, config::Default>::new(reader);
|
let mut decoder = de::Decoder::<_, config::Default>::new(reader);
|
||||||
D::decode(&mut decoder)
|
D::decode(&mut decoder)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
77
src/ser.rs
77
src/ser.rs
|
|
@ -1,77 +0,0 @@
|
||||||
use core::marker::PhantomData;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
config::{Config, Endian},
|
|
||||||
error::Error,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub trait Writer {
|
|
||||||
fn write(&mut self, bytes: &[u8]) -> Result<(), Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Encodeable {
|
|
||||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Encode {
|
|
||||||
fn encode_u32(&mut self, val: u32) -> Result<(), Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Encoder<W: Writer, C: Config> {
|
|
||||||
writer: W,
|
|
||||||
config: PhantomData<C>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: Writer, C: Config> Encoder<W, C> {
|
|
||||||
pub fn new(writer: W) -> Encoder<W, C> {
|
|
||||||
Encoder {
|
|
||||||
writer,
|
|
||||||
config: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, W: Writer, C: Config> Encode for &'a mut Encoder<W, C> {
|
|
||||||
fn encode_u32(&mut self, val: u32) -> Result<(), Error> {
|
|
||||||
let bytes = match C::ENDIAN {
|
|
||||||
Endian::Little => val.to_le_bytes(),
|
|
||||||
Endian::Big => val.to_be_bytes(),
|
|
||||||
};
|
|
||||||
|
|
||||||
self.writer.write(&bytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encodeable for u32 {
|
|
||||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), Error> {
|
|
||||||
encoder.encode_u32(*self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SliceWriter<'storage> {
|
|
||||||
slice: &'storage mut [u8],
|
|
||||||
cursor: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'storage> SliceWriter<'storage> {
|
|
||||||
pub(crate) fn new(bytes: &'storage mut [u8]) -> SliceWriter<'storage> {
|
|
||||||
SliceWriter {
|
|
||||||
slice: bytes,
|
|
||||||
cursor: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'storage> Writer for SliceWriter<'storage> {
|
|
||||||
fn write(&mut self, bytes: &[u8]) -> Result<(), Error> {
|
|
||||||
if bytes.len() - self.cursor > self.slice.len() {
|
|
||||||
return Err(Error::UnexpectedEnd);
|
|
||||||
}
|
|
||||||
let temp = &mut self.slice[self.cursor..bytes.len()];
|
|
||||||
|
|
||||||
temp.copy_from_slice(bytes);
|
|
||||||
self.cursor += bytes.len();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -4,7 +4,7 @@ use core::fmt::Debug;
|
||||||
|
|
||||||
fn the_same<V>(element: V)
|
fn the_same<V>(element: V)
|
||||||
where
|
where
|
||||||
V: bincode::ser::Encodeable + bincode::de::Decodable + PartialEq + Debug + Clone + 'static,
|
V: bincode::enc::Encodeable + bincode::de::Decodable + PartialEq + Debug + Clone + 'static,
|
||||||
{
|
{
|
||||||
let mut buffer = [0u8; 32];
|
let mut buffer = [0u8; 32];
|
||||||
bincode::encode_into_slice(element.clone(), &mut buffer).unwrap();
|
bincode::encode_into_slice(element.clone(), &mut buffer).unwrap();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue