Skip to content

Commit

Permalink
made the serde functions consistent with the base bincode functions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKoenders authored Jan 19, 2022
1 parent 983680c commit a041810
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 19 deletions.
36 changes: 26 additions & 10 deletions src/features/impl_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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> {
Expand Down
29 changes: 28 additions & 1 deletion src/features/serde/de_owned.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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,
}
Expand Down
38 changes: 36 additions & 2 deletions src/features/serde/ser.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -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,
Expand All @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit a041810

Please sign in to comment.