From a6a570131acb554ec1c99bab9bd5e84d9346d2e7 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 15 Oct 2022 12:37:42 +0800 Subject: [PATCH] Add no_time. --- CHANGELOG.md | 5 +++++ Cargo.toml | 1 + src/api/definitions/mod.rs | 1 + src/api/type_names.rs | 1 + src/lib.rs | 1 + src/packages/mod.rs | 1 + src/packages/pkg_std.rs | 2 +- src/packages/time_basic.rs | 1 + src/serde/de.rs | 1 + src/serde/serialize.rs | 2 ++ src/types/dynamic.rs | 22 +++++++++++++++++++++- src/types/mod.rs | 1 + 12 files changed, 37 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffcc3516..61d09c7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,11 @@ New features * This is necessary when using Rhai across shared-library boundaries. * A build script is used to extract the environment variable (`RHAI_AHASH_SEED`) and splice it into the source code before compilation. +### No Timestamps + +* A new feature, `no_time`, is added to disable support timestamps. +* This may be necessary when building for architectures without time support, such as raw WASM. + ### Serializable `Scope` * `Scope` is now serializable and deserializable via `serde`. diff --git a/Cargo.toml b/Cargo.toml index 5b506221..ff8a8b69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ no_function = ["no_closure"] # no script-defined functions (meaning no closur no_closure = [] # no automatic sharing and capture of anonymous functions to external variables no_module = [] # no modules no_custom_syntax = [] # no custom syntax or custom operators +no_time = [] # no timestamps unicode-xid-ident = ["unicode-xid"] # allow Unicode Standard Annex #31 for identifiers. metadata = ["serde", "serde_json", "rhai_codegen/metadata", "smartstring/serde"] # enable exporting functions metadata internals = [] # expose internal data structures diff --git a/src/api/definitions/mod.rs b/src/api/definitions/mod.rs index cd0a51c2..4ac1280d 100644 --- a/src/api/definitions/mod.rs +++ b/src/api/definitions/mod.rs @@ -556,6 +556,7 @@ fn def_type_name<'a>(ty: &'a str, engine: &'a Engine) -> Cow<'a, str> { let ty = ty.replace(type_name::(), "Map"); #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] let ty = ty.replace(type_name::(), "Instant"); let ty = ty.replace(type_name::(), "FnPtr"); diff --git a/src/api/type_names.rs b/src/api/type_names.rs index f426154b..eb0f9857 100644 --- a/src/api/type_names.rs +++ b/src/api/type_names.rs @@ -45,6 +45,7 @@ fn map_std_type_name(name: &str, shorthands: bool) -> &str { return if shorthands { "map" } else { "Map" }; } #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] if name == type_name::() || name == "Instant" { return if shorthands { "timestamp" } else { "Instant" }; } diff --git a/src/lib.rs b/src/lib.rs index 7b36c9cf..9a540e3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -205,6 +205,7 @@ pub use func::{NativeCallContext, RegisterNativeFunction}; pub use module::{FnNamespace, Module}; pub use tokenizer::Position; #[cfg(not(feature = "no_std"))] +#[cfg(not(feature = "no_time"))] pub use types::Instant; pub use types::{ Dynamic, EvalAltResult, FnPtr, ImmutableString, LexError, ParseError, ParseErrorType, Scope, diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 5a63e86b..c9f31bcf 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -39,6 +39,7 @@ pub use pkg_std::StandardPackage; pub use string_basic::BasicStringPackage; pub use string_more::MoreStringPackage; #[cfg(not(feature = "no_std"))] +#[cfg(not(feature = "no_time"))] pub use time_basic::BasicTimePackage; /// Trait that all packages must implement. diff --git a/src/packages/pkg_std.rs b/src/packages/pkg_std.rs index e7d4f1ff..cdc5fbbf 100644 --- a/src/packages/pkg_std.rs +++ b/src/packages/pkg_std.rs @@ -26,7 +26,7 @@ def_package! { #[cfg(not(feature = "no_index"))] BasicArrayPackage, #[cfg(not(feature = "no_index"))] BasicBlobPackage, #[cfg(not(feature = "no_object"))] BasicMapPackage, - #[cfg(not(feature = "no_std"))] BasicTimePackage, + #[cfg(all(not(feature = "no_std"), not(feature = "no_time")))] BasicTimePackage, MoreStringPackage { lib.standard = true; diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index 16ade536..e76cc06e 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -1,4 +1,5 @@ #![cfg(not(feature = "no_std"))] +#![cfg(not(feature = "no_time"))] use super::arithmetic::make_err as make_arithmetic_err; use crate::plugin::*; diff --git a/src/serde/de.rs b/src/serde/de.rs index 8f4cd237..4dd0b133 100644 --- a/src/serde/de.rs +++ b/src/serde/de.rs @@ -157,6 +157,7 @@ impl<'de> Deserializer<'de> for DynamicDeserializer<'de> { Union::Map(..) => self.deserialize_map(visitor), Union::FnPtr(..) => self.type_error(), #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(..) => self.type_error(), Union::Variant(ref value, ..) if value.is::() => self.deserialize_i8(visitor), diff --git a/src/serde/serialize.rs b/src/serde/serialize.rs index 69a280ef..509dad56 100644 --- a/src/serde/serialize.rs +++ b/src/serde/serialize.rs @@ -10,6 +10,7 @@ use std::prelude::v1::*; use serde::ser::SerializeMap; #[cfg(not(feature = "no_std"))] +#[cfg(not(feature = "no_time"))] use crate::types::dynamic::Variant; impl Serialize for Dynamic { @@ -66,6 +67,7 @@ impl Serialize for Dynamic { } Union::FnPtr(ref f, ..) => ser.serialize_str(f.fn_name()), #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(ref x, ..) => ser.serialize_str(x.as_ref().type_name()), Union::Variant(ref v, ..) => ser.serialize_str((***v).type_name()), diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index 6572dbe4..5643901a 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -15,10 +15,12 @@ use std::{ pub use super::Variant; #[cfg(not(feature = "no_std"))] +#[cfg(not(feature = "no_time"))] #[cfg(not(target_family = "wasm"))] pub use std::time::Instant; #[cfg(not(feature = "no_std"))] +#[cfg(not(feature = "no_time"))] #[cfg(target_family = "wasm")] pub use instant::Instant; @@ -85,6 +87,7 @@ pub enum Union { FnPtr(Box, Tag, AccessMode), /// A timestamp value. #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] TimeStamp(Box, Tag, AccessMode), /// Any type as a trait object. @@ -195,6 +198,7 @@ impl Dynamic { #[cfg(not(feature = "no_object"))] Union::Map(_, tag, _) => tag, #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(_, tag, _) => tag, #[cfg(not(feature = "no_closure"))] Union::Shared(_, tag, _) => tag, @@ -220,6 +224,7 @@ impl Dynamic { #[cfg(not(feature = "no_object"))] Union::Map(_, ref mut tag, _) => *tag = value, #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(_, ref mut tag, _) => *tag = value, #[cfg(not(feature = "no_closure"))] Union::Shared(_, ref mut tag, _) => *tag = value, @@ -292,6 +297,7 @@ impl Dynamic { return matches!(self.0, Union::FnPtr(..)); } #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] if TypeId::of::() == TypeId::of::() { return matches!(self.0, Union::TimeStamp(..)); } @@ -324,6 +330,7 @@ impl Dynamic { Union::Map(..) => TypeId::of::(), Union::FnPtr(..) => TypeId::of::(), #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(..) => TypeId::of::(), Union::Variant(ref v, ..) => (***v).type_id(), @@ -358,6 +365,7 @@ impl Dynamic { Union::Map(..) => "map", Union::FnPtr(..) => "Fn", #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(..) => "timestamp", Union::Variant(ref v, ..) => (***v).type_name(), @@ -408,6 +416,7 @@ impl Hash for Dynamic { Union::Variant(..) => unimplemented!("{} cannot be hashed", self.type_name()), #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(..) => unimplemented!("{} cannot be hashed", self.type_name()), } } @@ -433,6 +442,7 @@ impl fmt::Display for Dynamic { Union::Map(..) => fmt::Debug::fmt(self, f), Union::FnPtr(ref v, ..) => fmt::Display::fmt(v, f), #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(..) => f.write_str(""), Union::Variant(ref v, ..) => { @@ -538,6 +548,7 @@ impl fmt::Debug for Dynamic { } Union::FnPtr(ref v, ..) => fmt::Debug::fmt(v, f), #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(..) => write!(f, ""), Union::Variant(ref v, ..) => { @@ -636,6 +647,7 @@ impl Clone for Dynamic { Union::Map(ref v, tag, ..) => Self(Union::Map(v.clone(), tag, ReadWrite)), Union::FnPtr(ref v, tag, ..) => Self(Union::FnPtr(v.clone(), tag, ReadWrite)), #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(ref v, tag, ..) => Self(Union::TimeStamp(v.clone(), tag, ReadWrite)), Union::Variant(ref v, tag, ..) => Self(Union::Variant( @@ -876,8 +888,9 @@ impl Dynamic { } /// Create a new [`Dynamic`] from an [`Instant`]. /// - /// Not available under `no-std`. + /// Not available under `no-std` or `no_time`. #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] #[inline(always)] #[must_use] pub fn from_timestamp(value: Instant) -> Self { @@ -905,6 +918,7 @@ impl Dynamic { #[cfg(not(feature = "no_object"))] Union::Map(.., access) => access, #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(.., access) => access, #[cfg(not(feature = "no_closure"))] Union::Shared(.., access) => access, @@ -942,6 +956,7 @@ impl Dynamic { } } #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(.., ref mut access) => *access = typ, #[cfg(not(feature = "no_closure"))] Union::Shared(.., ref mut access) => *access = typ, @@ -1076,6 +1091,7 @@ impl Dynamic { reify!(value, |v: FnPtr| return v.into()); #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] reify!(value, |v: Instant| return v.into()); #[cfg(not(feature = "no_closure"))] reify!(value, |v: crate::Shared>| return v @@ -1169,6 +1185,7 @@ impl Dynamic { Union::Map(v, ..) => reify!(*v => Option), Union::FnPtr(v, ..) => reify!(*v => Option), #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] Union::TimeStamp(v, ..) => reify!(*v => Option), Union::Unit(v, ..) => reify!(v => Option), Union::Variant(v, ..) => (*v).as_boxed_any().downcast().ok().map(|x| *x), @@ -1468,6 +1485,7 @@ impl Dynamic { }; } #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] if TypeId::of::() == TypeId::of::() { return match self.0 { Union::TimeStamp(ref v, ..) => v.as_ref().as_any().downcast_ref::(), @@ -1566,6 +1584,7 @@ impl Dynamic { }; } #[cfg(not(feature = "no_std"))] + #[cfg(not(feature = "no_time"))] if TypeId::of::() == TypeId::of::() { return match self.0 { Union::TimeStamp(ref mut v, ..) => v.as_mut().as_any_mut().downcast_mut::(), @@ -1960,6 +1979,7 @@ impl From for Dynamic { } } #[cfg(not(feature = "no_std"))] +#[cfg(not(feature = "no_time"))] impl From for Dynamic { #[inline(always)] fn from(value: Instant) -> Self { diff --git a/src/types/mod.rs b/src/types/mod.rs index 475ca2c9..8de89ca8 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -15,6 +15,7 @@ pub use bloom_filter::BloomFilterU64; pub use custom_types::{CustomTypeInfo, CustomTypesCollection}; pub use dynamic::Dynamic; #[cfg(not(feature = "no_std"))] +#[cfg(not(feature = "no_time"))] pub use dynamic::Instant; pub use error::EvalAltResult; pub use fn_ptr::FnPtr;