Reduce feature gates.

This commit is contained in:
Stephen Chung
2021-12-06 20:52:47 +08:00
parent 5b64e0b383
commit 2a7a648429
17 changed files with 176 additions and 253 deletions

View File

@@ -8,12 +8,6 @@ use serde::{Deserialize, Deserializer};
use std::prelude::v1::*;
use std::{any::type_name, fmt};
#[cfg(not(feature = "no_index"))]
use crate::{Array, Blob};
#[cfg(not(feature = "no_object"))]
use crate::Map;
/// Deserializer for [`Dynamic`][crate::Dynamic] which is kept as a reference.
///
/// The reference is necessary because the deserialized type may hold references
@@ -363,7 +357,7 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
#[cfg(not(feature = "no_index"))]
return self
.value
.downcast_ref::<Blob>()
.downcast_ref::<crate::Blob>()
.map_or_else(|| self.type_error(), |x| _visitor.visit_bytes(x));
#[cfg(feature = "no_index")]
@@ -412,7 +406,7 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
fn deserialize_seq<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
#[cfg(not(feature = "no_index"))]
return self.value.downcast_ref::<Array>().map_or_else(
return self.value.downcast_ref::<crate::Array>().map_or_else(
|| self.type_error(),
|arr| _visitor.visit_seq(IterateDynamicArray::new(arr.iter())),
);
@@ -440,7 +434,7 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
fn deserialize_map<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
#[cfg(not(feature = "no_object"))]
return self.value.downcast_ref::<Map>().map_or_else(
return self.value.downcast_ref::<crate::Map>().map_or_else(
|| self.type_error(),
|map| {
_visitor.visit_map(IterateMap::new(
@@ -473,7 +467,7 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
visitor.visit_enum(s.as_str().into_deserializer())
} else {
#[cfg(not(feature = "no_object"))]
if let Some(map) = self.value.downcast_ref::<Map>() {
if let Some(map) = self.value.downcast_ref::<crate::Map>() {
let mut iter = map.iter();
let first = iter.next();
let second = iter.next();

View File

@@ -6,18 +6,6 @@ use std::fmt;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
#[cfg(not(feature = "no_index"))]
use crate::Array;
#[cfg(not(feature = "no_index"))]
use serde::de::SeqAccess;
#[cfg(not(feature = "no_object"))]
use crate::Map;
#[cfg(not(feature = "no_object"))]
use serde::de::MapAccess;
struct DynamicVisitor;
impl<'d> Visitor<'d> for DynamicVisitor {
@@ -141,8 +129,8 @@ impl<'d> Visitor<'d> for DynamicVisitor {
}
#[cfg(not(feature = "no_index"))]
fn visit_seq<A: SeqAccess<'d>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let mut arr = Array::new();
fn visit_seq<A: serde::de::SeqAccess<'d>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let mut arr = crate::Array::new();
while let Some(v) = seq.next_element()? {
arr.push(v);
@@ -152,8 +140,8 @@ impl<'d> Visitor<'d> for DynamicVisitor {
}
#[cfg(not(feature = "no_object"))]
fn visit_map<M: MapAccess<'d>>(self, mut map: M) -> Result<Self::Value, M::Error> {
let mut m = Map::new();
fn visit_map<M: serde::de::MapAccess<'d>>(self, mut map: M) -> Result<Self::Value, M::Error> {
let mut m = crate::Map::new();
while let Some((k, v)) = map.next_entry::<&str, _>()? {
m.insert(k.into(), v);

View File

@@ -1,3 +1,5 @@
#![cfg(feature = "metadata")]
use crate::module::calc_native_fn_hash;
use crate::{calc_fn_hash, Engine, AST};
use serde::{Deserialize, Serialize};

View File

@@ -3,12 +3,10 @@
mod de;
mod deserialize;
mod metadata;
mod ser;
mod serialize;
mod str;
#[cfg(feature = "metadata")]
mod metadata;
pub use de::from_dynamic;
pub use ser::to_dynamic;

View File

@@ -9,12 +9,6 @@ use std::fmt;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
#[cfg(not(feature = "no_index"))]
use crate::Array;
#[cfg(not(feature = "no_object"))]
use crate::Map;
/// Serializer for [`Dynamic`][crate::Dynamic] which is kept as a reference.
struct DynamicSerializer {
/// Buffer to hold a temporary key.
@@ -332,7 +326,7 @@ impl Serializer for &mut DynamicSerializer {
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Box<EvalAltResult>> {
#[cfg(not(feature = "no_index"))]
return Ok(DynamicSerializer::new(Array::new().into()));
return Ok(DynamicSerializer::new(crate::Array::new().into()));
#[cfg(feature = "no_index")]
return Err(EvalAltResult::ErrorMismatchDataType(
"".into(),
@@ -365,7 +359,7 @@ impl Serializer for &mut DynamicSerializer {
#[cfg(not(feature = "no_index"))]
return Ok(TupleVariantSerializer {
variant: _variant,
array: Array::with_capacity(_len),
array: crate::Array::with_capacity(_len),
});
#[cfg(any(feature = "no_object", feature = "no_index"))]
return Err(EvalAltResult::ErrorMismatchDataType(
@@ -378,7 +372,7 @@ impl Serializer for &mut DynamicSerializer {
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Box<EvalAltResult>> {
#[cfg(not(feature = "no_object"))]
return Ok(DynamicSerializer::new(Map::new().into()));
return Ok(DynamicSerializer::new(crate::Map::new().into()));
#[cfg(feature = "no_object")]
return Err(EvalAltResult::ErrorMismatchDataType(
"".into(),
@@ -406,7 +400,7 @@ impl Serializer for &mut DynamicSerializer {
#[cfg(not(feature = "no_object"))]
return Ok(StructVariantSerializer {
variant: _variant,
map: Map::new(),
map: crate::Map::new(),
});
#[cfg(feature = "no_object")]
return Err(EvalAltResult::ErrorMismatchDataType(
@@ -429,7 +423,7 @@ impl SerializeSeq for DynamicSerializer {
#[cfg(not(feature = "no_index"))]
{
let _value = _value.serialize(&mut *self)?;
let arr = self._value.downcast_mut::<Array>().unwrap();
let arr = self._value.downcast_mut::<crate::Array>().unwrap();
arr.push(_value);
Ok(())
}
@@ -467,7 +461,7 @@ impl SerializeTuple for DynamicSerializer {
#[cfg(not(feature = "no_index"))]
{
let _value = _value.serialize(&mut *self)?;
let arr = self._value.downcast_mut::<Array>().unwrap();
let arr = self._value.downcast_mut::<crate::Array>().unwrap();
arr.push(_value);
Ok(())
}
@@ -504,7 +498,7 @@ impl SerializeTupleStruct for DynamicSerializer {
#[cfg(not(feature = "no_index"))]
{
let _value = _value.serialize(&mut *self)?;
let arr = self._value.downcast_mut::<Array>().unwrap();
let arr = self._value.downcast_mut::<crate::Array>().unwrap();
arr.push(_value);
Ok(())
}
@@ -565,7 +559,7 @@ impl SerializeMap for DynamicSerializer {
)
})?;
let _value = _value.serialize(&mut *self)?;
let map = self._value.downcast_mut::<Map>().unwrap();
let map = self._value.downcast_mut::<crate::Map>().unwrap();
map.insert(key.into(), _value);
Ok(())
}
@@ -590,7 +584,7 @@ impl SerializeMap for DynamicSerializer {
EvalAltResult::ErrorMismatchDataType("string".into(), typ.into(), Position::NONE)
})?;
let _value = _value.serialize(&mut *self)?;
let map = self._value.downcast_mut::<Map>().unwrap();
let map = self._value.downcast_mut::<crate::Map>().unwrap();
map.insert(_key.into(), _value);
Ok(())
}
@@ -628,7 +622,7 @@ impl SerializeStruct for DynamicSerializer {
#[cfg(not(feature = "no_object"))]
{
let _value = _value.serialize(&mut *self)?;
let map = self._value.downcast_mut::<Map>().unwrap();
let map = self._value.downcast_mut::<crate::Map>().unwrap();
map.insert(_key.into(), _value);
Ok(())
}
@@ -657,7 +651,7 @@ impl SerializeStruct for DynamicSerializer {
#[cfg(not(any(feature = "no_object", feature = "no_index")))]
struct TupleVariantSerializer {
variant: &'static str,
array: Array,
array: crate::Array,
}
#[cfg(not(any(feature = "no_object", feature = "no_index")))]
@@ -682,7 +676,7 @@ impl serde::ser::SerializeTupleVariant for TupleVariantSerializer {
#[cfg(not(feature = "no_object"))]
struct StructVariantSerializer {
variant: &'static str,
map: Map,
map: crate::Map,
}
#[cfg(not(feature = "no_object"))]
@@ -707,7 +701,7 @@ impl serde::ser::SerializeStructVariant for StructVariantSerializer {
#[cfg(not(feature = "no_object"))]
fn make_variant(variant: &'static str, value: Dynamic) -> RhaiResult {
let mut map = Map::new();
let mut map = crate::Map::new();
map.insert(variant.into(), value);
Ok(map.into())
}