Skip to content

Commit

Permalink
fix (de)serialization of scalar and lists of characters
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Nov 24, 2024
1 parent 25c9b06 commit ebb9f87
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,8 @@ impl<'a, T: fmt::Display> fmt::Display for FormatShape<'a, T> {
deserialize = "T: ArrayValueSer + Deserialize<'de>"
))]
enum ArrayRep<T: ArrayValueSer> {
Scalar(T::Scalar),
List(T::Collection),
Scalar(T::Scalar),
Map(Shape, Value, T::Collection),
Metaless(Shape, T::Collection),
Full(Shape, T::Collection, ArrayMeta),
Expand Down Expand Up @@ -1377,7 +1377,7 @@ impl<T: ArrayValueSer> From<Array<T>> for ArrayRep<T> {
}
}
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)),
}
Expand All @@ -1389,6 +1389,10 @@ trait ArrayValueSer: ArrayValue + fmt::Debug {
type Collection: Serialize + DeserializeOwned + fmt::Debug;
fn make_collection(data: CowSlice<Self>) -> Self::Collection;
fn make_data(collection: Self::Collection) -> CowSlice<Self>;
/// Do not use the [`ArrayRep::Scalar`] variant
fn no_scalar() -> bool {
false
}
}

macro_rules! array_value_ser {
Expand Down Expand Up @@ -1430,6 +1434,9 @@ impl ArrayValueSer for char {
fn make_data(collection: Self::Collection) -> CowSlice<Self> {
collection.chars().collect()
}
fn no_scalar() -> bool {
true
}
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
Expand Down
4 changes: 2 additions & 2 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ node!(
NoInline(inner(Box<Node>)),
TrackCaller(inner(Box<Node>)),
(#[serde(untagged)] rep),
Run(nodes(EcoVec<Node>)),
(#[serde(untagged)] rep),
Push(val(Value)),
(#[serde(untagged)] rep),
Prim(prim(Primitive), span(usize)),
Expand All @@ -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<Node>)),
);

/// A node with a signature
Expand Down

0 comments on commit ebb9f87

Please sign in to comment.