Skip to content

Commit

Permalink
feat: Implement Deserialize and IntoStatic for Box<T> (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime authored Nov 20, 2024
1 parent 7db3a0d commit 87c059a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions merde/examples/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,7 +30,7 @@ enum ExampleEvent<'s> {
MouseDown(MouseDown),
TextInput(TextInput<'s>),
StringStuff(StringStuff<'s>),
Emergency(Emergency),
Emergency(Box<Emergency>),
}

merde::derive! {
Expand Down
10 changes: 10 additions & 0 deletions merde_core/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ impl<'s> Deserialize<'s> for Cow<'s, str> {
}
}

impl<'s, T: Deserialize<'s>> Deserialize<'s> for Box<T> {
async fn deserialize<D>(de: &mut D) -> Result<Self, D::Error<'s>>
where
D: Deserializer<'s> + ?Sized,
{
let value: T = de.t().await?;
Ok(Box::new(value))
}
}

impl<'s, T: Deserialize<'s>> Deserialize<'s> for Option<T> {
async fn deserialize<D>(de: &mut D) -> Result<Self, D::Error<'s>>
where
Expand Down
8 changes: 8 additions & 0 deletions merde_core/src/into_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: IntoStatic> IntoStatic for Box<T> {
type Output = Box<T::Output>;

fn into_static(self) -> Self::Output {
Box::new((*self).into_static())
}
}

impl<T: IntoStatic> IntoStatic for Option<T> {
type Output = Option<T::Output>;

Expand Down

0 comments on commit 87c059a

Please sign in to comment.