Skip to content

Commit

Permalink
Remove serde-persistent-deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Oct 30, 2024
1 parent b43370e commit e0cf3e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 41 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ edition = "2021"
[features]
default = ["std", "derive"]
std = ["alloc", "serde?/std"]
alloc = ["serde?/alloc", "serde-persistent-deserializer?/alloc"]
alloc = ["serde?/alloc"]
derive = ["bincode_derive"]
serde = ["dep:serde", "serde-persistent-deserializer"]
serde = ["dep:serde"]

[dependencies]
bincode_derive = { path = "derive", version = "2.0.0-rc.3", optional = true }
serde = { version = "1.0", default-features = false, optional = true }
serde-persistent-deserializer = { version = "0.2", default-features = false, optional = true }
unty = "0.0.3"

# Used for tests
Expand Down
36 changes: 15 additions & 21 deletions src/features/serde/de_borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@ use crate::{
};
use core::marker::PhantomData;
use serde::de::*;
use serde_persistent_deserializer::AsTransientDeserializer;

/// Serde decoder encapsulating a borrowed reader.
pub struct BorrowedSerdeDecoder<'de, DE: BorrowDecoder<'de>> {
pub(super) de: DE,
pub(super) pd: PhantomData<&'de ()>,
}

impl<'de, DE: BorrowDecoder<'de>> BorrowedSerdeDecoder<'de, DE> {
/// Return a type implementing `serde::Deserializer`.
pub fn as_deserializer<'a>(
&'a mut self,
) -> impl serde::Deserializer<'de, Error = DecodeError> + 'a {
SerdeDecoder {
de: &mut self.de,
pd: PhantomData,
}
}
}

impl<'de, C: Config> BorrowedSerdeDecoder<'de, DecoderImpl<SliceReader<'de>, C>> {
/// Creates the decoder from a borrowed slice.
pub fn from_slice(
Expand All @@ -32,23 +43,6 @@ impl<'de, C: Config> BorrowedSerdeDecoder<'de, DecoderImpl<SliceReader<'de>, C>>
}
}

impl<'de, DE: BorrowDecoder<'de>> AsTransientDeserializer<'de> for BorrowedSerdeDecoder<'de, DE> {
type Error = DecodeError;

#[inline]
fn as_transient_deserializer<'a>(&'a mut self) -> impl Deserializer<'de, Error = Self::Error> {
SerdeDecoder {
de: &mut self.de,
pd: PhantomData,
}
}

#[inline]
fn is_human_readable(&self) -> bool {
false
}
}

/// Attempt to decode a given type `D` from the given slice. Returns the decoded output and the amount of bytes read.
///
/// See the [config](../config/index.html) module for more information on configurations.
Expand All @@ -62,7 +56,7 @@ where
{
let mut serde_decoder =
BorrowedSerdeDecoder::<DecoderImpl<SliceReader<'de>, C>>::from_slice(slice, config);
let result = D::deserialize(serde_decoder.as_transient_deserializer())?;
let result = D::deserialize(serde_decoder.as_deserializer())?;
let bytes_read = slice.len() - serde_decoder.de.borrow_reader().slice.len();
Ok((result, bytes_read))
}
Expand All @@ -76,7 +70,7 @@ where
{
let mut serde_decoder =
BorrowedSerdeDecoder::<DecoderImpl<SliceReader<'de>, C>>::from_slice(slice, config);
T::deserialize(serde_decoder.as_transient_deserializer())
T::deserialize(serde_decoder.as_deserializer())
}

/// Decode a borrowed type from the given slice using a seed. Some parts of the decoded type are expected to be referring to the given slice
Expand All @@ -91,7 +85,7 @@ where
{
let mut serde_decoder =
BorrowedSerdeDecoder::<DecoderImpl<SliceReader<'de>, C>>::from_slice(slice, config);
seed.deserialize(serde_decoder.as_transient_deserializer())
seed.deserialize(serde_decoder.as_deserializer())
}

pub(super) struct SerdeDecoder<'a, 'de, DE: BorrowDecoder<'de>> {
Expand Down
28 changes: 11 additions & 17 deletions src/features/serde/de_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
IoReader,
};
use serde::de::*;
use serde_persistent_deserializer::AsTransientDeserializer;

use super::de_borrowed::borrow_decode_from_slice;

Expand All @@ -15,6 +14,15 @@ pub struct OwnedSerdeDecoder<DE: Decoder> {
pub(super) de: DE,
}

impl<DE: Decoder> OwnedSerdeDecoder<DE> {
/// Return a type implementing `serde::Deserializer`.
pub fn as_deserializer<'a>(
&'a mut self,
) -> impl for<'de> serde::Deserializer<'de, Error = DecodeError> + 'a {
SerdeDecoder { de: &mut self.de }
}
}

#[cfg(feature = "std")]
impl<'r, C: Config, R: std::io::Read> OwnedSerdeDecoder<DecoderImpl<IoReader<&'r mut R>, C>> {
/// Creates the decoder from an `std::io::Read` implementor.
Expand Down Expand Up @@ -42,20 +50,6 @@ impl<C: Config, R: Reader> OwnedSerdeDecoder<DecoderImpl<R, C>> {
}
}

impl<'de, DE: Decoder> AsTransientDeserializer<'de> for OwnedSerdeDecoder<DE> {
type Error = DecodeError;

#[inline]
fn as_transient_deserializer<'a>(&'a mut self) -> impl Deserializer<'de, Error = Self::Error> {
SerdeDecoder { de: &mut self.de }
}

#[inline]
fn is_human_readable(&self) -> bool {
false
}
}

/// Attempt to decode a given type `D` from the given slice. Returns the decoded output and the amount of bytes read.
///
/// Note that this does not work with borrowed types like `&str` or `&[u8]`. For that use [borrow_decode_from_slice].
Expand Down Expand Up @@ -85,7 +79,7 @@ pub fn decode_from_std_read<'r, D: DeserializeOwned, C: Config, R: std::io::Read
) -> Result<D, DecodeError> {
let mut serde_decoder =
OwnedSerdeDecoder::<DecoderImpl<IoReader<&'r mut R>, C>>::from_std_read(src, config);
D::deserialize(serde_decoder.as_transient_deserializer())
D::deserialize(serde_decoder.as_deserializer())
}

/// Attempt to decode a given type `D` from the given [Reader].
Expand All @@ -98,7 +92,7 @@ pub fn decode_from_reader<D: DeserializeOwned, R: Reader, C: Config>(
config: C,
) -> Result<D, DecodeError> {
let mut serde_decoder = OwnedSerdeDecoder::<DecoderImpl<R, C>>::from_reader(reader, config);
D::deserialize(serde_decoder.as_transient_deserializer())
D::deserialize(serde_decoder.as_deserializer())
}

pub(super) struct SerdeDecoder<'a, DE: Decoder> {
Expand Down

0 comments on commit e0cf3e6

Please sign in to comment.