Skip to content

Commit

Permalink
Use DynSerialize in merde_json
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Nov 30, 2024
1 parent 253d0da commit d5deafd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
12 changes: 7 additions & 5 deletions merde_core/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ where
}

pub trait DynSerializerExt {
fn serialize<'s, T: Serialize>(&'s mut self, t: &'s T) -> Result<(), MerdeError<'static>>;
fn serialize<T: Serialize>(&mut self, t: &T) -> Result<(), MerdeError<'static>>;
fn dyn_serialize(&mut self, t: &mut dyn DynSerialize) -> Result<(), MerdeError<'static>>;
}

impl<S> DynSerializerExt for S
where
S: DynSerializer,
{
fn serialize<'fut, T: Serialize>(
&'fut mut self,
t: &'fut T,
) -> Result<(), MerdeError<'static>> {
fn serialize<T: Serialize>(&mut self, t: &T) -> Result<(), MerdeError<'static>> {
T::serialize(t, self).run_sync_with_metastack()
}

fn dyn_serialize(&mut self, t: &mut dyn DynSerialize) -> Result<(), MerdeError<'static>> {
DynSerialize::dyn_serialize(t, self).run_sync_with_metastack()
}
}

pub trait Serialize {
Expand Down
19 changes: 8 additions & 11 deletions merde_json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub use serialize::{JsonSerializer, JsonSerializerWriter};
mod jiter_lite;

use merde_core::{
Deserialize, DeserializeOwned, DynDeserializerExt, DynSerializerExt, MerdeError, MetastackExt,
Serialize,
Deserialize, DeserializeOwned, DynDeserializerExt, DynSerialize, DynSerializerExt, MerdeError,
MetastackExt, Serialize,
};

/// Deserialize an instance of type `T` from a string of JSON text.
Expand Down Expand Up @@ -71,14 +71,11 @@ pub fn to_vec<T: Serialize>(value: &T) -> Result<Vec<u8>, MerdeError<'static>> {
}

/// Serialize the given data structure as JSON into the I/O stream.
pub fn to_writer<W, T>(
mut writer: impl std::io::Write,
value: &T,
) -> Result<(), MerdeError<'static>>
where
T: Serialize,
{
let mut s = JsonSerializer::from_writer(&mut writer);
s.serialize(value)?;
pub fn to_writer(
writer: &mut dyn std::io::Write,
value: &mut dyn DynSerialize,
) -> Result<(), MerdeError<'static>> {
let mut s = JsonSerializer::from_writer(writer);
s.dyn_serialize(value)?;
Ok(())
}
4 changes: 1 addition & 3 deletions merde_json/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ where

impl<'w> JsonSerializer<SyncWriteWrapper<'w>> {
/// Makes a json serializer that writes to a std::io::Write
pub fn from_writer<SW: std::io::Write + 'w>(
w: &'w mut SW,
) -> JsonSerializer<SyncWriteWrapper<'w>> {
pub fn from_writer(w: &'w mut dyn std::io::Write) -> JsonSerializer<SyncWriteWrapper<'w>> {
JsonSerializer::new(SyncWriteWrapper(w))
}
}
Expand Down

0 comments on commit d5deafd

Please sign in to comment.