From 87c059a59c9b6b908ed6061f101d7cdee47edcb5 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Wed, 20 Nov 2024 13:51:48 +0100 Subject: [PATCH] feat: Implement Deserialize and IntoStatic for `Box` (#107) --- merde/examples/enums.rs | 4 ++-- merde_core/src/deserialize.rs | 10 ++++++++++ merde_core/src/into_static.rs | 8 ++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/merde/examples/enums.rs b/merde/examples/enums.rs index 50d2c33..9822b19 100644 --- a/merde/examples/enums.rs +++ b/merde/examples/enums.rs @@ -10,7 +10,7 @@ fn main() { text: "Hello".into(), }), ExampleEvent::StringStuff(StringStuff("Some string".into())), - ExampleEvent::Emergency(Emergency::NoPizzaLeft), + ExampleEvent::Emergency(Box::new(Emergency::NoPizzaLeft)), ]; for event in events { @@ -30,7 +30,7 @@ enum ExampleEvent<'s> { MouseDown(MouseDown), TextInput(TextInput<'s>), StringStuff(StringStuff<'s>), - Emergency(Emergency), + Emergency(Box), } merde::derive! { diff --git a/merde_core/src/deserialize.rs b/merde_core/src/deserialize.rs index 6790074..38f4bbb 100644 --- a/merde_core/src/deserialize.rs +++ b/merde_core/src/deserialize.rs @@ -409,6 +409,16 @@ impl<'s> Deserialize<'s> for Cow<'s, str> { } } +impl<'s, T: Deserialize<'s>> Deserialize<'s> for Box { + async fn deserialize(de: &mut D) -> Result> + where + D: Deserializer<'s> + ?Sized, + { + let value: T = de.t().await?; + Ok(Box::new(value)) + } +} + impl<'s, T: Deserialize<'s>> Deserialize<'s> for Option { async fn deserialize(de: &mut D) -> Result> where diff --git a/merde_core/src/into_static.rs b/merde_core/src/into_static.rs index 0358398..3a87f38 100644 --- a/merde_core/src/into_static.rs +++ b/merde_core/src/into_static.rs @@ -90,6 +90,14 @@ impl_into_static_passthru!( String, u128, u64, u32, u16, u8, i128, i64, i32, i16, i8, bool, char, usize, isize, f32, f64 ); +impl IntoStatic for Box { + type Output = Box; + + fn into_static(self) -> Self::Output { + Box::new((*self).into_static()) + } +} + impl IntoStatic for Option { type Output = Option;