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

@@ -13,23 +13,10 @@ use std::{
str::FromStr,
};
#[cfg(not(feature = "no_float"))]
use crate::{ast::FloatWrapper, FLOAT};
#[cfg(feature = "decimal")]
use rust_decimal::Decimal;
#[cfg(not(feature = "no_index"))]
use crate::{Array, Blob};
#[cfg(not(feature = "no_object"))]
use crate::Map;
#[cfg(not(feature = "no_std"))]
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
use std::time::Instant;
use fmt::Debug;
#[cfg(not(feature = "no_std"))]
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
use instant::Instant;
@@ -190,26 +177,26 @@ pub enum Union {
///
/// Not available under `no_float`.
#[cfg(not(feature = "no_float"))]
Float(FloatWrapper<FLOAT>, Tag, AccessMode),
Float(crate::ast::FloatWrapper<crate::FLOAT>, Tag, AccessMode),
/// _(decimal)_ A fixed-precision decimal value.
/// Exported under the `decimal` feature only.
#[cfg(feature = "decimal")]
Decimal(Box<Decimal>, Tag, AccessMode),
Decimal(Box<rust_decimal::Decimal>, Tag, AccessMode),
/// An array value.
///
/// Not available under `no_index`.
#[cfg(not(feature = "no_index"))]
Array(Box<Array>, Tag, AccessMode),
Array(Box<crate::Array>, Tag, AccessMode),
/// An blob (byte array).
///
/// Not available under `no_index`.
#[cfg(not(feature = "no_index"))]
Blob(Box<Blob>, Tag, AccessMode),
Blob(Box<crate::Blob>, Tag, AccessMode),
/// An object map value.
///
/// Not available under `no_object`.
#[cfg(not(feature = "no_object"))]
Map(Box<Map>, Tag, AccessMode),
Map(Box<crate::Map>, Tag, AccessMode),
/// A function pointer.
FnPtr(Box<FnPtr>, Tag, AccessMode),
/// A timestamp value.
@@ -411,15 +398,15 @@ impl Dynamic {
Union::Char(_, _, _) => TypeId::of::<char>(),
Union::Int(_, _, _) => TypeId::of::<INT>(),
#[cfg(not(feature = "no_float"))]
Union::Float(_, _, _) => TypeId::of::<FLOAT>(),
Union::Float(_, _, _) => TypeId::of::<crate::FLOAT>(),
#[cfg(feature = "decimal")]
Union::Decimal(_, _, _) => TypeId::of::<Decimal>(),
Union::Decimal(_, _, _) => TypeId::of::<rust_decimal::Decimal>(),
#[cfg(not(feature = "no_index"))]
Union::Array(_, _, _) => TypeId::of::<Array>(),
Union::Array(_, _, _) => TypeId::of::<crate::Array>(),
#[cfg(not(feature = "no_index"))]
Union::Blob(_, _, _) => TypeId::of::<Blob>(),
Union::Blob(_, _, _) => TypeId::of::<crate::Blob>(),
#[cfg(not(feature = "no_object"))]
Union::Map(_, _, _) => TypeId::of::<Map>(),
Union::Map(_, _, _) => TypeId::of::<crate::Map>(),
Union::FnPtr(_, _, _) => TypeId::of::<FnPtr>(),
#[cfg(not(feature = "no_std"))]
Union::TimeStamp(_, _, _) => TypeId::of::<Instant>(),
@@ -450,7 +437,7 @@ impl Dynamic {
Union::Char(_, _, _) => "char",
Union::Int(_, _, _) => type_name::<INT>(),
#[cfg(not(feature = "no_float"))]
Union::Float(_, _, _) => type_name::<FLOAT>(),
Union::Float(_, _, _) => type_name::<crate::FLOAT>(),
#[cfg(feature = "decimal")]
Union::Decimal(_, _, _) => "decimal",
#[cfg(not(feature = "no_index"))]
@@ -582,19 +569,19 @@ pub(crate) fn map_std_type_name(name: &str) -> &str {
return "Fn";
}
#[cfg(feature = "decimal")]
if name == type_name::<Decimal>() {
if name == type_name::<rust_decimal::Decimal>() {
return "decimal";
}
#[cfg(not(feature = "no_index"))]
if name == type_name::<Array>() {
if name == type_name::<crate::Array>() {
return "array";
}
#[cfg(not(feature = "no_index"))]
if name == type_name::<Blob>() {
if name == type_name::<crate::Blob>() {
return "blob";
}
#[cfg(not(feature = "no_object"))]
if name == type_name::<Map>() {
if name == type_name::<crate::Map>() {
return "map";
}
#[cfg(not(feature = "no_std"))]
@@ -939,14 +926,14 @@ impl Dynamic {
pub const fn from_char(value: char) -> Self {
Self(Union::Char(value, DEFAULT_TAG_VALUE, ReadWrite))
}
/// Create a new [`Dynamic`] from a [`FLOAT`].
/// Create a new [`Dynamic`] from a [`FLOAT`][crate::FLOAT].
///
/// Not available under `no_float`.
#[cfg(not(feature = "no_float"))]
#[inline(always)]
pub const fn from_float(value: FLOAT) -> Self {
pub const fn from_float(value: crate::FLOAT) -> Self {
Self(Union::Float(
FloatWrapper::new_const(value),
crate::ast::FloatWrapper::new_const(value),
DEFAULT_TAG_VALUE,
ReadWrite,
))
@@ -956,7 +943,7 @@ impl Dynamic {
/// Exported under the `decimal` feature only.
#[cfg(feature = "decimal")]
#[inline(always)]
pub fn from_decimal(value: Decimal) -> Self {
pub fn from_decimal(value: rust_decimal::Decimal) -> Self {
Self(Union::Decimal(value.into(), DEFAULT_TAG_VALUE, ReadWrite))
}
/// Create a new [`Dynamic`] from an [`Instant`].
@@ -1104,12 +1091,12 @@ impl Dynamic {
///
/// # Notes
///
/// Beware that you need to pass in an [`Array`] type for it to be recognized as an [`Array`].
/// A [`Vec<T>`][Vec] does not get automatically converted to an [`Array`], but will be a generic
/// Beware that you need to pass in an [`Array`][crate::Array] type for it to be recognized as an [`Array`][crate::Array].
/// A [`Vec<T>`][Vec] does not get automatically converted to an [`Array`][crate::Array], but will be a generic
/// restricted trait object instead, because [`Vec<T>`][Vec] is not a supported standard type.
///
/// Similarly, passing in a [`HashMap<String, T>`][std::collections::HashMap] or
/// [`BTreeMap<String, T>`][std::collections::BTreeMap] will not get a [`Map`] but a trait object.
/// [`BTreeMap<String, T>`][std::collections::BTreeMap] will not get a [`Map`][crate::Map] but a trait object.
///
/// # Examples
///
@@ -1143,12 +1130,12 @@ impl Dynamic {
return (*val.downcast_ref::<INT>().expect(CHECKED)).into();
}
#[cfg(not(feature = "no_float"))]
if TypeId::of::<T>() == TypeId::of::<FLOAT>() {
return (*val.downcast_ref::<FLOAT>().expect(CHECKED)).into();
if TypeId::of::<T>() == TypeId::of::<crate::FLOAT>() {
return (*val.downcast_ref::<crate::FLOAT>().expect(CHECKED)).into();
}
#[cfg(feature = "decimal")]
if TypeId::of::<T>() == TypeId::of::<Decimal>() {
return (*val.downcast_ref::<Decimal>().expect(CHECKED)).into();
if TypeId::of::<T>() == TypeId::of::<rust_decimal::Decimal>() {
return (*val.downcast_ref::<rust_decimal::Decimal>().expect(CHECKED)).into();
}
if TypeId::of::<T>() == TypeId::of::<bool>() {
return (*val.downcast_ref::<bool>().expect(CHECKED)).into();
@@ -1176,14 +1163,14 @@ impl Dynamic {
};
#[cfg(not(feature = "no_index"))]
{
value = match unsafe_try_cast::<_, Array>(value) {
value = match unsafe_try_cast::<_, crate::Array>(value) {
Ok(array) => return array.into(),
Err(value) => value,
};
}
#[cfg(not(feature = "no_index"))]
{
value = match unsafe_try_cast::<_, Blob>(value) {
value = match unsafe_try_cast::<_, crate::Blob>(value) {
Ok(blob) => return Self(Union::Blob(Box::new(blob), DEFAULT_TAG_VALUE, ReadWrite)),
Err(value) => value,
};
@@ -1191,7 +1178,7 @@ impl Dynamic {
#[cfg(not(feature = "no_object"))]
{
value = match unsafe_try_cast::<_, Map>(value) {
value = match unsafe_try_cast::<_, crate::Map>(value) {
Ok(map) => return map.into(),
Err(value) => value,
};
@@ -1298,7 +1285,7 @@ impl Dynamic {
}
#[cfg(not(feature = "no_float"))]
if TypeId::of::<T>() == TypeId::of::<FLOAT>() {
if TypeId::of::<T>() == TypeId::of::<crate::FLOAT>() {
return match self.0 {
Union::Float(value, _, _) => unsafe_try_cast(*value).ok(),
_ => None,
@@ -1306,7 +1293,7 @@ impl Dynamic {
}
#[cfg(feature = "decimal")]
if TypeId::of::<T>() == TypeId::of::<Decimal>() {
if TypeId::of::<T>() == TypeId::of::<rust_decimal::Decimal>() {
return match self.0 {
Union::Decimal(value, _, _) => unsafe_try_cast(*value).ok(),
_ => None,
@@ -1342,7 +1329,7 @@ impl Dynamic {
}
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<Array>() {
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
return match self.0 {
Union::Array(value, _, _) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v),
_ => None,
@@ -1350,7 +1337,7 @@ impl Dynamic {
}
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<Blob>() {
if TypeId::of::<T>() == TypeId::of::<crate::Blob>() {
return match self.0 {
Union::Blob(value, _, _) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v),
_ => None,
@@ -1358,7 +1345,7 @@ impl Dynamic {
}
#[cfg(not(feature = "no_object"))]
if TypeId::of::<T>() == TypeId::of::<Map>() {
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
return match self.0 {
Union::Map(value, _, _) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v),
_ => None,
@@ -1652,14 +1639,14 @@ impl Dynamic {
};
}
#[cfg(not(feature = "no_float"))]
if TypeId::of::<T>() == TypeId::of::<FLOAT>() {
if TypeId::of::<T>() == TypeId::of::<crate::FLOAT>() {
return match self.0 {
Union::Float(ref value, _, _) => value.as_ref().as_any().downcast_ref::<T>(),
_ => None,
};
}
#[cfg(feature = "decimal")]
if TypeId::of::<T>() == TypeId::of::<Decimal>() {
if TypeId::of::<T>() == TypeId::of::<rust_decimal::Decimal>() {
return match self.0 {
Union::Decimal(ref value, _, _) => value.as_ref().as_any().downcast_ref::<T>(),
_ => None,
@@ -1684,21 +1671,21 @@ impl Dynamic {
};
}
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<Array>() {
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
return match self.0 {
Union::Array(ref value, _, _) => value.as_ref().as_any().downcast_ref::<T>(),
_ => None,
};
}
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<Blob>() {
if TypeId::of::<T>() == TypeId::of::<crate::Blob>() {
return match self.0 {
Union::Blob(ref value, _, _) => value.as_ref().as_any().downcast_ref::<T>(),
_ => None,
};
}
#[cfg(not(feature = "no_object"))]
if TypeId::of::<T>() == TypeId::of::<Map>() {
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
return match self.0 {
Union::Map(ref value, _, _) => value.as_ref().as_any().downcast_ref::<T>(),
_ => None,
@@ -1750,7 +1737,7 @@ impl Dynamic {
};
}
#[cfg(not(feature = "no_float"))]
if TypeId::of::<T>() == TypeId::of::<FLOAT>() {
if TypeId::of::<T>() == TypeId::of::<crate::FLOAT>() {
return match self.0 {
Union::Float(ref mut value, _, _) => {
value.as_mut().as_mut_any().downcast_mut::<T>()
@@ -1759,7 +1746,7 @@ impl Dynamic {
};
}
#[cfg(feature = "decimal")]
if TypeId::of::<T>() == TypeId::of::<Decimal>() {
if TypeId::of::<T>() == TypeId::of::<rust_decimal::Decimal>() {
return match self.0 {
Union::Decimal(ref mut value, _, _) => {
value.as_mut().as_mut_any().downcast_mut::<T>()
@@ -1786,7 +1773,7 @@ impl Dynamic {
};
}
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<Array>() {
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
return match self.0 {
Union::Array(ref mut value, _, _) => {
value.as_mut().as_mut_any().downcast_mut::<T>()
@@ -1795,14 +1782,14 @@ impl Dynamic {
};
}
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<Blob>() {
if TypeId::of::<T>() == TypeId::of::<crate::Blob>() {
return match self.0 {
Union::Blob(ref mut value, _, _) => value.as_mut().as_mut_any().downcast_mut::<T>(),
_ => None,
};
}
#[cfg(not(feature = "no_object"))]
if TypeId::of::<T>() == TypeId::of::<Map>() {
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
return match self.0 {
Union::Map(ref mut value, _, _) => value.as_mut().as_mut_any().downcast_mut::<T>(),
_ => None,
@@ -1864,13 +1851,13 @@ impl Dynamic {
_ => Err(self.type_name()),
}
}
/// Cast the [`Dynamic`] as the system floating-point type [`FLOAT`].
/// Cast the [`Dynamic`] as the system floating-point type [`FLOAT`][crate::FLOAT].
/// Returns the name of the actual type if the cast fails.
///
/// Not available under `no_float`.
#[cfg(not(feature = "no_float"))]
#[inline]
pub fn as_float(&self) -> Result<FLOAT, &'static str> {
pub fn as_float(&self) -> Result<crate::FLOAT, &'static str> {
match self.0 {
Union::Float(n, _, _) => Ok(*n),
#[cfg(not(feature = "no_closure"))]
@@ -1878,13 +1865,13 @@ impl Dynamic {
_ => Err(self.type_name()),
}
}
/// _(decimal)_ Cast the [`Dynamic`] as a [`Decimal`](https://docs.rs/rust_decimal).
/// _(decimal)_ Cast the [`Dynamic`] as a [`Decimal`][rust_decimal::Decimal].
/// Returns the name of the actual type if the cast fails.
///
/// Exported under the `decimal` feature only.
#[cfg(feature = "decimal")]
#[inline]
pub fn as_decimal(&self) -> Result<Decimal, &'static str> {
pub fn as_decimal(&self) -> Result<rust_decimal::Decimal, &'static str> {
match self.0 {
Union::Decimal(ref n, _, _) => Ok(**n),
#[cfg(not(feature = "no_closure"))]
@@ -1979,23 +1966,23 @@ impl From<INT> for Dynamic {
}
}
#[cfg(not(feature = "no_float"))]
impl From<FLOAT> for Dynamic {
impl From<crate::FLOAT> for Dynamic {
#[inline(always)]
fn from(value: FLOAT) -> Self {
fn from(value: crate::FLOAT) -> Self {
Self(Union::Float(value.into(), DEFAULT_TAG_VALUE, ReadWrite))
}
}
#[cfg(not(feature = "no_float"))]
impl From<FloatWrapper<FLOAT>> for Dynamic {
impl From<crate::ast::FloatWrapper<crate::FLOAT>> for Dynamic {
#[inline(always)]
fn from(value: FloatWrapper<FLOAT>) -> Self {
fn from(value: crate::ast::FloatWrapper<crate::FLOAT>) -> Self {
Self(Union::Float(value, DEFAULT_TAG_VALUE, ReadWrite))
}
}
#[cfg(feature = "decimal")]
impl From<Decimal> for Dynamic {
impl From<rust_decimal::Decimal> for Dynamic {
#[inline(always)]
fn from(value: Decimal) -> Self {
fn from(value: rust_decimal::Decimal) -> Self {
Self(Union::Decimal(value.into(), DEFAULT_TAG_VALUE, ReadWrite))
}
}
@@ -2026,9 +2013,9 @@ impl FromStr for Dynamic {
}
#[cfg(not(feature = "no_index"))]
impl Dynamic {
/// Create a [`Dynamic`] from an [`Array`].
/// Create a [`Dynamic`] from an [`Array`][crate::Array].
#[inline(always)]
pub(crate) fn from_array(array: Array) -> Self {
pub(crate) fn from_array(array: crate::Array) -> Self {
Self(Union::Array(array.into(), DEFAULT_TAG_VALUE, ReadWrite))
}
}
@@ -2067,10 +2054,10 @@ impl<T: Variant + Clone> std::iter::FromIterator<T> for Dynamic {
}
#[cfg(not(feature = "no_index"))]
impl Dynamic {
/// Convert the [`Dynamic`] into an [`Array`].
/// Convert the [`Dynamic`] into an [`Array`][crate::Array].
/// Returns the name of the actual type if the cast fails.
#[inline(always)]
pub fn into_array(self) -> Result<Array, &'static str> {
pub fn into_array(self) -> Result<crate::Array, &'static str> {
match self.0 {
Union::Array(a, _, _) => Ok(*a),
#[cfg(not(feature = "no_closure"))]
@@ -2148,13 +2135,13 @@ impl Dynamic {
impl Dynamic {
/// Create a [`Dynamic`] from a [`Vec<u8>`].
#[inline(always)]
pub fn from_blob(blob: Blob) -> Self {
pub fn from_blob(blob: crate::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)]
pub fn into_blob(self) -> Result<Blob, &'static str> {
pub fn into_blob(self) -> Result<crate::Blob, &'static str> {
match self.0 {
Union::Blob(a, _, _) => Ok(*a),
#[cfg(not(feature = "no_closure"))]
@@ -2175,9 +2162,9 @@ impl Dynamic {
}
#[cfg(not(feature = "no_object"))]
impl Dynamic {
/// Create a [`Dynamic`] from a [`Map`].
/// Create a [`Dynamic`] from a [`Map`][crate::Map].
#[inline(always)]
pub(crate) fn from_map(map: Map) -> Self {
pub(crate) fn from_map(map: crate::Map) -> Self {
Self(Union::Map(map.into(), DEFAULT_TAG_VALUE, ReadWrite))
}
}