Skip to content

Commit

Permalink
refactor: make Serializer return a normal Vec<u8>
Browse files Browse the repository at this point in the history
  • Loading branch information
threadexio committed Dec 29, 2023
1 parent 7f0e09e commit 40529a8
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 220 deletions.
18 changes: 3 additions & 15 deletions channels-serdes/src/bincode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::vec::Vec;

use bincode::Options;

use crate::{Deserializer, PayloadBuffer, Serializer};
use crate::{Deserializer, Serializer};

fn default_bincode_config() -> impl Options {
bincode::options()
Expand Down Expand Up @@ -43,20 +43,8 @@ where
{
type Error = bincode::Error;

fn serialize(
&mut self,

t: &T,
) -> Result<PayloadBuffer, Self::Error> {
let mut buf = PayloadBuffer::new();
let bincode = &mut default_bincode_config();

let size_hint = bincode.serialized_size(t)?;
if let Ok(size_hint) = usize::try_from(size_hint) {
buf.reserve(size_hint);
}

bincode.serialize_into(&mut buf, t).map(|_| buf)
fn serialize(&mut self, t: &T) -> Result<Vec<u8>, Self::Error> {
default_bincode_config().serialize(t)
}
}

Expand Down
123 changes: 0 additions & 123 deletions channels-serdes/src/buf.rs

This file was deleted.

13 changes: 5 additions & 8 deletions channels-serdes/src/cbor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::vec::Vec;

use crate::{Deserializer, PayloadBuffer, Serializer};
use crate::{Deserializer, Serializer};

/// The [`mod@ciborium`] serializer which automatically works with all
/// types that implement [`serde::Serialize`] and [`serde::Deserialize`].
Expand All @@ -20,13 +20,10 @@ where
{
type Error = ciborium::ser::Error<std::io::Error>;

fn serialize(
&mut self,
t: &T,
) -> Result<PayloadBuffer, Self::Error> {
let mut buf = PayloadBuffer::new();

ciborium::into_writer(t, &mut buf).map(|_| buf)
fn serialize(&mut self, t: &T) -> Result<Vec<u8>, Self::Error> {
let mut buf = Vec::new();
ciborium::into_writer(t, &mut buf)?;
Ok(buf)
}
}

Expand Down
14 changes: 3 additions & 11 deletions channels-serdes/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::vec::Vec;

use crate::{Deserializer, PayloadBuffer, Serializer};
use crate::{Deserializer, Serializer};

/// The [`mod@serde_json`] serializer which automatically works with all
/// types that implement [`serde::Serialize`] and [`serde::Deserialize`].
Expand All @@ -25,16 +25,8 @@ where
{
type Error = serde_json::Error;

fn serialize(
&mut self,
t: &T,
) -> Result<PayloadBuffer, Self::Error> {
let mut buf = PayloadBuffer::new();

let data = serde_json::to_vec(t)?;
buf.put_slice(&data);

Ok(buf)
fn serialize(&mut self, t: &T) -> Result<Vec<u8>, Self::Error> {
serde_json::to_vec(t)
}
}

Expand Down
9 changes: 1 addition & 8 deletions channels-serdes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ extern crate alloc;

use alloc::vec::Vec;

mod buf;

pub use self::buf::PayloadBuffer;

/// The [`Serializer`] trait allows converting a type `T` to safe-to-transport
/// byte sequences.
///
Expand All @@ -41,10 +37,7 @@ pub trait Serializer<T> {
type Error;

/// Serialize `t` to a buffer.
fn serialize(
&mut self,
t: &T,
) -> Result<PayloadBuffer, Self::Error>;
fn serialize(&mut self, t: &T) -> Result<Vec<u8>, Self::Error>;
}

/// The [`Deserializer`] trait allows converting a byte slice to a type `T`.
Expand Down
Loading

0 comments on commit 40529a8

Please sign in to comment.