Polyfiill some functions for decimal+no_float.

This commit is contained in:
Stephen Chung 2021-08-11 16:09:04 +08:00
parent 73c0ba4ec5
commit e0cae4546c
2 changed files with 26 additions and 1 deletions

View File

@ -1,6 +1,15 @@
Rhai Release Notes Rhai Release Notes
================== ==================
Version 1.1.0
=============
Enhancements
------------
* `parse_float()`, `PI()` and `E()` now defer to `Decimal` under `no_float` if `decimal` is turned on.
Version 1.0.2 Version 1.0.2
============= =============
@ -105,7 +114,7 @@ New features
* Each `Dynamic` value can now contain arbitrary data (type `i32`) in the form of a _tag_. This is to use up otherwise wasted space in the `Dynamic` type. * Each `Dynamic` value can now contain arbitrary data (type `i32`) in the form of a _tag_. This is to use up otherwise wasted space in the `Dynamic` type.
* A new internal feature `no_smartstring` to turn off `SmartString` for those rare cases that it is needed. * A new internal feature `no_smartstring` to turn off `SmartString` for those rare cases that it is needed.
* `DynamicReadLock` and `DynamicWriteLoc` are exposed under `internals`. * `DynamicReadLock` and `DynamicWriteLoc` are exposed under `internals`.
* `From<Shared<Locked<Dynamic>>>` is added for `Dynamic` mapping directly to a shared value, together with support for `Dynamic::from`. * `From< Shared< Locked<Dynamic> > >` is added for `Dynamic` mapping directly to a shared value, together with support for `Dynamic::from`.
* An indexer with string index acts as a _fallback_ to a property getter/setter. * An indexer with string index acts as a _fallback_ to a property getter/setter.
Enhancements Enhancements

View File

@ -309,6 +309,22 @@ mod decimal_functions {
Decimal, MathematicalOps, Decimal, MathematicalOps,
}; };
#[cfg(feature = "no_float")]
pub mod float_polyfills {
#[rhai_fn(name = "PI")]
pub fn pi() -> Decimal {
Decimal::PI
}
#[rhai_fn(name = "E")]
pub fn e() -> Decimal {
Decimal::E
}
#[rhai_fn(return_raw)]
pub fn parse_float(s: &str) -> Result<Decimal, Box<EvalAltResult>> {
super::parse_decimal(s)
}
}
#[rhai_fn(return_raw)] #[rhai_fn(return_raw)]
pub fn sqrt(x: Decimal) -> Result<Decimal, Box<EvalAltResult>> { pub fn sqrt(x: Decimal) -> Result<Decimal, Box<EvalAltResult>> {
x.sqrt() x.sqrt()