Add smartstring default feature.
This commit is contained in:
parent
fc6c5ecd00
commit
07efdddba3
@ -17,13 +17,12 @@ categories = ["no-std", "embedded", "wasm", "parser-implementations"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
smallvec = { version = "1.6", default-features = false, features = ["union"] }
|
smallvec = { version = "1.6", default-features = false, features = ["union"] }
|
||||||
smartstring = { version = "0.2.6" }
|
|
||||||
ahash = { version = "0.7", default-features = false }
|
ahash = { version = "0.7", default-features = false }
|
||||||
num-traits = { version = "0.2", default_features = false }
|
num-traits = { version = "0.2", default_features = false }
|
||||||
rhai_codegen = { version = "0.3.4", path = "codegen", features = ["metadata"] }
|
rhai_codegen = { version = "0.3.4", path = "codegen", features = ["metadata"] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = ["smartstring"]
|
||||||
unchecked = [] # unchecked arithmetic
|
unchecked = [] # unchecked arithmetic
|
||||||
sync = [] # restrict to only types that implement Send + Sync
|
sync = [] # restrict to only types that implement Send + Sync
|
||||||
no_optimize = [] # no script optimizer
|
no_optimize = [] # no script optimizer
|
||||||
@ -76,6 +75,10 @@ default_features = false
|
|||||||
features = ["alloc"]
|
features = ["alloc"]
|
||||||
optional = true
|
optional = true
|
||||||
|
|
||||||
|
[dependencies.smartstring]
|
||||||
|
version = "0.2.6"
|
||||||
|
optional = true
|
||||||
|
|
||||||
[dependencies.unicode-xid]
|
[dependencies.unicode-xid]
|
||||||
version = "0.2"
|
version = "0.2"
|
||||||
default_features = false
|
default_features = false
|
||||||
|
@ -1480,6 +1480,10 @@ impl fmt::Debug for FloatWrapper {
|
|||||||
impl fmt::Display for FloatWrapper {
|
impl fmt::Display for FloatWrapper {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
#[cfg(feature = "no_std")]
|
||||||
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
use num_traits::Float;
|
||||||
|
|
||||||
let abs = self.0.abs();
|
let abs = self.0.abs();
|
||||||
if abs > 10000000000000.0 || abs < 0.0000000000001 {
|
if abs > 10000000000000.0 || abs < 0.0000000000001 {
|
||||||
write!(f, "{:e}", self.0)
|
write!(f, "{:e}", self.0)
|
||||||
|
@ -8,7 +8,7 @@ use crate::stdlib::{
|
|||||||
fmt,
|
fmt,
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
string::{String, ToString},
|
string::String,
|
||||||
};
|
};
|
||||||
use crate::{FnPtr, ImmutableString, INT};
|
use crate::{FnPtr, ImmutableString, INT};
|
||||||
|
|
||||||
@ -1673,9 +1673,10 @@ impl From<&ImmutableString> for Dynamic {
|
|||||||
value.clone().into()
|
value.clone().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<C: smartstring::SmartStringMode> From<&smartstring::SmartString<C>> for Dynamic {
|
#[cfg(feature = "smartstring")]
|
||||||
|
impl From<&crate::Identifier> for Dynamic {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from(value: &smartstring::SmartString<C>) -> Self {
|
fn from(value: &crate::Identifier) -> Self {
|
||||||
value.to_string().into()
|
value.to_string().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ use crate::optimize::OptimizationLevel;
|
|||||||
use crate::stdlib::{
|
use crate::stdlib::{
|
||||||
any::{type_name, TypeId},
|
any::{type_name, TypeId},
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
format,
|
|
||||||
string::String,
|
string::String,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -61,7 +60,7 @@ impl Engine {
|
|||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
let mut param_type_names: crate::StaticVec<_> = F::param_names()
|
let mut param_type_names: crate::StaticVec<_> = F::param_names()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|ty| format!("_: {}", self.map_type_name(ty)))
|
.map(|ty| crate::stdlib::format!("_: {}", self.map_type_name(ty)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
@ -121,7 +120,7 @@ impl Engine {
|
|||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
let param_type_names: crate::StaticVec<_> = F::param_names()
|
let param_type_names: crate::StaticVec<_> = F::param_names()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|ty| format!("_: {}", self.map_type_name(ty)))
|
.map(|ty| crate::stdlib::format!("_: {}", self.map_type_name(ty)))
|
||||||
.chain(crate::stdlib::iter::once(
|
.chain(crate::stdlib::iter::once(
|
||||||
self.map_type_name(F::return_type_name()).into(),
|
self.map_type_name(F::return_type_name()).into(),
|
||||||
))
|
))
|
||||||
@ -1167,7 +1166,7 @@ impl Engine {
|
|||||||
|
|
||||||
let mut f = crate::stdlib::fs::File::open(path.clone()).map_err(|err| {
|
let mut f = crate::stdlib::fs::File::open(path.clone()).map_err(|err| {
|
||||||
EvalAltResult::ErrorSystem(
|
EvalAltResult::ErrorSystem(
|
||||||
format!("Cannot open script file '{}'", path.to_string_lossy()),
|
crate::stdlib::format!("Cannot open script file '{}'", path.to_string_lossy()),
|
||||||
err.into(),
|
err.into(),
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
@ -1176,7 +1175,7 @@ impl Engine {
|
|||||||
|
|
||||||
f.read_to_string(&mut contents).map_err(|err| {
|
f.read_to_string(&mut contents).map_err(|err| {
|
||||||
EvalAltResult::ErrorSystem(
|
EvalAltResult::ErrorSystem(
|
||||||
format!("Cannot read script file '{}'", path.to_string_lossy()),
|
crate::stdlib::format!("Cannot read script file '{}'", path.to_string_lossy()),
|
||||||
err.into(),
|
err.into(),
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
@ -1991,7 +1990,10 @@ impl Engine {
|
|||||||
signatures.extend(self.global_namespace.gen_fn_signatures());
|
signatures.extend(self.global_namespace.gen_fn_signatures());
|
||||||
|
|
||||||
self.global_sub_modules.iter().for_each(|(name, m)| {
|
self.global_sub_modules.iter().for_each(|(name, m)| {
|
||||||
signatures.extend(m.gen_fn_signatures().map(|f| format!("{}::{}", name, f)))
|
signatures.extend(
|
||||||
|
m.gen_fn_signatures()
|
||||||
|
.map(|f| crate::stdlib::format!("{}::{}", name, f)),
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
if include_packages {
|
if include_packages {
|
||||||
|
@ -41,6 +41,10 @@ pub fn get_builtin_binary_op_fn(
|
|||||||
x: &Dynamic,
|
x: &Dynamic,
|
||||||
y: &Dynamic,
|
y: &Dynamic,
|
||||||
) -> Option<fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult> {
|
) -> Option<fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult> {
|
||||||
|
#[cfg(feature = "no_std")]
|
||||||
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
use num_traits::Float;
|
||||||
|
|
||||||
let type1 = x.type_id();
|
let type1 = x.type_id();
|
||||||
let type2 = y.type_id();
|
let type2 = y.type_id();
|
||||||
|
|
||||||
@ -411,6 +415,10 @@ pub fn get_builtin_op_assignment_fn(
|
|||||||
x: &Dynamic,
|
x: &Dynamic,
|
||||||
y: &Dynamic,
|
y: &Dynamic,
|
||||||
) -> Option<fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult> {
|
) -> Option<fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult> {
|
||||||
|
#[cfg(feature = "no_std")]
|
||||||
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
use num_traits::Float;
|
||||||
|
|
||||||
let type1 = x.type_id();
|
let type1 = x.type_id();
|
||||||
let type2 = y.type_id();
|
let type2 = y.type_id();
|
||||||
|
|
||||||
|
11
src/lib.rs
11
src/lib.rs
@ -135,6 +135,15 @@ pub use syntax::Expression;
|
|||||||
pub use token::Position;
|
pub use token::Position;
|
||||||
pub use utils::ImmutableString;
|
pub use utils::ImmutableString;
|
||||||
|
|
||||||
|
/// An identifier in Rhai. [`SmartString`](https://crates.io/crates/smartstring) is used because most
|
||||||
|
/// identifiers are ASCII and short, fewer than 23 characters, so they can be stored inline.
|
||||||
|
#[cfg(feature = "smartstring")]
|
||||||
|
pub type Identifier = smartstring::SmartString<smartstring::Compact>;
|
||||||
|
|
||||||
|
/// An identifier in Rhai.
|
||||||
|
#[cfg(not(feature = "smartstring"))]
|
||||||
|
pub type Identifier = ImmutableString;
|
||||||
|
|
||||||
/// A trait to enable registering Rust functions.
|
/// A trait to enable registering Rust functions.
|
||||||
/// This trait is no longer needed and will be removed in the future.
|
/// This trait is no longer needed and will be removed in the future.
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
@ -292,8 +301,6 @@ type StaticVec<T> = smallvec::SmallVec<[T; 4]>;
|
|||||||
#[cfg(feature = "internals")]
|
#[cfg(feature = "internals")]
|
||||||
pub type StaticVec<T> = smallvec::SmallVec<[T; 4]>;
|
pub type StaticVec<T> = smallvec::SmallVec<[T; 4]>;
|
||||||
|
|
||||||
pub type Identifier = smartstring::SmartString<smartstring::Compact>;
|
|
||||||
|
|
||||||
// Compiler guards against mutually-exclusive feature flags
|
// Compiler guards against mutually-exclusive feature flags
|
||||||
|
|
||||||
#[cfg(feature = "no_float")]
|
#[cfg(feature = "no_float")]
|
||||||
|
@ -7,6 +7,10 @@ use crate::{def_package, EvalAltResult, Position, INT};
|
|||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
use crate::FLOAT;
|
use crate::FLOAT;
|
||||||
|
|
||||||
|
#[cfg(feature = "no_std")]
|
||||||
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
use num_traits::Float;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn make_err(msg: impl Into<String>) -> Box<EvalAltResult> {
|
pub fn make_err(msg: impl Into<String>) -> Box<EvalAltResult> {
|
||||||
EvalAltResult::ErrorArithmetic(msg.into(), Position::NONE).into()
|
EvalAltResult::ErrorArithmetic(msg.into(), Position::NONE).into()
|
||||||
|
@ -12,6 +12,10 @@ use crate::result::EvalAltResult;
|
|||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
use crate::stdlib::format;
|
use crate::stdlib::format;
|
||||||
|
|
||||||
|
#[cfg(feature = "no_std")]
|
||||||
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
use num_traits::Float;
|
||||||
|
|
||||||
#[cfg(feature = "decimal")]
|
#[cfg(feature = "decimal")]
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
|
|
||||||
|
@ -55,6 +55,10 @@ mod print_debug_functions {
|
|||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
pub mod float_functions {
|
pub mod float_functions {
|
||||||
|
#[cfg(feature = "no_std")]
|
||||||
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
use num_traits::Float;
|
||||||
|
|
||||||
#[rhai_fn(name = "print", name = "to_string")]
|
#[rhai_fn(name = "print", name = "to_string")]
|
||||||
pub fn print_f64(number: f64) -> ImmutableString {
|
pub fn print_f64(number: f64) -> ImmutableString {
|
||||||
let abs = number.abs();
|
let abs = number.abs();
|
||||||
|
@ -166,7 +166,7 @@ impl<'e> ParseState<'e> {
|
|||||||
|
|
||||||
/// Get an interned string, creating one if it is not yet interned.
|
/// Get an interned string, creating one if it is not yet interned.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn get_interned_string(&mut self, text: impl AsRef<str>) -> Identifier {
|
pub fn get_interned_string(&mut self, text: impl AsRef<str> + Into<Identifier>) -> Identifier {
|
||||||
self.interned_strings.get(text)
|
self.interned_strings.get(text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
src/utils.rs
14
src/utils.rs
@ -590,6 +590,7 @@ impl PartialOrd<ImmutableString> for String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "smartstring")]
|
||||||
impl From<ImmutableString> for Identifier {
|
impl From<ImmutableString> for Identifier {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from(value: ImmutableString) -> Self {
|
fn from(value: ImmutableString) -> Self {
|
||||||
@ -597,6 +598,7 @@ impl From<ImmutableString> for Identifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "smartstring")]
|
||||||
impl From<Identifier> for ImmutableString {
|
impl From<Identifier> for ImmutableString {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from(value: Identifier) -> Self {
|
fn from(value: Identifier) -> Self {
|
||||||
@ -627,7 +629,15 @@ pub struct StringInterner(BTreeSet<Identifier>);
|
|||||||
impl StringInterner {
|
impl StringInterner {
|
||||||
/// Get an interned string, creating one if it is not yet interned.
|
/// Get an interned string, creating one if it is not yet interned.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn get(&mut self, text: impl AsRef<str>) -> Identifier {
|
pub fn get(&mut self, text: impl AsRef<str> + Into<Identifier>) -> Identifier {
|
||||||
text.as_ref().into()
|
#[cfg(feature = "smartstring")]
|
||||||
|
return text.as_ref().into();
|
||||||
|
|
||||||
|
#[cfg(not(feature = "smartstring"))]
|
||||||
|
return self.0.get(text.as_ref()).cloned().unwrap_or_else(|| {
|
||||||
|
let s: Identifier = text.into();
|
||||||
|
self.0.insert(s.clone());
|
||||||
|
s
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user