From 2a7a6484295ca14ae7484bbe2cdef6b2acc50632 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 6 Dec 2021 20:52:47 +0800 Subject: [PATCH] Reduce feature gates. --- src/api/compile.rs | 9 +-- src/api/deprecated.rs | 1 - src/api/limits.rs | 3 +- src/api/register.rs | 32 ++++----- src/ast.rs | 18 ++--- src/engine.rs | 55 +++++++-------- src/func/call.rs | 5 +- src/module/mod.rs | 20 ++---- src/packages/math_basic.rs | 2 +- src/parser.rs | 44 ++++++------ src/serde/de.rs | 14 ++-- src/serde/deserialize.rs | 20 ++---- src/serde/metadata.rs | 2 + src/serde/mod.rs | 4 +- src/serde/ser.rs | 32 ++++----- src/tokenizer.rs | 31 ++++----- src/types/dynamic.rs | 137 +++++++++++++++++-------------------- 17 files changed, 176 insertions(+), 253 deletions(-) diff --git a/src/api/compile.rs b/src/api/compile.rs index f089e7ed..9ca748cd 100644 --- a/src/api/compile.rs +++ b/src/api/compile.rs @@ -5,9 +5,6 @@ use crate::{Engine, ParseError, Scope, AST}; #[cfg(feature = "no_std")] use std::prelude::v1::*; -#[cfg(not(feature = "no_object"))] -use crate::Map; - impl Engine { /// Compile a string into an [`AST`], which can be used later for evaluation. /// @@ -313,7 +310,7 @@ impl Engine { self.optimization_level, ) } - /// Parse a JSON string into an [object map][`Map`]. + /// Parse a JSON string into an [object map][crate::Map]. /// This is a light-weight alternative to using, say, /// [`serde_json`](https://crates.io/crates/serde_json) to deserialize the JSON. /// @@ -362,14 +359,14 @@ impl Engine { &self, json: impl AsRef, has_null: bool, - ) -> Result> { + ) -> Result> { use crate::tokenizer::Token; fn parse_json_inner( engine: &Engine, json: &str, has_null: bool, - ) -> Result> { + ) -> Result> { let mut scope = Scope::new(); let json_text = json.trim_start(); let scripts = if json_text.starts_with(Token::MapStart.literal_syntax()) { diff --git a/src/api/deprecated.rs b/src/api/deprecated.rs index 1bcd23b3..b315e636 100644 --- a/src/api/deprecated.rs +++ b/src/api/deprecated.rs @@ -4,7 +4,6 @@ use crate::{ Dynamic, Engine, EvalAltResult, FnPtr, ImmutableString, NativeCallContext, RhaiResult, Scope, AST, }; - #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/api/limits.rs b/src/api/limits.rs index f95ddc8f..7d9fd36c 100644 --- a/src/api/limits.rs +++ b/src/api/limits.rs @@ -2,11 +2,10 @@ #![cfg(not(feature = "unchecked"))] use crate::Engine; +use std::num::{NonZeroU64, NonZeroUsize}; #[cfg(feature = "no_std")] use std::prelude::v1::*; -use std::num::{NonZeroU64, NonZeroUsize}; - /// A type containing all the limits imposed by the [`Engine`]. /// /// Not available under `unchecked`. diff --git a/src/api/register.rs b/src/api/register.rs index a7f19301..304731b9 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -9,12 +9,6 @@ use std::any::{type_name, TypeId}; #[cfg(feature = "no_std")] use std::prelude::v1::*; -#[cfg(not(feature = "no_index"))] -use crate::Array; - -#[cfg(not(feature = "no_object"))] -use crate::Map; - impl Engine { /// Get the global namespace module (which is the last module in `global_modules`). #[inline(always)] @@ -529,7 +523,7 @@ impl Engine { /// /// # Panics /// - /// Panics if the type is [`Array`], [`Map`], [`String`], + /// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`], /// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT]. /// Indexers for arrays, object maps, strings and integers cannot be registered. /// @@ -573,11 +567,11 @@ impl Engine { get_fn: impl Fn(&mut T, X) -> V + SendSync + 'static, ) -> &mut Self { #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for arrays."); } #[cfg(not(feature = "no_object"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for object maps."); } if TypeId::of::() == TypeId::of::() @@ -600,7 +594,7 @@ impl Engine { /// /// # Panics /// - /// Panics if the type is [`Array`], [`Map`], [`String`], + /// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`], /// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT]. /// Indexers for arrays, object maps, strings and integers cannot be registered. /// @@ -650,11 +644,11 @@ impl Engine { get_fn: impl Fn(&mut T, X) -> Result> + SendSync + 'static, ) -> &mut Self { #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for arrays."); } #[cfg(not(feature = "no_object"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for object maps."); } if TypeId::of::() == TypeId::of::() @@ -675,7 +669,7 @@ impl Engine { /// /// # Panics /// - /// Panics if the type is [`Array`], [`Map`], [`String`], + /// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`], /// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT]. /// Indexers for arrays, object maps, strings and integers cannot be registered. /// @@ -721,11 +715,11 @@ impl Engine { set_fn: impl Fn(&mut T, X, V) + SendSync + 'static, ) -> &mut Self { #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for arrays."); } #[cfg(not(feature = "no_object"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for object maps."); } if TypeId::of::() == TypeId::of::() @@ -746,7 +740,7 @@ impl Engine { /// /// # Panics /// - /// Panics if the type is [`Array`], [`Map`], [`String`], + /// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`], /// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT]. /// Indexers for arrays, object maps, strings and integers cannot be registered. /// @@ -799,11 +793,11 @@ impl Engine { set_fn: impl Fn(&mut T, X, V) -> Result<(), Box> + SendSync + 'static, ) -> &mut Self { #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for arrays."); } #[cfg(not(feature = "no_object"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for object maps."); } if TypeId::of::() == TypeId::of::() @@ -824,7 +818,7 @@ impl Engine { /// /// # Panics /// - /// Panics if the type is [`Array`], [`Map`], [`String`], + /// Panics if the type is [`Array`][crate::Array], [`Map`][crate::Map], [`String`], /// [`ImmutableString`][crate::ImmutableString], `&str` or [`INT`][crate::INT]. /// Indexers for arrays, object maps, strings and integers cannot be registered. /// diff --git a/src/ast.rs b/src/ast.rs index c4778bef..15b9ba10 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -25,15 +25,9 @@ use std::{ #[cfg(not(feature = "no_float"))] use std::str::FromStr; -#[cfg(not(feature = "no_float"))] -use crate::FLOAT; - #[cfg(not(feature = "no_float"))] use num_traits::Float; -#[cfg(not(feature = "no_index"))] -use crate::Array; - /// A type representing the access mode of a function. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum FnAccess { @@ -1918,7 +1912,7 @@ impl FnCallExpr { pub struct FloatWrapper(F); #[cfg(not(feature = "no_float"))] -impl Hash for FloatWrapper { +impl Hash for FloatWrapper { #[inline(always)] fn hash(&self, state: &mut H) { self.0.to_ne_bytes().hash(state); @@ -2022,11 +2016,11 @@ impl FloatWrapper { } #[cfg(not(feature = "no_float"))] -impl FloatWrapper { +impl FloatWrapper { /// Create a new [`FloatWrapper`]. #[inline(always)] #[must_use] - pub const fn new_const(value: FLOAT) -> Self { + pub const fn new_const(value: crate::FLOAT) -> Self { Self(value) } } @@ -2037,7 +2031,7 @@ impl FloatWrapper { pub enum Expr { /// Dynamic constant. /// - /// Used to hold complex constants such as [`Array`] or [`Map`][crate::Map] for quick cloning. + /// Used to hold complex constants such as [`Array`][crate::Array] or [`Map`][crate::Map] for quick cloning. /// Primitive data types should use the appropriate variants to avoid an allocation. DynamicConstant(Box, Position), /// Boolean constant. @@ -2048,7 +2042,7 @@ pub enum Expr { /// /// Not available under `no_float`. #[cfg(not(feature = "no_float"))] - FloatConstant(FloatWrapper, Position), + FloatConstant(FloatWrapper, Position), /// Character constant. CharConstant(char, Position), /// [String][ImmutableString] constant. @@ -2224,7 +2218,7 @@ impl Expr { #[cfg(not(feature = "no_index"))] Self::Array(x, _) if self.is_constant() => { - let mut arr = Array::with_capacity(x.len()); + let mut arr = crate::Array::with_capacity(x.len()); arr.extend( x.iter() .map(|v| v.get_literal_value().expect("constant value")), diff --git a/src/engine.rs b/src/engine.rs index a6f9469b..512aa7b0 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -29,15 +29,6 @@ use std::{ ops::{Deref, DerefMut}, }; -#[cfg(not(feature = "no_index"))] -use crate::{Array, Blob}; - -#[cfg(not(feature = "no_object"))] -use crate::Map; - -#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] -use crate::ast::FnCallHashes; - pub type Precedence = NonZeroU8; /// _(internals)_ A stack of imported [modules][Module] plus mutable runtime global states. @@ -246,7 +237,7 @@ impl Imports { /// Get the pre-calculated index getter hash. #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[must_use] - pub(crate) fn fn_hash_idx_get(&mut self) -> u64 { + pub(crate) fn hash_idx_get(&mut self) -> u64 { if self.fn_hash_indexing != (0, 0) { self.fn_hash_indexing.0 } else { @@ -259,7 +250,7 @@ impl Imports { /// Get the pre-calculated index setter hash. #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[must_use] - pub(crate) fn fn_hash_idx_set(&mut self) -> u64 { + pub(crate) fn hash_idx_set(&mut self) -> u64 { if self.fn_hash_indexing != (0, 0) { self.fn_hash_indexing.1 } else { @@ -559,7 +550,7 @@ impl<'a> Target<'a> { #[cfg(not(feature = "no_index"))] Self::BitField(_, _, _) => TypeId::of::() == TypeId::of::(), #[cfg(not(feature = "no_index"))] - Self::BlobByte(_, _, _) => TypeId::of::() == TypeId::of::(), + Self::BlobByte(_, _, _) => TypeId::of::() == TypeId::of::(), #[cfg(not(feature = "no_index"))] Self::StringChar(_, _, _) => TypeId::of::() == TypeId::of::(), } @@ -641,7 +632,7 @@ impl<'a> Target<'a> { )) })?; - let value = &mut *value.write_lock::().expect("`Blob`"); + let value = &mut *value.write_lock::().expect("`Blob`"); let index = *index; @@ -1372,7 +1363,8 @@ impl Engine { if let Some(mut new_val) = try_setter { // Try to call index setter if value is changed - let hash_set = FnCallHashes::from_native(mods.fn_hash_idx_set()); + let hash_set = + crate::ast::FnCallHashes::from_native(mods.hash_idx_set()); let args = &mut [target, &mut idx_val_for_setter, &mut new_val]; if let Err(err) = self.exec_fn_call( @@ -1418,7 +1410,8 @@ impl Engine { if let Some(mut new_val) = try_setter { // Try to call index setter - let hash_set = FnCallHashes::from_native(mods.fn_hash_idx_set()); + let hash_set = + crate::ast::FnCallHashes::from_native(mods.hash_idx_set()); let args = &mut [target, &mut idx_val_for_setter, &mut new_val]; self.exec_fn_call( @@ -1461,7 +1454,7 @@ impl Engine { unreachable!("function call in dot chain should not be namespace-qualified") } // {xxx:map}.id op= ??? - Expr::Property(x) if target.is::() && new_val.is_some() => { + Expr::Property(x) if target.is::() && new_val.is_some() => { let (name, pos) = &x.2; let ((new_val, new_pos), (op_info, op_pos)) = new_val.expect("`Some`"); let index = name.into(); @@ -1479,7 +1472,7 @@ impl Engine { Ok((Dynamic::UNIT, true)) } // {xxx:map}.id - Expr::Property(x) if target.is::() => { + Expr::Property(x) if target.is::() => { let (name, pos) = &x.2; let index = name.into(); let val = self.get_indexed_mut( @@ -1493,7 +1486,7 @@ impl Engine { let ((mut new_val, new_pos), (op_info, op_pos)) = new_val.expect("`Some`"); if op_info.is_some() { - let hash = FnCallHashes::from_native(*hash_get); + let hash = crate::ast::FnCallHashes::from_native(*hash_get); let args = &mut [target.as_mut()]; let (mut orig_val, _) = self .exec_fn_call( @@ -1537,7 +1530,7 @@ impl Engine { new_val = orig_val; } - let hash = FnCallHashes::from_native(*hash_set); + let hash = crate::ast::FnCallHashes::from_native(*hash_set); let args = &mut [target.as_mut(), &mut new_val]; self.exec_fn_call( mods, state, lib, setter, hash, args, is_ref_mut, true, *pos, None, @@ -1547,7 +1540,8 @@ impl Engine { // Try an indexer if property does not exist EvalAltResult::ErrorDotExpr(_, _) => { let args = &mut [target, &mut name.into(), &mut new_val]; - let hash_set = FnCallHashes::from_native(mods.fn_hash_idx_set()); + let hash_set = + crate::ast::FnCallHashes::from_native(mods.hash_idx_set()); let pos = Position::NONE; self.exec_fn_call( @@ -1567,7 +1561,7 @@ impl Engine { // xxx.id Expr::Property(x) => { let ((getter, hash_get), _, (name, pos)) = x.as_ref(); - let hash = FnCallHashes::from_native(*hash_get); + let hash = crate::ast::FnCallHashes::from_native(*hash_get); let args = &mut [target.as_mut()]; self.exec_fn_call( mods, state, lib, getter, hash, args, is_ref_mut, true, *pos, None, @@ -1597,7 +1591,7 @@ impl Engine { } // {xxx:map}.sub_lhs[expr] | {xxx:map}.sub_lhs.expr Expr::Index(x, term, x_pos) | Expr::Dot(x, term, x_pos) - if target.is::() => + if target.is::() => { let val_target = &mut match x.lhs { Expr::Property(ref p) => { @@ -1641,8 +1635,8 @@ impl Engine { let ((getter, hash_get), (setter, hash_set), (name, pos)) = p.as_ref(); let rhs_chain = match_chaining_type(rhs); - let hash_get = FnCallHashes::from_native(*hash_get); - let hash_set = FnCallHashes::from_native(*hash_set); + let hash_get = crate::ast::FnCallHashes::from_native(*hash_get); + let hash_set = crate::ast::FnCallHashes::from_native(*hash_set); let mut arg_values = [target.as_mut(), &mut Dynamic::UNIT.clone()]; let args = &mut arg_values[..1]; @@ -1705,9 +1699,10 @@ impl Engine { EvalAltResult::ErrorDotExpr(_, _) => { let args = &mut [target.as_mut(), &mut name.into(), val]; - let hash_set = FnCallHashes::from_native( - mods.fn_hash_idx_set(), - ); + let hash_set = + crate::ast::FnCallHashes::from_native( + mods.hash_idx_set(), + ); self.exec_fn_call( mods, state, lib, FN_IDX_SET, hash_set, args, is_ref_mut, true, *pos, None, level, @@ -2160,7 +2155,7 @@ impl Engine { _ if use_indexers => { let args = &mut [target, &mut idx]; - let hash_get = FnCallHashes::from_native(mods.fn_hash_idx_get()); + let hash_get = crate::ast::FnCallHashes::from_native(mods.hash_idx_get()); let idx_pos = Position::NONE; self.exec_fn_call( @@ -2267,7 +2262,7 @@ impl Engine { Expr::Array(x, _) => Ok(x .iter() .try_fold( - Array::with_capacity(x.len()), + crate::Array::with_capacity(x.len()), |mut arr, item| -> Result<_, Box> { arr.push( self.eval_expr(scope, mods, state, lib, this_ptr, item, level)? @@ -2950,7 +2945,7 @@ impl Engine { } #[cfg(not(feature = "no_object"))] _ => { - let mut err_map = Map::new(); + let mut err_map = crate::Map::new(); let err_pos = err.take_position(); err_map.insert("message".into(), err.to_string().into()); diff --git a/src/func/call.rs b/src/func/call.rs index 4f0e1018..a4f679e9 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -23,9 +23,6 @@ use std::{ mem, }; -#[cfg(not(feature = "no_object"))] -use crate::Map; - /// Arguments to a function call, which is a list of [`&mut Dynamic`][Dynamic]. pub type FnCallArgs<'a> = [&'a mut Dynamic]; @@ -841,7 +838,7 @@ impl Engine { // Check if it is a map method call in OOP style #[cfg(not(feature = "no_object"))] - if let Some(map) = target.read_lock::() { + if let Some(map) = target.read_lock::() { if let Some(val) = map.get(fn_name) { if let Some(fn_ptr) = val.read_lock::() { // Remap the function name diff --git a/src/module/mod.rs b/src/module/mod.rs index 144beb3d..8152f0a9 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -23,12 +23,6 @@ use std::{ ops::{Add, AddAssign, Deref, DerefMut}, }; -#[cfg(not(feature = "no_index"))] -use crate::Array; - -#[cfg(not(feature = "no_object"))] -use crate::Map; - /// A type representing the namespace of a function. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum FnNamespace { @@ -942,7 +936,7 @@ impl Module { /// /// # Panics /// - /// Panics if the type is [`Array`] or [`Map`]. + /// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map]. /// Indexers for arrays, object maps and strings cannot be registered. /// /// # Function Metadata @@ -971,11 +965,11 @@ impl Module { F: Fn(&mut A, B) -> Result> + SendSync + 'static, { #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for arrays."); } #[cfg(not(feature = "no_object"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for object maps."); } if TypeId::of::() == TypeId::of::() @@ -1003,7 +997,7 @@ impl Module { /// /// # Panics /// - /// Panics if the type is [`Array`] or [`Map`]. + /// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map]. /// Indexers for arrays, object maps and strings cannot be registered. /// /// # Function Metadata @@ -1032,11 +1026,11 @@ impl Module { F: Fn(&mut A, B, C) -> Result<(), Box> + SendSync + 'static, { #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for arrays."); } #[cfg(not(feature = "no_object"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { panic!("Cannot register indexer for object maps."); } if TypeId::of::() == TypeId::of::() @@ -1064,7 +1058,7 @@ impl Module { /// /// # Panics /// - /// Panics if the type is [`Array`] or [`Map`]. + /// Panics if the type is [`Array`][crate::Array] or [`Map`][crate::Map]. /// Indexers for arrays, object maps and strings cannot be registered. /// /// # Function Metadata diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 1e65f66f..0479b937 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -6,7 +6,7 @@ use crate::{def_package, Position, INT}; use std::prelude::v1::*; #[cfg(not(feature = "no_float"))] -use crate::{EvalAltResult, FLOAT}; +use crate::FLOAT; #[cfg(feature = "no_std")] #[cfg(not(feature = "no_float"))] diff --git a/src/parser.rs b/src/parser.rs index 4ed28b47..92e36a2b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -27,12 +27,6 @@ use std::{ ops::AddAssign, }; -#[cfg(not(feature = "no_float"))] -use crate::{custom_syntax::markers::CUSTOM_SYNTAX_MARKER_FLOAT, FLOAT}; - -#[cfg(not(feature = "no_function"))] -use crate::FnAccess; - type PERR = ParseErrorType; type FunctionsLib = BTreeMap>; @@ -1573,7 +1567,7 @@ fn parse_unary( .map(|i| Expr::IntegerConstant(i, pos)) .or_else(|| { #[cfg(not(feature = "no_float"))] - return Some(Expr::FloatConstant((-(num as FLOAT)).into(), pos)); + return Some(Expr::FloatConstant((-(num as crate::FLOAT)).into(), pos)); #[cfg(feature = "no_float")] return None; }) @@ -2173,19 +2167,23 @@ fn parse_custom_syntax( } }, #[cfg(not(feature = "no_float"))] - CUSTOM_SYNTAX_MARKER_FLOAT => match input.next().expect(NEVER_ENDS) { - (Token::FloatConstant(f), pos) => { - inputs.push(Expr::FloatConstant(f, pos)); - segments.push(f.to_string().into()); - tokens.push(state.get_identifier(CUSTOM_SYNTAX_MARKER_FLOAT)); + crate::custom_syntax::markers::CUSTOM_SYNTAX_MARKER_FLOAT => { + match input.next().expect(NEVER_ENDS) { + (Token::FloatConstant(f), pos) => { + inputs.push(Expr::FloatConstant(f, pos)); + segments.push(f.to_string().into()); + tokens.push(state.get_identifier( + crate::custom_syntax::markers::CUSTOM_SYNTAX_MARKER_FLOAT, + )); + } + (_, pos) => { + return Err(PERR::MissingSymbol( + "Expecting a floating-point number".to_string(), + ) + .into_err(pos)) + } } - (_, pos) => { - return Err(PERR::MissingSymbol( - "Expecting a floating-point number".to_string(), - ) - .into_err(pos)) - } - }, + } CUSTOM_SYNTAX_MARKER_STRING => match input.next().expect(NEVER_ENDS) { (Token::StringConstant(s), pos) => { let s: ImmutableString = state.get_identifier(s).into(); @@ -2849,9 +2847,9 @@ fn parse_stmt( Token::Fn | Token::Private => { let access = if matches!(token, Token::Private) { eat_token(input, Token::Private); - FnAccess::Private + crate::FnAccess::Private } else { - FnAccess::Public + crate::FnAccess::Public }; match input.next().expect(NEVER_ENDS) { @@ -3055,7 +3053,7 @@ fn parse_fn( input: &mut TokenStream, state: &mut ParseState, lib: &mut FunctionsLib, - access: FnAccess, + access: crate::FnAccess, settings: ParseSettings, #[cfg(not(feature = "no_function"))] #[cfg(feature = "metadata")] @@ -3281,7 +3279,7 @@ fn parse_anon_fn( // Define the function let script = ScriptFnDef { name: fn_name.clone(), - access: FnAccess::Public, + access: crate::FnAccess::Public, params, body: body.into(), lib: None, diff --git a/src/serde/de.rs b/src/serde/de.rs index 6d299ec2..bd055ce0 100644 --- a/src/serde/de.rs +++ b/src/serde/de.rs @@ -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::() + .downcast_ref::() .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>(self, _visitor: V) -> Result> { #[cfg(not(feature = "no_index"))] - return self.value.downcast_ref::().map_or_else( + return self.value.downcast_ref::().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>(self, _visitor: V) -> Result> { #[cfg(not(feature = "no_object"))] - return self.value.downcast_ref::().map_or_else( + return self.value.downcast_ref::().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::() { + if let Some(map) = self.value.downcast_ref::() { let mut iter = map.iter(); let first = iter.next(); let second = iter.next(); diff --git a/src/serde/deserialize.rs b/src/serde/deserialize.rs index de970d54..320a01f4 100644 --- a/src/serde/deserialize.rs +++ b/src/serde/deserialize.rs @@ -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>(self, mut seq: A) -> Result { - let mut arr = Array::new(); + fn visit_seq>(self, mut seq: A) -> Result { + 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>(self, mut map: M) -> Result { - let mut m = Map::new(); + fn visit_map>(self, mut map: M) -> Result { + let mut m = crate::Map::new(); while let Some((k, v)) = map.next_entry::<&str, _>()? { m.insert(k.into(), v); diff --git a/src/serde/metadata.rs b/src/serde/metadata.rs index 0f79d941..6470e528 100644 --- a/src/serde/metadata.rs +++ b/src/serde/metadata.rs @@ -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}; diff --git a/src/serde/mod.rs b/src/serde/mod.rs index f391d694..42b57e6e 100644 --- a/src/serde/mod.rs +++ b/src/serde/mod.rs @@ -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; diff --git a/src/serde/ser.rs b/src/serde/ser.rs index 5211f219..b415d8fc 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -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) -> Result> { #[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) -> Result> { #[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::().unwrap(); + let arr = self._value.downcast_mut::().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::().unwrap(); + let arr = self._value.downcast_mut::().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::().unwrap(); + let arr = self._value.downcast_mut::().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::().unwrap(); + let map = self._value.downcast_mut::().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::().unwrap(); + let map = self._value.downcast_mut::().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::().unwrap(); + let map = self._value.downcast_mut::().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()) } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 91aa892f..8bdcc508 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -19,15 +19,6 @@ use std::{ str::{Chars, FromStr}, }; -#[cfg(not(feature = "no_float"))] -use crate::{ast::FloatWrapper, FLOAT}; - -#[cfg(feature = "decimal")] -use rust_decimal::Decimal; - -#[cfg(not(feature = "no_function"))] -use crate::engine::KEYWORD_IS_DEF_FN; - /// _(internals)_ A type containing commands to control the tokenizer. #[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)] pub struct TokenizerControlBlock { @@ -305,12 +296,12 @@ pub enum Token { /// /// Reserved under the `no_float` feature. #[cfg(not(feature = "no_float"))] - FloatConstant(FloatWrapper), - /// A [`Decimal`] constant. + FloatConstant(crate::ast::FloatWrapper), + /// A [`Decimal`][rust_decimal::Decimal] constant. /// /// Requires the `decimal` feature. #[cfg(feature = "decimal")] - DecimalConstant(Decimal), + DecimalConstant(rust_decimal::Decimal), /// An identifier. Identifier(Box), /// A character constant. @@ -789,7 +780,7 @@ impl Token { } #[cfg(not(feature = "no_function"))] - KEYWORD_IS_DEF_FN => Reserved(syntax.into()), + crate::engine::KEYWORD_IS_DEF_FN => Reserved(syntax.into()), _ => return None, }) @@ -1534,18 +1525,20 @@ fn get_next_token_inner( // If integer parsing is unnecessary, try float instead #[cfg(not(feature = "no_float"))] - let num = - num.or_else(|_| FloatWrapper::from_str(&out).map(Token::FloatConstant)); + let num = num.or_else(|_| { + crate::ast::FloatWrapper::from_str(&out).map(Token::FloatConstant) + }); // Then try decimal #[cfg(feature = "decimal")] - let num = - num.or_else(|_| Decimal::from_str(&out).map(Token::DecimalConstant)); + let num = num.or_else(|_| { + rust_decimal::Decimal::from_str(&out).map(Token::DecimalConstant) + }); // Then try decimal in scientific notation #[cfg(feature = "decimal")] let num = num.or_else(|_| { - Decimal::from_scientific(&out).map(Token::DecimalConstant) + rust_decimal::Decimal::from_scientific(&out).map(Token::DecimalConstant) }); num.unwrap_or_else(|_| { @@ -1990,7 +1983,7 @@ pub fn is_keyword_function(name: impl AsRef) -> bool { | KEYWORD_FN_PTR_CALL | KEYWORD_FN_PTR_CURRY | KEYWORD_IS_DEF_VAR => true, #[cfg(not(feature = "no_function"))] - KEYWORD_IS_DEF_FN => true, + crate::engine::KEYWORD_IS_DEF_FN => true, _ => false, } diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index af4e73e4..9f142049 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -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, Tag, AccessMode), + Float(crate::ast::FloatWrapper, Tag, AccessMode), /// _(decimal)_ A fixed-precision decimal value. /// Exported under the `decimal` feature only. #[cfg(feature = "decimal")] - Decimal(Box, Tag, AccessMode), + Decimal(Box, Tag, AccessMode), /// An array value. /// /// Not available under `no_index`. #[cfg(not(feature = "no_index"))] - Array(Box, Tag, AccessMode), + Array(Box, Tag, AccessMode), /// An blob (byte array). /// /// Not available under `no_index`. #[cfg(not(feature = "no_index"))] - Blob(Box, Tag, AccessMode), + Blob(Box, Tag, AccessMode), /// An object map value. /// /// Not available under `no_object`. #[cfg(not(feature = "no_object"))] - Map(Box, Tag, AccessMode), + Map(Box, Tag, AccessMode), /// A function pointer. FnPtr(Box, Tag, AccessMode), /// A timestamp value. @@ -411,15 +398,15 @@ impl Dynamic { Union::Char(_, _, _) => TypeId::of::(), Union::Int(_, _, _) => TypeId::of::(), #[cfg(not(feature = "no_float"))] - Union::Float(_, _, _) => TypeId::of::(), + Union::Float(_, _, _) => TypeId::of::(), #[cfg(feature = "decimal")] - Union::Decimal(_, _, _) => TypeId::of::(), + Union::Decimal(_, _, _) => TypeId::of::(), #[cfg(not(feature = "no_index"))] - Union::Array(_, _, _) => TypeId::of::(), + Union::Array(_, _, _) => TypeId::of::(), #[cfg(not(feature = "no_index"))] - Union::Blob(_, _, _) => TypeId::of::(), + Union::Blob(_, _, _) => TypeId::of::(), #[cfg(not(feature = "no_object"))] - Union::Map(_, _, _) => TypeId::of::(), + Union::Map(_, _, _) => TypeId::of::(), Union::FnPtr(_, _, _) => TypeId::of::(), #[cfg(not(feature = "no_std"))] Union::TimeStamp(_, _, _) => TypeId::of::(), @@ -450,7 +437,7 @@ impl Dynamic { Union::Char(_, _, _) => "char", Union::Int(_, _, _) => type_name::(), #[cfg(not(feature = "no_float"))] - Union::Float(_, _, _) => type_name::(), + Union::Float(_, _, _) => type_name::(), #[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::() { + if name == type_name::() { return "decimal"; } #[cfg(not(feature = "no_index"))] - if name == type_name::() { + if name == type_name::() { return "array"; } #[cfg(not(feature = "no_index"))] - if name == type_name::() { + if name == type_name::() { return "blob"; } #[cfg(not(feature = "no_object"))] - if name == type_name::() { + if name == type_name::() { 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`][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`][Vec] does not get automatically converted to an [`Array`][crate::Array], but will be a generic /// restricted trait object instead, because [`Vec`][Vec] is not a supported standard type. /// /// Similarly, passing in a [`HashMap`][std::collections::HashMap] or - /// [`BTreeMap`][std::collections::BTreeMap] will not get a [`Map`] but a trait object. + /// [`BTreeMap`][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::().expect(CHECKED)).into(); } #[cfg(not(feature = "no_float"))] - if TypeId::of::() == TypeId::of::() { - return (*val.downcast_ref::().expect(CHECKED)).into(); + if TypeId::of::() == TypeId::of::() { + return (*val.downcast_ref::().expect(CHECKED)).into(); } #[cfg(feature = "decimal")] - if TypeId::of::() == TypeId::of::() { - return (*val.downcast_ref::().expect(CHECKED)).into(); + if TypeId::of::() == TypeId::of::() { + return (*val.downcast_ref::().expect(CHECKED)).into(); } if TypeId::of::() == TypeId::of::() { return (*val.downcast_ref::().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::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { 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::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { 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::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { 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::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { 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::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { 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::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Float(ref value, _, _) => value.as_ref().as_any().downcast_ref::(), _ => None, }; } #[cfg(feature = "decimal")] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Decimal(ref value, _, _) => value.as_ref().as_any().downcast_ref::(), _ => None, @@ -1684,21 +1671,21 @@ impl Dynamic { }; } #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Array(ref value, _, _) => value.as_ref().as_any().downcast_ref::(), _ => None, }; } #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Blob(ref value, _, _) => value.as_ref().as_any().downcast_ref::(), _ => None, }; } #[cfg(not(feature = "no_object"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Map(ref value, _, _) => value.as_ref().as_any().downcast_ref::(), _ => None, @@ -1750,7 +1737,7 @@ impl Dynamic { }; } #[cfg(not(feature = "no_float"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Float(ref mut value, _, _) => { value.as_mut().as_mut_any().downcast_mut::() @@ -1759,7 +1746,7 @@ impl Dynamic { }; } #[cfg(feature = "decimal")] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Decimal(ref mut value, _, _) => { value.as_mut().as_mut_any().downcast_mut::() @@ -1786,7 +1773,7 @@ impl Dynamic { }; } #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Array(ref mut value, _, _) => { value.as_mut().as_mut_any().downcast_mut::() @@ -1795,14 +1782,14 @@ impl Dynamic { }; } #[cfg(not(feature = "no_index"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Blob(ref mut value, _, _) => value.as_mut().as_mut_any().downcast_mut::(), _ => None, }; } #[cfg(not(feature = "no_object"))] - if TypeId::of::() == TypeId::of::() { + if TypeId::of::() == TypeId::of::() { return match self.0 { Union::Map(ref mut value, _, _) => value.as_mut().as_mut_any().downcast_mut::(), _ => 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 { + pub fn as_float(&self) -> Result { 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 { + pub fn as_decimal(&self) -> Result { match self.0 { Union::Decimal(ref n, _, _) => Ok(**n), #[cfg(not(feature = "no_closure"))] @@ -1979,23 +1966,23 @@ impl From for Dynamic { } } #[cfg(not(feature = "no_float"))] -impl From for Dynamic { +impl From 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> for Dynamic { +impl From> for Dynamic { #[inline(always)] - fn from(value: FloatWrapper) -> Self { + fn from(value: crate::ast::FloatWrapper) -> Self { Self(Union::Float(value, DEFAULT_TAG_VALUE, ReadWrite)) } } #[cfg(feature = "decimal")] -impl From for Dynamic { +impl From 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 std::iter::FromIterator 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 { + pub fn into_array(self) -> Result { 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`]. #[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`]. /// Returns the name of the actual type if the cast fails. #[inline(always)] - pub fn into_blob(self) -> Result { + pub fn into_blob(self) -> Result { 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)) } }