From 3db24934d6f2ef074e58eb9b116228327a963c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Kundr=C3=A1t?= <49568499+i123iu@users.noreply.github.com> Date: Mon, 29 Jul 2024 08:48:49 +0200 Subject: [PATCH] fix issue 1586 (#2725) --- packages/core/src/hotreload_utils.rs | 16 ++++++++++++++++ packages/core/src/nodes.rs | 1 + 2 files changed, 17 insertions(+) diff --git a/packages/core/src/hotreload_utils.rs b/packages/core/src/hotreload_utils.rs index 43dde887c..86e901d65 100644 --- a/packages/core/src/hotreload_utils.rs +++ b/packages/core/src/hotreload_utils.rs @@ -71,3 +71,19 @@ pub enum FmtSegment { id: usize, }, } + +/// A helper function for deserializing to a `&'static str`. +/// Parses escaped characters (like `"\n"` or `"\t"`), +/// which cannot be done in-place using the default serde json deserializer. +/// +/// **Leaks the memory** in order to achieve a `'static` reference. +/// +/// Solves [this issue](https://github.com/DioxusLabs/dioxus/issues/1586). +#[doc(hidden)] +pub fn static_str_deserializer<'de, D>(deserializer: D) -> Result<&'static str, D::Error> +where + D: serde::de::Deserializer<'de>, +{ + ::deserialize(deserializer) + .map(|s| Box::leak(Box::new(s)) as &'static str) +} diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index fee79e36a..2e8048d6d 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -523,6 +523,7 @@ pub enum TemplateNode { /// This template node is just a piece of static text Text { /// The actual text + #[serde(deserialize_with = "crate::hotreload_utils::static_str_deserializer")] text: &'static str, },