From ebb9f87568b90c9971375aec080508f61dfe554c Mon Sep 17 00:00:00 2001 From: Kai Schmidt Date: Sun, 24 Nov 2024 12:39:50 -0800 Subject: [PATCH] fix (de)serialization of scalar and lists of characters --- src/array.rs | 11 +++++++++-- src/tree.rs | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/array.rs b/src/array.rs index 996a2b86d..d5fb1c45b 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1321,8 +1321,8 @@ impl<'a, T: fmt::Display> fmt::Display for FormatShape<'a, T> { deserialize = "T: ArrayValueSer + Deserialize<'de>" ))] enum ArrayRep { - Scalar(T::Scalar), List(T::Collection), + Scalar(T::Scalar), Map(Shape, Value, T::Collection), Metaless(Shape, T::Collection), Full(Shape, T::Collection, ArrayMeta), @@ -1377,7 +1377,7 @@ impl From> for ArrayRep { } } match arr.rank() { - 0 => ArrayRep::Scalar(arr.data[0].clone().into()), + 0 if !T::no_scalar() => ArrayRep::Scalar(arr.data[0].clone().into()), 1 => ArrayRep::List(T::make_collection(arr.data)), _ => ArrayRep::Metaless(arr.shape, T::make_collection(arr.data)), } @@ -1389,6 +1389,10 @@ trait ArrayValueSer: ArrayValue + fmt::Debug { type Collection: Serialize + DeserializeOwned + fmt::Debug; fn make_collection(data: CowSlice) -> Self::Collection; fn make_data(collection: Self::Collection) -> CowSlice; + /// Do not use the [`ArrayRep::Scalar`] variant + fn no_scalar() -> bool { + false + } } macro_rules! array_value_ser { @@ -1430,6 +1434,9 @@ impl ArrayValueSer for char { fn make_data(collection: Self::Collection) -> CowSlice { collection.chars().collect() } + fn no_scalar() -> bool { + true + } } #[derive(Debug, Clone, Copy, Serialize, Deserialize)] diff --git a/src/tree.rs b/src/tree.rs index 07e1f59e4..b2ba63cb6 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -39,8 +39,6 @@ node!( NoInline(inner(Box)), TrackCaller(inner(Box)), (#[serde(untagged)] rep), - Run(nodes(EcoVec)), - (#[serde(untagged)] rep), Push(val(Value)), (#[serde(untagged)] rep), Prim(prim(Primitive), span(usize)), @@ -52,6 +50,8 @@ node!( ImplMod(prim(ImplPrimitive), args(Ops), span(usize)), (#[serde(untagged)] rep), Call(func(Function), span(usize)), + (#[serde(untagged)] rep), + Run(nodes(EcoVec)), ); /// A node with a signature