Fix builds.

This commit is contained in:
Stephen Chung 2021-12-02 17:46:39 +08:00
parent 27c0181035
commit 9a5bd9396a
3 changed files with 28 additions and 9 deletions

View File

@ -1,9 +1,8 @@
//! Implement deserialization support of [`Dynamic`][crate::Dynamic] for [`serde`].
use super::str::StringSliceDeserializer;
use crate::types::dynamic::Union;
use crate::{Dynamic, EvalAltResult, ImmutableString, LexError, Position};
use serde::de::{DeserializeSeed, Error, IntoDeserializer, MapAccess, SeqAccess, Visitor};
use serde::de::{DeserializeSeed, Error, IntoDeserializer, Visitor};
use serde::{Deserialize, Deserializer};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -359,13 +358,14 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
fn deserialize_bytes<V: Visitor<'de>>(
self,
visitor: V,
_visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
#[cfg(not(feature = "no_index"))]
return self
.value
.downcast_ref::<Blob>()
.map_or_else(|| self.type_error(), |x| visitor.visit_bytes(x));
.map_or_else(|| self.type_error(), |x| _visitor.visit_bytes(x));
#[cfg(feature = "no_index")]
return self.type_error();
}
@ -509,11 +509,13 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
}
/// `SeqAccess` implementation for arrays.
#[cfg(not(feature = "no_index"))]
struct IterateDynamicArray<'a, ITER: Iterator<Item = &'a Dynamic>> {
/// Iterator for a stream of [`Dynamic`][crate::Dynamic] values.
iter: ITER,
}
#[cfg(not(feature = "no_index"))]
impl<'a, ITER: Iterator<Item = &'a Dynamic>> IterateDynamicArray<'a, ITER> {
#[must_use]
pub fn new(iter: ITER) -> Self {
@ -521,7 +523,8 @@ impl<'a, ITER: Iterator<Item = &'a Dynamic>> IterateDynamicArray<'a, ITER> {
}
}
impl<'a: 'de, 'de, ITER: Iterator<Item = &'a Dynamic>> SeqAccess<'de>
#[cfg(not(feature = "no_index"))]
impl<'a: 'de, 'de, ITER: Iterator<Item = &'a Dynamic>> serde::de::SeqAccess<'de>
for IterateDynamicArray<'a, ITER>
{
type Error = Box<EvalAltResult>;
@ -541,6 +544,7 @@ impl<'a: 'de, 'de, ITER: Iterator<Item = &'a Dynamic>> SeqAccess<'de>
}
/// `MapAccess` implementation for maps.
#[cfg(not(feature = "no_object"))]
struct IterateMap<'a, KEYS, VALUES>
where
KEYS: Iterator<Item = &'a str>,
@ -564,7 +568,8 @@ where
}
}
impl<'a: 'de, 'de, KEYS, VALUES> MapAccess<'de> for IterateMap<'a, KEYS, VALUES>
#[cfg(not(feature = "no_object"))]
impl<'a: 'de, 'de, KEYS, VALUES> serde::de::MapAccess<'de> for IterateMap<'a, KEYS, VALUES>
where
KEYS: Iterator<Item = &'a str>,
VALUES: Iterator<Item = &'a Dynamic>,
@ -579,7 +584,7 @@ where
match self.keys.next() {
None => Ok(None),
Some(item) => seed
.deserialize(&mut StringSliceDeserializer::from_str(item))
.deserialize(&mut super::str::StringSliceDeserializer::from_str(item))
.map(Some),
}
}

View File

@ -260,8 +260,17 @@ impl Serializer for &mut DynamicSerializer {
Ok(v.into())
}
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Box<EvalAltResult>> {
Ok(v.into())
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Box<EvalAltResult>> {
#[cfg(not(feature = "no_index"))]
return Ok(Dynamic::from_blob(_v.to_vec()));
#[cfg(feature = "no_index")]
return Err(EvalAltResult::ErrorMismatchDataType(
"".into(),
"BLOB's are not supported with 'no_index'".into(),
Position::NONE,
)
.into());
}
fn serialize_none(self) -> Result<Self::Ok, Box<EvalAltResult>> {

View File

@ -2146,6 +2146,11 @@ impl Dynamic {
}
#[cfg(not(feature = "no_index"))]
impl Dynamic {
/// Create a [`Dynamic`] from a [`Vec<u8>`].
#[inline(always)]
pub fn from_blob(blob: Blob) -> Self {
Self(Union::Blob(Box::new(blob), DEFAULT_TAG_VALUE, ReadWrite))
}
/// Convert the [`Dynamic`] into a [`Vec<u8>`].
/// Returns the name of the actual type if the cast fails.
#[inline(always)]