Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify lifetimes #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,24 @@ where
}

/// Deserialization implementation for BCS
struct Deserializer<R> {
pub struct Deserializer<R> {
input: R,
max_remaining_depth: usize,
}

impl<'de, R: Read> Deserializer<TeeReader<'de, R>> {
fn from_reader(input: &'de mut R, max_remaining_depth: usize) -> Self {
impl<R: Read> Deserializer<TeeReader<R>> {
fn from_reader(input: R, max_remaining_depth: usize) -> Self {
Deserializer {
input: TeeReader::new(input),
max_remaining_depth,
}
}
}

impl<'de> Deserializer<&'de [u8]> {
impl<R> Deserializer<R> {
/// Creates a new `Deserializer` which will be deserializing the provided
/// input.
fn new(input: &'de [u8], max_remaining_depth: usize) -> Self {
pub fn new(input: R, max_remaining_depth: usize) -> Self {
Deserializer {
input,
max_remaining_depth,
Expand All @@ -164,24 +164,24 @@ impl<'de> Deserializer<&'de [u8]> {
}

/// A reader that can optionally capture all bytes from an underlying [`Read`]er
struct TeeReader<'de, R> {
struct TeeReader<R> {
/// the underlying reader
reader: &'de mut R,
reader: R,
/// If non-empty, all bytes read from the underlying reader will be captured in the last entry here.
captured_keys: Vec<Vec<u8>>,
}

impl<'de, R> TeeReader<'de, R> {
impl<R> TeeReader<R> {
/// Wraps the provided reader in a new [`TeeReader`].
pub fn new(reader: &'de mut R) -> Self {
pub fn new(reader: R) -> Self {
Self {
reader,
captured_keys: Vec::new(),
}
}
}

impl<'de, R: Read> Read for TeeReader<'de, R> {
impl<R: Read> Read for TeeReader<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let bytes_read = self.reader.read(buf)?;
if let Some(buffer) = self.captured_keys.last_mut() {
Expand Down Expand Up @@ -289,7 +289,7 @@ trait BcsDeserializer<'de> {
}
}

impl<'de, R: Read> Deserializer<TeeReader<'de, R>> {
impl<R: Read> Deserializer<TeeReader<R>> {
fn parse_vec(&mut self) -> Result<Vec<u8>> {
let len = self.parse_length()?;
let mut output = vec![0; len];
Expand All @@ -303,7 +303,7 @@ impl<'de, R: Read> Deserializer<TeeReader<'de, R>> {
}
}

impl<'de, R: Read> BcsDeserializer<'de> for Deserializer<TeeReader<'de, R>> {
impl<'de, R: Read> BcsDeserializer<'de> for Deserializer<TeeReader<R>> {
type MaybeBorrowedBytes = Vec<u8>;

fn fill_slice(&mut self, slice: &mut [u8]) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ pub const MAX_CONTAINER_DEPTH: usize = 500;

pub use de::{
from_bytes, from_bytes_seed, from_bytes_seed_with_limit, from_bytes_with_limit, from_reader,
from_reader_seed, from_reader_seed_with_limit, from_reader_with_limit,
from_reader_seed, from_reader_seed_with_limit, from_reader_with_limit, Deserializer,
};
pub use error::{Error, Result};
pub use ser::{
is_human_readable, serialize_into, serialize_into_with_limit, serialized_size,
serialized_size_with_limit, to_bytes, to_bytes_with_limit,
serialized_size_with_limit, to_bytes, to_bytes_with_limit, Serializer,
};
87 changes: 44 additions & 43 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ where
}

/// Same as `to_bytes` but write directly into an `std::io::Write` object.
pub fn serialize_into<W, T>(write: &mut W, value: &T) -> Result<()>
pub fn serialize_into<T>(write: impl std::io::Write, value: &T) -> Result<()>
where
W: ?Sized + std::io::Write,
T: ?Sized + Serialize,
{
let serializer = Serializer::new(write, crate::MAX_CONTAINER_DEPTH);
Expand All @@ -81,9 +80,12 @@ where

/// Same as `serialize_into` but use `limit` as max container depth instead of MAX_CONTAINER_DEPTH
/// Note that `limit` has to be lower than MAX_CONTAINER_DEPTH
pub fn serialize_into_with_limit<W, T>(write: &mut W, value: &T, limit: usize) -> Result<()>
pub fn serialize_into_with_limit<T>(
write: impl std::io::Write,
value: &T,
limit: usize,
) -> Result<()>
where
W: ?Sized + std::io::Write,
T: ?Sized + Serialize,
{
if limit > crate::MAX_CONTAINER_DEPTH {
Expand Down Expand Up @@ -140,17 +142,17 @@ pub fn is_human_readable() -> bool {
}

/// Serialization implementation for BCS
struct Serializer<'a, W: ?Sized> {
output: &'a mut W,
pub struct Serializer<W> {
output: W,
max_remaining_depth: usize,
}

impl<'a, W> Serializer<'a, W>
impl<W> Serializer<W>
where
W: ?Sized + std::io::Write,
W: std::io::Write,
{
/// Creates a new `Serializer` which will emit BCS.
fn new(output: &'a mut W, max_remaining_depth: usize) -> Self {
pub fn new(output: W, max_remaining_depth: usize) -> Self {
Self {
output,
max_remaining_depth,
Expand Down Expand Up @@ -190,17 +192,17 @@ where
}
}

impl<'a, W> ser::Serializer for Serializer<'a, W>
impl<W> ser::Serializer for Serializer<W>
where
W: ?Sized + std::io::Write,
W: std::io::Write,
{
type Ok = ();
type Error = Error;
type SerializeSeq = Self;
type SerializeTuple = Self;
type SerializeTupleStruct = Self;
type SerializeTupleVariant = Self;
type SerializeMap = MapSerializer<'a, W>;
type SerializeMap = MapSerializer<W>;
type SerializeStruct = Self;
type SerializeStructVariant = Self;

Expand Down Expand Up @@ -228,27 +230,27 @@ where
self.serialize_u128(v as u128)
}

fn serialize_u8(self, v: u8) -> Result<()> {
fn serialize_u8(mut self, v: u8) -> Result<()> {
self.output.write_all(&[v])?;
Ok(())
}

fn serialize_u16(self, v: u16) -> Result<()> {
fn serialize_u16(mut self, v: u16) -> Result<()> {
self.output.write_all(&v.to_le_bytes())?;
Ok(())
}

fn serialize_u32(self, v: u32) -> Result<()> {
fn serialize_u32(mut self, v: u32) -> Result<()> {
self.output.write_all(&v.to_le_bytes())?;
Ok(())
}

fn serialize_u64(self, v: u64) -> Result<()> {
fn serialize_u64(mut self, v: u64) -> Result<()> {
self.output.write_all(&v.to_le_bytes())?;
Ok(())
}

fn serialize_u128(self, v: u128) -> Result<()> {
fn serialize_u128(mut self, v: u128) -> Result<()> {
self.output.write_all(&v.to_le_bytes())?;
Ok(())
}
Expand Down Expand Up @@ -283,7 +285,7 @@ where
}

// A present optional is represented as `01` followed by the serialized value
fn serialize_some<T>(self, value: &T) -> Result<()>
fn serialize_some<T>(mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
Expand Down Expand Up @@ -403,9 +405,9 @@ where
}
}

impl<'a, W> ser::SerializeSeq for Serializer<'a, W>
impl<W> ser::SerializeSeq for Serializer<W>
where
W: ?Sized + std::io::Write,
W: std::io::Write,
{
type Ok = ();
type Error = Error;
Expand All @@ -414,17 +416,17 @@ where
where
T: ?Sized + Serialize,
{
value.serialize(Serializer::new(self.output, self.max_remaining_depth))
value.serialize(Serializer::new(&mut self.output, self.max_remaining_depth))
}

fn end(self) -> Result<()> {
Ok(())
}
}

impl<'a, W> ser::SerializeTuple for Serializer<'a, W>
impl<W> ser::SerializeTuple for Serializer<W>
where
W: ?Sized + std::io::Write,
W: std::io::Write,
{
type Ok = ();
type Error = Error;
Expand All @@ -433,17 +435,17 @@ where
where
T: ?Sized + Serialize,
{
value.serialize(Serializer::new(self.output, self.max_remaining_depth))
value.serialize(Serializer::new(&mut self.output, self.max_remaining_depth))
}

fn end(self) -> Result<()> {
Ok(())
}
}

impl<'a, W> ser::SerializeTupleStruct for Serializer<'a, W>
impl<W> ser::SerializeTupleStruct for Serializer<W>
where
W: ?Sized + std::io::Write,
W: std::io::Write,
{
type Ok = ();
type Error = Error;
Expand All @@ -452,17 +454,17 @@ where
where
T: ?Sized + Serialize,
{
value.serialize(Serializer::new(self.output, self.max_remaining_depth))
value.serialize(Serializer::new(&mut self.output, self.max_remaining_depth))
}

fn end(self) -> Result<()> {
Ok(())
}
}

impl<'a, W> ser::SerializeTupleVariant for Serializer<'a, W>
impl<W> ser::SerializeTupleVariant for Serializer<W>
where
W: ?Sized + std::io::Write,
W: std::io::Write,
{
type Ok = ();
type Error = Error;
Expand All @@ -471,23 +473,22 @@ where
where
T: ?Sized + Serialize,
{
value.serialize(Serializer::new(self.output, self.max_remaining_depth))
value.serialize(Serializer::new(&mut self.output, self.max_remaining_depth))
}

fn end(self) -> Result<()> {
Ok(())
}
}

#[doc(hidden)]
struct MapSerializer<'a, W: ?Sized> {
serializer: Serializer<'a, W>,
pub struct MapSerializer<W> {
serializer: Serializer<W>,
entries: Vec<(Vec<u8>, Vec<u8>)>,
next_key: Option<Vec<u8>>,
}

impl<'a, W: ?Sized> MapSerializer<'a, W> {
fn new(serializer: Serializer<'a, W>) -> Self {
impl<W> MapSerializer<W> {
fn new(serializer: Serializer<W>) -> Self {
MapSerializer {
serializer,
entries: Vec::new(),
Expand All @@ -496,9 +497,9 @@ impl<'a, W: ?Sized> MapSerializer<'a, W> {
}
}

impl<'a, W> ser::SerializeMap for MapSerializer<'a, W>
impl<W> ser::SerializeMap for MapSerializer<W>
where
W: ?Sized + std::io::Write,
W: std::io::Write,
{
type Ok = ();
type Error = Error;
Expand Down Expand Up @@ -557,9 +558,9 @@ where
}
}

impl<'a, W> ser::SerializeStruct for Serializer<'a, W>
impl<W> ser::SerializeStruct for Serializer<W>
where
W: ?Sized + std::io::Write,
W: std::io::Write,
{
type Ok = ();
type Error = Error;
Expand All @@ -568,17 +569,17 @@ where
where
T: ?Sized + Serialize,
{
value.serialize(Serializer::new(self.output, self.max_remaining_depth))
value.serialize(Serializer::new(&mut self.output, self.max_remaining_depth))
}

fn end(self) -> Result<()> {
Ok(())
}
}

impl<'a, W> ser::SerializeStructVariant for Serializer<'a, W>
impl<W> ser::SerializeStructVariant for Serializer<W>
where
W: ?Sized + std::io::Write,
W: std::io::Write,
{
type Ok = ();
type Error = Error;
Expand All @@ -587,7 +588,7 @@ where
where
T: ?Sized + Serialize,
{
value.serialize(Serializer::new(self.output, self.max_remaining_depth))
value.serialize(Serializer::new(&mut self.output, self.max_remaining_depth))
}

fn end(self) -> Result<()> {
Expand Down
Loading