Reduce feature gates.
This commit is contained in:
parent
5b64e0b383
commit
2a7a648429
@ -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<str>,
|
||||
has_null: bool,
|
||||
) -> Result<Map, Box<crate::EvalAltResult>> {
|
||||
) -> Result<crate::Map, Box<crate::EvalAltResult>> {
|
||||
use crate::tokenizer::Token;
|
||||
|
||||
fn parse_json_inner(
|
||||
engine: &Engine,
|
||||
json: &str,
|
||||
has_null: bool,
|
||||
) -> Result<Map, Box<crate::EvalAltResult>> {
|
||||
) -> Result<crate::Map, Box<crate::EvalAltResult>> {
|
||||
let mut scope = Scope::new();
|
||||
let json_text = json.trim_start();
|
||||
let scripts = if json_text.starts_with(Token::MapStart.literal_syntax()) {
|
||||
|
@ -4,7 +4,6 @@ use crate::{
|
||||
Dynamic, Engine, EvalAltResult, FnPtr, ImmutableString, NativeCallContext, RhaiResult, Scope,
|
||||
AST,
|
||||
};
|
||||
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
|
@ -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`.
|
||||
|
@ -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::<T>() == TypeId::of::<Array>() {
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
|
||||
panic!("Cannot register indexer for arrays.");
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
if TypeId::of::<T>() == TypeId::of::<Map>() {
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
|
||||
panic!("Cannot register indexer for object maps.");
|
||||
}
|
||||
if TypeId::of::<T>() == TypeId::of::<String>()
|
||||
@ -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<V, Box<EvalAltResult>> + SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
if TypeId::of::<T>() == TypeId::of::<Array>() {
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
|
||||
panic!("Cannot register indexer for arrays.");
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
if TypeId::of::<T>() == TypeId::of::<Map>() {
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
|
||||
panic!("Cannot register indexer for object maps.");
|
||||
}
|
||||
if TypeId::of::<T>() == TypeId::of::<String>()
|
||||
@ -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::<T>() == TypeId::of::<Array>() {
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
|
||||
panic!("Cannot register indexer for arrays.");
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
if TypeId::of::<T>() == TypeId::of::<Map>() {
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
|
||||
panic!("Cannot register indexer for object maps.");
|
||||
}
|
||||
if TypeId::of::<T>() == TypeId::of::<String>()
|
||||
@ -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<EvalAltResult>> + SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
if TypeId::of::<T>() == TypeId::of::<Array>() {
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
|
||||
panic!("Cannot register indexer for arrays.");
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
if TypeId::of::<T>() == TypeId::of::<Map>() {
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Map>() {
|
||||
panic!("Cannot register indexer for object maps.");
|
||||
}
|
||||
if TypeId::of::<T>() == TypeId::of::<String>()
|
||||
@ -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.
|
||||
///
|
||||
|
18
src/ast.rs
18
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>(F);
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl Hash for FloatWrapper<FLOAT> {
|
||||
impl Hash for FloatWrapper<crate::FLOAT> {
|
||||
#[inline(always)]
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.0.to_ne_bytes().hash(state);
|
||||
@ -2022,11 +2016,11 @@ impl<F: Float> FloatWrapper<F> {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
impl FloatWrapper<FLOAT> {
|
||||
impl FloatWrapper<crate::FLOAT> {
|
||||
/// 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<FLOAT> {
|
||||
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<Dynamic>, Position),
|
||||
/// Boolean constant.
|
||||
@ -2048,7 +2042,7 @@ pub enum Expr {
|
||||
///
|
||||
/// Not available under `no_float`.
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
FloatConstant(FloatWrapper<FLOAT>, Position),
|
||||
FloatConstant(FloatWrapper<crate::FLOAT>, 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")),
|
||||
|
@ -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::<T>() == TypeId::of::<bool>(),
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Self::BlobByte(_, _, _) => TypeId::of::<T>() == TypeId::of::<Blob>(),
|
||||
Self::BlobByte(_, _, _) => TypeId::of::<T>() == TypeId::of::<crate::Blob>(),
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
Self::StringChar(_, _, _) => TypeId::of::<T>() == TypeId::of::<char>(),
|
||||
}
|
||||
@ -641,7 +632,7 @@ impl<'a> Target<'a> {
|
||||
))
|
||||
})?;
|
||||
|
||||
let value = &mut *value.write_lock::<Blob>().expect("`Blob`");
|
||||
let value = &mut *value.write_lock::<crate::Blob>().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::<Map>() && new_val.is_some() => {
|
||||
Expr::Property(x) if target.is::<crate::Map>() && 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::<Map>() => {
|
||||
Expr::Property(x) if target.is::<crate::Map>() => {
|
||||
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::<Map>() =>
|
||||
if target.is::<crate::Map>() =>
|
||||
{
|
||||
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,8 +1699,9 @@ 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,
|
||||
@ -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<EvalAltResult>> {
|
||||
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());
|
||||
|
@ -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::<Map>() {
|
||||
if let Some(map) = target.read_lock::<crate::Map>() {
|
||||
if let Some(val) = map.get(fn_name) {
|
||||
if let Some(fn_ptr) = val.read_lock::<FnPtr>() {
|
||||
// Remap the function name
|
||||
|
@ -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<T, Box<EvalAltResult>> + SendSync + 'static,
|
||||
{
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
if TypeId::of::<A>() == TypeId::of::<Array>() {
|
||||
if TypeId::of::<A>() == TypeId::of::<crate::Array>() {
|
||||
panic!("Cannot register indexer for arrays.");
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
if TypeId::of::<A>() == TypeId::of::<Map>() {
|
||||
if TypeId::of::<A>() == TypeId::of::<crate::Map>() {
|
||||
panic!("Cannot register indexer for object maps.");
|
||||
}
|
||||
if TypeId::of::<A>() == TypeId::of::<String>()
|
||||
@ -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<EvalAltResult>> + SendSync + 'static,
|
||||
{
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
if TypeId::of::<A>() == TypeId::of::<Array>() {
|
||||
if TypeId::of::<A>() == TypeId::of::<crate::Array>() {
|
||||
panic!("Cannot register indexer for arrays.");
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
if TypeId::of::<A>() == TypeId::of::<Map>() {
|
||||
if TypeId::of::<A>() == TypeId::of::<crate::Map>() {
|
||||
panic!("Cannot register indexer for object maps.");
|
||||
}
|
||||
if TypeId::of::<A>() == TypeId::of::<String>()
|
||||
@ -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
|
||||
|
@ -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"))]
|
||||
|
@ -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<u64, Shared<ScriptFnDef>>;
|
||||
@ -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,11 +2167,14 @@ fn parse_custom_syntax(
|
||||
}
|
||||
},
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
CUSTOM_SYNTAX_MARKER_FLOAT => match input.next().expect(NEVER_ENDS) {
|
||||
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(CUSTOM_SYNTAX_MARKER_FLOAT));
|
||||
tokens.push(state.get_identifier(
|
||||
crate::custom_syntax::markers::CUSTOM_SYNTAX_MARKER_FLOAT,
|
||||
));
|
||||
}
|
||||
(_, pos) => {
|
||||
return Err(PERR::MissingSymbol(
|
||||
@ -2185,7 +2182,8 @@ fn parse_custom_syntax(
|
||||
)
|
||||
.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,
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
@ -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};
|
||||
|
@ -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;
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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<FLOAT>),
|
||||
/// A [`Decimal`] constant.
|
||||
FloatConstant(crate::ast::FloatWrapper<crate::FLOAT>),
|
||||
/// A [`Decimal`][rust_decimal::Decimal] constant.
|
||||
///
|
||||
/// Requires the `decimal` feature.
|
||||
#[cfg(feature = "decimal")]
|
||||
DecimalConstant(Decimal),
|
||||
DecimalConstant(rust_decimal::Decimal),
|
||||
/// An identifier.
|
||||
Identifier(Box<str>),
|
||||
/// 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<str>) -> 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,
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user