Fix serde build.

This commit is contained in:
Stephen Chung 2021-03-29 18:46:32 +08:00
parent e306a92ea0
commit 3a6e6848fd
6 changed files with 27 additions and 22 deletions

View File

@ -622,7 +622,7 @@ impl Module {
pub fn update_fn_metadata(&mut self, hash_fn: u64, arg_names: &[&str]) -> &mut Self { pub fn update_fn_metadata(&mut self, hash_fn: u64, arg_names: &[&str]) -> &mut Self {
let param_names = arg_names let param_names = arg_names
.iter() .iter()
.map(|&name| self.interned_strings.get(name)) .map(|&name| self.identifiers.get(name))
.collect(); .collect();
if let Some(f) = self.functions.get_mut(&hash_fn) { if let Some(f) = self.functions.get_mut(&hash_fn) {
@ -692,7 +692,7 @@ impl Module {
let param_names = _arg_names let param_names = _arg_names
.iter() .iter()
.flat_map(|p| p.iter()) .flat_map(|p| p.iter())
.map(|&arg| self.interned_strings.get(arg)) .map(|&arg| self.identifiers.get(arg))
.collect(); .collect();
let hash_fn = calc_native_fn_hash(empty(), &name, &param_types); let hash_fn = calc_native_fn_hash(empty(), &name, &param_types);

View File

@ -1,6 +1,6 @@
//! Implement deserialization support of [`Dynamic`][crate::Dynamic] for [`serde`]. //! Implement deserialization support of [`Dynamic`][crate::Dynamic] for [`serde`].
use super::str::ImmutableStringDeserializer; use super::str::StringSliceDeserializer;
use crate::dynamic::Union; use crate::dynamic::Union;
use crate::stdlib::{any::type_name, boxed::Box, fmt, string::ToString}; use crate::stdlib::{any::type_name, boxed::Box, fmt, string::ToString};
use crate::{Dynamic, EvalAltResult, ImmutableString, LexError, Position}; use crate::{Dynamic, EvalAltResult, ImmutableString, LexError, Position};
@ -418,7 +418,12 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
return self.value.downcast_ref::<Map>().map_or_else( return self.value.downcast_ref::<Map>().map_or_else(
|| self.type_error(), || self.type_error(),
|map| _visitor.visit_map(IterateMap::new(map.keys(), map.values())), |map| {
_visitor.visit_map(IterateMap::new(
map.keys().map(|key| key.as_str()),
map.values(),
))
},
); );
#[cfg(feature = "no_object")] #[cfg(feature = "no_object")]
@ -512,7 +517,7 @@ impl<'a: 'de, 'de, ITER: Iterator<Item = &'a Dynamic>> SeqAccess<'de> for Iterat
/// `MapAccess` implementation for maps. /// `MapAccess` implementation for maps.
struct IterateMap<'a, KEYS, VALUES> struct IterateMap<'a, KEYS, VALUES>
where where
KEYS: Iterator<Item = &'a ImmutableString>, KEYS: Iterator<Item = &'a str>,
VALUES: Iterator<Item = &'a Dynamic>, VALUES: Iterator<Item = &'a Dynamic>,
{ {
// Iterator for a stream of [`Dynamic`][crate::Dynamic] keys. // Iterator for a stream of [`Dynamic`][crate::Dynamic] keys.
@ -524,7 +529,7 @@ where
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
impl<'a, KEYS, VALUES> IterateMap<'a, KEYS, VALUES> impl<'a, KEYS, VALUES> IterateMap<'a, KEYS, VALUES>
where where
KEYS: Iterator<Item = &'a ImmutableString>, KEYS: Iterator<Item = &'a str>,
VALUES: Iterator<Item = &'a Dynamic>, VALUES: Iterator<Item = &'a Dynamic>,
{ {
pub fn new(keys: KEYS, values: VALUES) -> Self { pub fn new(keys: KEYS, values: VALUES) -> Self {
@ -534,7 +539,7 @@ where
impl<'a: 'de, 'de, KEYS, VALUES> MapAccess<'de> for IterateMap<'a, KEYS, VALUES> impl<'a: 'de, 'de, KEYS, VALUES> MapAccess<'de> for IterateMap<'a, KEYS, VALUES>
where where
KEYS: Iterator<Item = &'a ImmutableString>, KEYS: Iterator<Item = &'a str>,
VALUES: Iterator<Item = &'a Dynamic>, VALUES: Iterator<Item = &'a Dynamic>,
{ {
type Error = Box<EvalAltResult>; type Error = Box<EvalAltResult>;
@ -543,11 +548,11 @@ where
&mut self, &mut self,
seed: K, seed: K,
) -> Result<Option<K::Value>, Box<EvalAltResult>> { ) -> Result<Option<K::Value>, Box<EvalAltResult>> {
// Deserialize each `ImmutableString` key coming out of the keys iterator. // Deserialize each `Identifier` key coming out of the keys iterator.
match self.keys.next() { match self.keys.next() {
None => Ok(None), None => Ok(None),
Some(item) => seed Some(item) => seed
.deserialize(&mut ImmutableStringDeserializer::from_str(item)) .deserialize(&mut StringSliceDeserializer::from_str(item))
.map(Some), .map(Some),
} }
} }

View File

@ -143,8 +143,8 @@ impl<'d> Visitor<'d> for DynamicVisitor {
fn visit_map<M: MapAccess<'d>>(self, mut map: M) -> Result<Self::Value, M::Error> { fn visit_map<M: MapAccess<'d>>(self, mut map: M) -> Result<Self::Value, M::Error> {
let mut m: Map = Default::default(); let mut m: Map = Default::default();
while let Some((k, v)) = map.next_entry()? { while let Some((k, v)) = map.next_entry::<&str, _>()? {
m.insert(k, v); m.insert(k.into(), v);
} }
Ok(m.into()) Ok(m.into())

View File

@ -550,7 +550,7 @@ impl SerializeMap for DynamicSerializer {
})?; })?;
let _value = _value.serialize(&mut *self)?; let _value = _value.serialize(&mut *self)?;
let map = self._value.downcast_mut::<Map>().unwrap(); let map = self._value.downcast_mut::<Map>().unwrap();
map.insert(key, _value); map.insert(key.into(), _value);
Ok(()) Ok(())
} }
#[cfg(feature = "no_object")] #[cfg(feature = "no_object")]
@ -575,7 +575,7 @@ impl SerializeMap for DynamicSerializer {
})?; })?;
let _value = _value.serialize(&mut *self)?; let _value = _value.serialize(&mut *self)?;
let map = self._value.downcast_mut::<Map>().unwrap(); let map = self._value.downcast_mut::<Map>().unwrap();
map.insert(_key, _value); map.insert(_key.into(), _value);
Ok(()) Ok(())
} }
#[cfg(feature = "no_object")] #[cfg(feature = "no_object")]

View File

@ -54,7 +54,7 @@ impl Serialize for Dynamic {
Union::Map(m, _) => { Union::Map(m, _) => {
let mut map = ser.serialize_map(Some(m.len()))?; let mut map = ser.serialize_map(Some(m.len()))?;
for (k, v) in m.iter() { for (k, v) in m.iter() {
map.serialize_entry(k, v)?; map.serialize_entry(k.as_str(), v)?;
} }
map.end() map.end()
} }

View File

@ -1,17 +1,17 @@
//! Implement deserialization support of [`ImmutableString`][crate::ImmutableString] for [`serde`]. //! Implement deserialization support of [`ImmutableString`][crate::ImmutableString] for [`serde`].
use crate::stdlib::{any::type_name, boxed::Box}; use crate::stdlib::{any::type_name, boxed::Box};
use crate::{EvalAltResult, ImmutableString, Position}; use crate::{EvalAltResult, Position};
use serde::de::{Deserializer, Visitor}; use serde::de::{Deserializer, Visitor};
/// Deserializer for `ImmutableString`. /// Deserializer for `ImmutableString`.
pub struct ImmutableStringDeserializer<'a> { pub struct StringSliceDeserializer<'a> {
value: &'a ImmutableString, value: &'a str,
} }
impl<'a> ImmutableStringDeserializer<'a> { impl<'a> StringSliceDeserializer<'a> {
/// Create an `ImmutableStringDeserializer` from an `ImmutableString` reference. /// Create an `ImmutableStringDeserializer` from an `&str` reference.
pub fn from_str(value: &'a ImmutableString) -> Self { pub fn from_str(value: &'a str) -> Self {
Self { value } Self { value }
} }
/// Shortcut for a type conversion error. /// Shortcut for a type conversion error.
@ -25,7 +25,7 @@ impl<'a> ImmutableStringDeserializer<'a> {
} }
} }
impl<'de> Deserializer<'de> for &mut ImmutableStringDeserializer<'de> { impl<'de> Deserializer<'de> for &mut StringSliceDeserializer<'de> {
type Error = Box<EvalAltResult>; type Error = Box<EvalAltResult>;
fn deserialize_any<V: Visitor<'de>>(self, v: V) -> Result<V::Value, Box<EvalAltResult>> { fn deserialize_any<V: Visitor<'de>>(self, v: V) -> Result<V::Value, Box<EvalAltResult>> {
@ -69,7 +69,7 @@ impl<'de> Deserializer<'de> for &mut ImmutableStringDeserializer<'de> {
} }
fn deserialize_str<V: Visitor<'de>>(self, v: V) -> Result<V::Value, Box<EvalAltResult>> { fn deserialize_str<V: Visitor<'de>>(self, v: V) -> Result<V::Value, Box<EvalAltResult>> {
// Only allow deserialization into a string. // Only allow deserialization into a string.
v.visit_borrowed_str(self.value.as_str()) v.visit_borrowed_str(self.value)
} }
fn deserialize_string<V: Visitor<'de>>( fn deserialize_string<V: Visitor<'de>>(
self, self,