made the serde functions consistent with the base bincode functions (#483)
This commit is contained in:
parent
fc1f3acdcb
commit
d13d177bea
|
|
@ -23,17 +23,23 @@ use std::{
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
pub fn decode_from_std_read<D: Decode, C: Config, R: std::io::Read>(
|
pub fn decode_from_std_read<D: Decode, C: Config, R: std::io::Read>(
|
||||||
src: &mut R,
|
src: &mut R,
|
||||||
_config: C,
|
config: C,
|
||||||
) -> Result<D, DecodeError> {
|
) -> Result<D, DecodeError> {
|
||||||
let reader = IoReader { reader: src };
|
let reader = IoReader::new(src);
|
||||||
let mut decoder = DecoderImpl::<_, C>::new(reader, _config);
|
let mut decoder = DecoderImpl::<_, C>::new(reader, config);
|
||||||
D::decode(&mut decoder)
|
D::decode(&mut decoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IoReader<R> {
|
pub(crate) struct IoReader<R> {
|
||||||
reader: R,
|
reader: R,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<R> IoReader<R> {
|
||||||
|
pub fn new(reader: R) -> Self {
|
||||||
|
Self { reader }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<R> Reader for IoReader<R>
|
impl<R> Reader for IoReader<R>
|
||||||
where
|
where
|
||||||
R: std::io::Read,
|
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,
|
dst: &mut W,
|
||||||
config: C,
|
config: C,
|
||||||
) -> Result<usize, EncodeError> {
|
) -> Result<usize, EncodeError> {
|
||||||
let writer = IoWriter {
|
let writer = IoWriter::new(dst);
|
||||||
writer: dst,
|
|
||||||
bytes_written: 0,
|
|
||||||
};
|
|
||||||
let mut encoder = EncoderImpl::<_, C>::new(writer, config);
|
let mut encoder = EncoderImpl::<_, C>::new(writer, config);
|
||||||
val.encode(&mut encoder)?;
|
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,
|
writer: &'a mut W,
|
||||||
bytes_written: usize,
|
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> {
|
impl<'storage, W: std::io::Write> Writer for IoWriter<'storage, W> {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
|
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use super::DecodeError as SerdeDecodeError;
|
use super::DecodeError as SerdeDecodeError;
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
de::{Decode, Decoder},
|
de::{read::Reader, Decode, Decoder},
|
||||||
error::DecodeError,
|
error::DecodeError,
|
||||||
};
|
};
|
||||||
use serde_incl::de::*;
|
use serde_incl::de::*;
|
||||||
|
|
@ -24,6 +24,33 @@ where
|
||||||
Ok((result, bytes_read))
|
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) struct SerdeDecoder<'a, DE: Decoder> {
|
||||||
pub(crate) de: &'a mut DE,
|
pub(crate) de: &'a mut DE,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use super::EncodeError as SerdeEncodeError;
|
use super::EncodeError as SerdeEncodeError;
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
enc::{Encode, Encoder},
|
enc::{write::Writer, Encode, Encoder},
|
||||||
error::EncodeError,
|
error::EncodeError,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
|
|
@ -23,7 +23,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode a `serde` `Serialize` type into a given byte slice with the bincode algorithm
|
/// 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
|
where
|
||||||
T: Serialize,
|
T: Serialize,
|
||||||
C: Config,
|
C: Config,
|
||||||
|
|
@ -35,6 +35,40 @@ where
|
||||||
Ok(encoder.into_writer().bytes_written())
|
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) struct SerdeEncoder<'a, ENC: Encoder> {
|
||||||
pub(super) enc: &'a mut ENC,
|
pub(super) enc: &'a mut ENC,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -133,9 +133,9 @@ pub fn decode_from_slice<'a, D: de::BorrowDecode<'a>, C: Config>(
|
||||||
/// [config]: config/index.html
|
/// [config]: config/index.html
|
||||||
pub fn decode_from_reader<D: de::Decode, R: Reader, C: Config>(
|
pub fn decode_from_reader<D: de::Decode, R: Reader, C: Config>(
|
||||||
reader: R,
|
reader: R,
|
||||||
_config: C,
|
config: C,
|
||||||
) -> Result<D, error::DecodeError> {
|
) -> 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)
|
D::decode(&mut decoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,8 +61,8 @@ fn test_serialize_deserialize_borrowed_data() {
|
||||||
];
|
];
|
||||||
|
|
||||||
let mut result = [0u8; 20];
|
let mut result = [0u8; 20];
|
||||||
let len =
|
let len = bincode::serde::encode_into_slice(&input, &mut result, bincode::config::standard())
|
||||||
bincode::serde::encode_to_slice(&input, &mut result, bincode::config::standard()).unwrap();
|
.unwrap();
|
||||||
let result = &result[..len];
|
let result = &result[..len];
|
||||||
assert_eq!(result, expected);
|
assert_eq!(result, expected);
|
||||||
|
|
||||||
|
|
@ -107,8 +107,8 @@ fn test_serialize_deserialize_owned_data() {
|
||||||
];
|
];
|
||||||
|
|
||||||
let mut result = [0u8; 20];
|
let mut result = [0u8; 20];
|
||||||
let len =
|
let len = bincode::serde::encode_into_slice(&input, &mut result, bincode::config::standard())
|
||||||
bincode::serde::encode_to_slice(&input, &mut result, bincode::config::standard()).unwrap();
|
.unwrap();
|
||||||
let result = &result[..len];
|
let result = &result[..len];
|
||||||
assert_eq!(result, expected);
|
assert_eq!(result, expected);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue