Skip to content

Commit

Permalink
Introduce DynDeserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Nov 30, 2024
1 parent 5da4e62 commit b4d8818
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions merde_core/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,32 @@ pub trait DeserializeOwned: Sized + IntoStatic {
) -> impl Future<Output = Result<Self, MerdeError<'s>>>;
}

pub trait DynDeserialize {
fn dyn_deserialize<'de>(
de: &'de mut dyn DynDeserializer<'de>,
) -> Result<Box<Self>, MerdeError<'static>>
where
Self: Sized;
}

impl dyn DynDeserialize {
fn _assert_dyn_safe(_: Box<dyn DynDeserialize>) {}
}

impl<T> DynDeserialize for T
where
T: DeserializeOwned,
{
fn dyn_deserialize<'de>(
de: &'de mut dyn DynDeserializer<'de>,
) -> Result<Box<Self>, MerdeError<'static>> {
match T::deserialize_owned(de).run_sync_with_metastack() {
Ok(value) => Ok(Box::new(value)),
Err(e) => Err(e.into_static()),
}
}
}

impl<T> DeserializeOwned for T
where
T: IntoStatic,
Expand Down
1 change: 1 addition & 0 deletions merde_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub use deserialize::DeserOpinions;
pub use deserialize::Deserialize;
pub use deserialize::DeserializeOwned;
pub use deserialize::Deserializer;
pub use deserialize::DynDeserialize;
pub use deserialize::DynDeserializer;
pub use deserialize::DynDeserializerExt;
pub use deserialize::FieldSlot;
Expand Down

0 comments on commit b4d8818

Please sign in to comment.