fix serialization

This commit is contained in:
Evan Almloff committed 2022-12-19 11:48:28 -06:00
1 parent fbc89a7088
commit 96ce704f8e
5 files changed
+50 -7

No files matched your search

+38 -2
View File
@@ -108,6 +108,10 @@ pub struct Template<'a> {
/// The name of the template. This must be unique across your entire program for template diffing to work properly
///
/// If two templates have the same name, it's likely that Dioxus will panic when diffing.
#[cfg_attr(
feature = "serialize",
serde(deserialize_with = "deserialize_string_leaky")
)]
pub name: &'a str,
/// The list of template nodes that make up the template
@@ -120,17 +124,49 @@ pub struct Template<'a> {
///
/// These will be one segment shorter than the path sent to the renderer since those paths are relative to the
/// topmost element, not the `roots` field.
#[cfg_attr(feature = "serialize", serde(deserialize_with = "deserialize_leaky"))]
#[cfg_attr(
feature = "serialize",
serde(deserialize_with = "deserialize_bytes_leaky")
)]
pub node_paths: &'a [&'a [u8]],
/// The paths of each dynamic attribute relative to the root of the template
///
/// These will be one segment shorter than the path sent to the renderer since those paths are relative to the
/// topmost element, not the `roots` field.
#[cfg_attr(feature = "serialize", serde(deserialize_with = "deserialize_leaky"))]
#[cfg_attr(
feature = "serialize",
serde(deserialize_with = "deserialize_bytes_leaky")
)]
pub attr_paths: &'a [&'a [u8]],
}
#[cfg(feature = "serialize")]
fn deserialize_string_leaky<'a, 'de, D>(deserializer: D) -> Result<&'a str, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let deserialized = String::deserialize(deserializer)?;
Ok(&*Box::leak(deserialized.into_boxed_str()))
}
#[cfg(feature = "serialize")]
fn deserialize_bytes_leaky<'a, 'de, D>(deserializer: D) -> Result<&'a [&'a [u8]], D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let deserialized = Vec::<Vec<u8>>::deserialize(deserializer)?;
let deserialized = deserialized
.into_iter()
.map(|v| &*Box::leak(v.into_boxed_slice()))
.collect::<Vec<_>>();
Ok(&*Box::leak(deserialized.into_boxed_slice()))
}
#[cfg(feature = "serialize")]
fn deserialize_leaky<'a, 'de, T: serde::Deserialize<'de>, D>(
deserializer: D,
+1
View File
@@ -458,6 +458,7 @@ impl VirtualDom {
/// The caller must ensure that the template refrences the same dynamic attributes and nodes as the original template.
pub fn replace_template(&mut self, template: Template<'static>) {
self.templates.insert(template.name, template);
panic!("{:?}", self.templates);
// iterating a slab is very inefficient, but this is a rare operation that will only happen during development so it's fine
for (_, scope) in &self.scopes {
if let Some(RenderReturn::Sync(Ok(sync))) = scope.try_root_node() {