Add no_time.

This commit is contained in:
Stephen Chung 2022-10-15 12:37:42 +08:00
parent 9b226f321e
commit a6a570131a
12 changed files with 37 additions and 2 deletions

View File

@ -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`.

View File

@ -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

View File

@ -556,6 +556,7 @@ fn def_type_name<'a>(ty: &'a str, engine: &'a Engine) -> Cow<'a, str> {
let ty = ty.replace(type_name::<crate::Map>(), "Map");
#[cfg(not(feature = "no_std"))]
#[cfg(not(feature = "no_time"))]
let ty = ty.replace(type_name::<crate::Instant>(), "Instant");
let ty = ty.replace(type_name::<FnPtr>(), "FnPtr");

View File

@ -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::<crate::Instant>() || name == "Instant" {
return if shorthands { "timestamp" } else { "Instant" };
}

View File

@ -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,

View File

@ -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.

View File

@ -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;

View File

@ -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::*;

View File

@ -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::<i8>() => self.deserialize_i8(visitor),

View File

@ -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()),

View File

@ -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<FnPtr>, Tag, AccessMode),
/// A timestamp value.
#[cfg(not(feature = "no_std"))]
#[cfg(not(feature = "no_time"))]
TimeStamp(Box<Instant>, 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::<T>() == TypeId::of::<crate::Instant>() {
return matches!(self.0, Union::TimeStamp(..));
}
@ -324,6 +330,7 @@ impl Dynamic {
Union::Map(..) => TypeId::of::<crate::Map>(),
Union::FnPtr(..) => TypeId::of::<FnPtr>(),
#[cfg(not(feature = "no_std"))]
#[cfg(not(feature = "no_time"))]
Union::TimeStamp(..) => TypeId::of::<Instant>(),
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("<timestamp>"),
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, "<timestamp>"),
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<crate::Locked<Self>>| return v
@ -1169,6 +1185,7 @@ impl Dynamic {
Union::Map(v, ..) => reify!(*v => Option<T>),
Union::FnPtr(v, ..) => reify!(*v => Option<T>),
#[cfg(not(feature = "no_std"))]
#[cfg(not(feature = "no_time"))]
Union::TimeStamp(v, ..) => reify!(*v => Option<T>),
Union::Unit(v, ..) => reify!(v => Option<T>),
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::<T>() == TypeId::of::<Instant>() {
return match self.0 {
Union::TimeStamp(ref v, ..) => v.as_ref().as_any().downcast_ref::<T>(),
@ -1566,6 +1584,7 @@ impl Dynamic {
};
}
#[cfg(not(feature = "no_std"))]
#[cfg(not(feature = "no_time"))]
if TypeId::of::<T>() == TypeId::of::<Instant>() {
return match self.0 {
Union::TimeStamp(ref mut v, ..) => v.as_mut().as_any_mut().downcast_mut::<T>(),
@ -1960,6 +1979,7 @@ impl From<FnPtr> for Dynamic {
}
}
#[cfg(not(feature = "no_std"))]
#[cfg(not(feature = "no_time"))]
impl From<Instant> for Dynamic {
#[inline(always)]
fn from(value: Instant) -> Self {

View File

@ -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;