made the serde functions consistent with the base bincode functions (#483)

This commit is contained in:
Trangar 2022-01-19 18:57:52 +01:00 committed by GitHub
parent fc1f3acdcb
commit d13d177bea
5 changed files with 96 additions and 19 deletions

View File

@ -23,17 +23,23 @@ use std::{
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn decode_from_std_read<D: Decode, C: Config, R: std::io::Read>(
src: &mut R,
_config: C,
config: C,
) -> Result<D, DecodeError> {
let reader = IoReader { reader: src };
let mut decoder = DecoderImpl::<_, C>::new(reader, _config);
let reader = IoReader::new(src);
let mut decoder = DecoderImpl::<_, C>::new(reader, config);
D::decode(&mut decoder)
}
struct IoReader<R> {
pub(crate) struct IoReader<R> {
reader: R,
}
impl<R> IoReader<R> {
pub fn new(reader: R) -> Self {
Self { reader }
}
}
impl<R> Reader for IoReader<R>
where
R: std::io::Read,
@ -79,20 +85,30 @@ pub fn encode_into_std_write<E: Encode, C: Config, W: std::io::Write>(
dst: &mut W,
config: C,
) -> Result<usize, EncodeError> {
let writer = IoWriter {
writer: dst,
bytes_written: 0,
};
let writer = IoWriter::new(dst);
let mut encoder = EncoderImpl::<_, C>::new(writer, config);
val.encode(&mut encoder)?;
Ok(encoder.into_writer().bytes_written)
Ok(encoder.into_writer().bytes_written())
}
struct IoWriter<'a, W: std::io::Write> {
pub(crate) struct IoWriter<'a, W: std::io::Write> {
writer: &'a mut W,
bytes_written: usize,
}
impl<'a, W: std::io::Write> IoWriter<'a, W> {
pub fn new(writer: &'a mut W) -> Self {
Self {
writer,
bytes_written: 0,
}
}
pub fn bytes_written(&self) -> usize {
self.bytes_written
}
}
impl<'storage, W: std::io::Write> Writer for IoWriter<'storage, W> {
#[inline(always)]
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {

View File

@ -1,7 +1,7 @@
use super::DecodeError as SerdeDecodeError;
use crate::{
config::Config,
de::{Decode, Decoder},
de::{read::Reader, Decode, Decoder},
error::DecodeError,
};
use serde_incl::de::*;
@ -24,6 +24,33 @@ where
Ok((result, bytes_read))
}
/// Decode an owned type from the given `std::io::Read`.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn decode_from_std_read<D: DeserializeOwned, C: Config, R: std::io::Read>(
src: &mut R,
config: C,
) -> Result<D, DecodeError> {
let reader = crate::IoReader::new(src);
let mut decoder = crate::de::DecoderImpl::new(reader, config);
let serde_decoder = SerdeDecoder { de: &mut decoder };
D::deserialize(serde_decoder)
}
/// Attempt to decode a given type `D` from the given [Reader].
///
/// See the [config] module for more information on configurations.
///
/// [config]: config/index.html
pub fn decode_from_reader<D: DeserializeOwned, R: Reader, C: Config>(
reader: R,
config: C,
) -> Result<D, DecodeError> {
let mut decoder = crate::de::DecoderImpl::<_, C>::new(reader, config);
let serde_decoder = SerdeDecoder { de: &mut decoder };
D::deserialize(serde_decoder)
}
pub(crate) struct SerdeDecoder<'a, DE: Decoder> {
pub(crate) de: &'a mut DE,
}

View File

@ -1,7 +1,7 @@
use super::EncodeError as SerdeEncodeError;
use crate::{
config::Config,
enc::{Encode, Encoder},
enc::{write::Writer, Encode, Encoder},
error::EncodeError,
};
#[cfg(feature = "alloc")]
@ -23,7 +23,7 @@ where
}
/// Encode a `serde` `Serialize` type into a given byte slice with the bincode algorithm
pub fn encode_to_slice<T, C>(t: T, slice: &mut [u8], config: C) -> Result<usize, EncodeError>
pub fn encode_into_slice<T, C>(t: T, slice: &mut [u8], config: C) -> Result<usize, EncodeError>
where
T: Serialize,
C: Config,
@ -35,6 +35,40 @@ where
Ok(encoder.into_writer().bytes_written())
}
/// Encode the given value into a custom [Writer].
///
/// See the [config] module for more information on configurations.
///
/// [config]: config/index.html
pub fn encode_into_writer<E: Serialize, W: Writer, C: Config>(
val: E,
writer: W,
config: C,
) -> Result<(), EncodeError> {
let mut encoder = crate::enc::EncoderImpl::<_, C>::new(writer, config);
let serializer = SerdeEncoder { enc: &mut encoder };
val.serialize(serializer)?;
Ok(())
}
/// Encode the given value into any type that implements `std::io::Write`, e.g. `std::fs::File`, with the given `Config`.
/// See the [config] module for more information.
///
/// [config]: config/index.html
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[cfg(feature = "std")]
pub fn encode_into_std_write<E: Serialize, C: Config, W: std::io::Write>(
val: E,
dst: &mut W,
config: C,
) -> Result<usize, EncodeError> {
let writer = crate::IoWriter::new(dst);
let mut encoder = crate::enc::EncoderImpl::<_, C>::new(writer, config);
let serializer = SerdeEncoder { enc: &mut encoder };
val.serialize(serializer)?;
Ok(encoder.into_writer().bytes_written())
}
pub(super) struct SerdeEncoder<'a, ENC: Encoder> {
pub(super) enc: &'a mut ENC,
}

View File

@ -133,9 +133,9 @@ pub fn decode_from_slice<'a, D: de::BorrowDecode<'a>, C: Config>(
/// [config]: config/index.html
pub fn decode_from_reader<D: de::Decode, R: Reader, C: Config>(
reader: R,
_config: C,
config: C,
) -> Result<D, error::DecodeError> {
let mut decoder = de::DecoderImpl::<_, C>::new(reader, _config);
let mut decoder = de::DecoderImpl::<_, C>::new(reader, config);
D::decode(&mut decoder)
}

View File

@ -61,8 +61,8 @@ fn test_serialize_deserialize_borrowed_data() {
];
let mut result = [0u8; 20];
let len =
bincode::serde::encode_to_slice(&input, &mut result, bincode::config::standard()).unwrap();
let len = bincode::serde::encode_into_slice(&input, &mut result, bincode::config::standard())
.unwrap();
let result = &result[..len];
assert_eq!(result, expected);
@ -107,8 +107,8 @@ fn test_serialize_deserialize_owned_data() {
];
let mut result = [0u8; 20];
let len =
bincode::serde::encode_to_slice(&input, &mut result, bincode::config::standard()).unwrap();
let len = bincode::serde::encode_into_slice(&input, &mut result, bincode::config::standard())
.unwrap();
let result = &result[..len];
assert_eq!(result, expected);