Add new constant from functions for Dynamic.

This commit is contained in:
Stephen Chung 2021-07-25 22:48:40 +08:00
parent ffb1531e0e
commit 3127f9a8af
2 changed files with 98 additions and 72 deletions

View File

@ -10,6 +10,7 @@ Enhancements
* `$symbol$` is supported in custom syntax to match any symbol. * `$symbol$` is supported in custom syntax to match any symbol.
* `Dynamic::as_string` and `Dynamic::as_immutable_string` are deprecated and replaced by `into_string` and `into_immutable_string` respectively. * `Dynamic::as_string` and `Dynamic::as_immutable_string` are deprecated and replaced by `into_string` and `into_immutable_string` respectively.
* Added a number of constants to `Dynamic`. * Added a number of constants to `Dynamic`.
* Added a number of constants and `fromXXX` constant methods to `Dynamic`.
* `parse_float()`, `PI()` and `E()` now defer to `Decimal` under `no_float` if `decimal` is turned on. * `parse_float()`, `PI()` and `E()` now defer to `Decimal` under `no_float` if `decimal` is turned on.

View File

@ -10,6 +10,7 @@ use std::{
fmt, fmt,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
str::FromStr,
}; };
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
@ -819,126 +820,149 @@ impl Default for Dynamic {
impl Dynamic { impl Dynamic {
/// A [`Dynamic`] containing a `()`. /// A [`Dynamic`] containing a `()`.
pub const UNIT: Dynamic = Self(Union::Unit((), DEFAULT_TAG_VALUE, ReadWrite)); pub const UNIT: Self = Self(Union::Unit((), DEFAULT_TAG_VALUE, ReadWrite));
/// A [`Dynamic`] containing a `true`. /// A [`Dynamic`] containing a `true`.
pub const TRUE: Dynamic = Self(Union::Bool(true, DEFAULT_TAG_VALUE, ReadWrite)); pub const TRUE: Self = Self::from_bool(true);
/// A [`Dynamic`] containing a [`false`]. /// A [`Dynamic`] containing a [`false`].
pub const FALSE: Dynamic = Self(Union::Bool(false, DEFAULT_TAG_VALUE, ReadWrite)); pub const FALSE: Self = Self::from_bool(false);
/// A [`Dynamic`] containing the integer zero. /// A [`Dynamic`] containing the integer zero.
pub const ZERO: Dynamic = Self(Union::Int(0, DEFAULT_TAG_VALUE, ReadWrite)); pub const ZERO: Self = Self::from_int(0);
/// A [`Dynamic`] containing the integer one. /// A [`Dynamic`] containing the integer one.
pub const ONE: Dynamic = Self(Union::Int(1, DEFAULT_TAG_VALUE, ReadWrite)); pub const ONE: Self = Self::from_int(1);
/// A [`Dynamic`] containing the integer two. /// A [`Dynamic`] containing the integer two.
pub const TWO: Dynamic = Self(Union::Int(2, DEFAULT_TAG_VALUE, ReadWrite)); pub const TWO: Self = Self::from_int(2);
/// A [`Dynamic`] containing the integer three. /// A [`Dynamic`] containing the integer three.
pub const THREE: Dynamic = Self(Union::Int(3, DEFAULT_TAG_VALUE, ReadWrite)); pub const THREE: Self = Self::from_int(3);
/// A [`Dynamic`] containing the integer ten. /// A [`Dynamic`] containing the integer ten.
pub const TEN: Dynamic = Self(Union::Int(10, DEFAULT_TAG_VALUE, ReadWrite)); pub const TEN: Self = Self::from_int(10);
/// A [`Dynamic`] containing the integer one hundred. /// A [`Dynamic`] containing the integer one hundred.
pub const HUNDRED: Dynamic = Self(Union::Int(100, DEFAULT_TAG_VALUE, ReadWrite)); pub const HUNDRED: Self = Self::from_int(100);
/// A [`Dynamic`] containing the integer one thousand. /// A [`Dynamic`] containing the integer one thousand.
pub const THOUSAND: Dynamic = Self(Union::Int(1000, DEFAULT_TAG_VALUE, ReadWrite)); pub const THOUSAND: Self = Self::from_int(1000);
/// A [`Dynamic`] containing the integer negative one. /// A [`Dynamic`] containing the integer negative one.
pub const NEGATIVE_ONE: Dynamic = Self(Union::Int(-1, DEFAULT_TAG_VALUE, ReadWrite)); pub const NEGATIVE_ONE: Self = Self::from_int(-1);
/// A [`Dynamic`] containing `0.0`. /// A [`Dynamic`] containing `0.0`.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_ZERO: Dynamic = Self(Union::Float( pub const FLOAT_ZERO: Self = Self::from_float(0.0);
FloatWrapper::new_const(0.0),
DEFAULT_TAG_VALUE,
ReadWrite,
));
/// A [`Dynamic`] containing `1.0`. /// A [`Dynamic`] containing `1.0`.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_ONE: Dynamic = Self(Union::Float( pub const FLOAT_ONE: Self = Self::from_float(1.0);
FloatWrapper::new_const(1.0),
DEFAULT_TAG_VALUE,
ReadWrite,
));
/// A [`Dynamic`] containing `2.0`. /// A [`Dynamic`] containing `2.0`.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_TWO: Dynamic = Self(Union::Float( pub const FLOAT_TWO: Self = Self::from_float(2.0);
FloatWrapper::new_const(2.0),
DEFAULT_TAG_VALUE,
ReadWrite,
));
/// A [`Dynamic`] containing `10.0`. /// A [`Dynamic`] containing `10.0`.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_TEN: Dynamic = Self(Union::Float( pub const FLOAT_TEN: Self = Self::from_float(10.0);
FloatWrapper::new_const(10.0),
DEFAULT_TAG_VALUE,
ReadWrite,
));
/// A [`Dynamic`] containing `100.0`. /// A [`Dynamic`] containing `100.0`.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_HUNDRED: Dynamic = Self(Union::Float( pub const FLOAT_HUNDRED: Self = Self::from_float(100.0);
FloatWrapper::new_const(100.0),
DEFAULT_TAG_VALUE,
ReadWrite,
));
/// A [`Dynamic`] containing `1000.0`. /// A [`Dynamic`] containing `1000.0`.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_THOUSAND: Dynamic = Self(Union::Float( pub const FLOAT_THOUSAND: Self = Self::from_float(1000.0);
FloatWrapper::new_const(1000.0),
DEFAULT_TAG_VALUE,
ReadWrite,
));
/// A [`Dynamic`] containing `-1.0`. /// A [`Dynamic`] containing `-1.0`.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_NEGATIVE_ONE: Dynamic = Self(Union::Float( pub const FLOAT_NEGATIVE_ONE: Self = Self::from_float(-1.0);
FloatWrapper::new_const(-1.0),
DEFAULT_TAG_VALUE,
ReadWrite,
));
/// A [`Dynamic`] containing π. /// A [`Dynamic`] containing π.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_PI: Dynamic = Self(Union::Float(
#[cfg(not(feature = "f32_float"))] #[cfg(not(feature = "f32_float"))]
FloatWrapper::new_const(std::f64::consts::PI), pub const FLOAT_PI: Self = Self::from_float(
#[cfg(not(feature = "f32_float"))]
{
std::f64::consts::PI
},
#[cfg(feature = "f32_float")] #[cfg(feature = "f32_float")]
FloatWrapper::new_const(std::f32::consts::PI), {
DEFAULT_TAG_VALUE, std::f32::consts::PI
ReadWrite, },
)); );
/// A [`Dynamic`] containing π/2. /// A [`Dynamic`] containing π/2.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_HALF_PI: Dynamic = Self(Union::Float( pub const FLOAT_HALF_PI: Self = Self::from_float(
#[cfg(not(feature = "f32_float"))] #[cfg(not(feature = "f32_float"))]
FloatWrapper::new_const(std::f64::consts::PI / 2.0), {
std::f64::consts::PI / 2.0
},
#[cfg(feature = "f32_float")] #[cfg(feature = "f32_float")]
FloatWrapper::new_const(std::f32::consts::PI / 2.0), {
DEFAULT_TAG_VALUE, std::f32::consts::PI / 2.0
ReadWrite, },
)); );
/// A [`Dynamic`] containing 2π. /// A [`Dynamic`] containing 2π.
/// ///
/// Not available under `no_float`. /// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
pub const FLOAT_TWO_PI: Dynamic = Self(Union::Float( pub const FLOAT_TWO_PI: Self = Self::from_float(
#[cfg(not(feature = "f32_float"))] #[cfg(not(feature = "f32_float"))]
FloatWrapper::new_const(2.0 * std::f64::consts::PI), {
std::f64::consts::PI * 2.0
},
#[cfg(feature = "f32_float")] #[cfg(feature = "f32_float")]
FloatWrapper::new_const(2.0 * std::f32::consts::PI), {
std::f32::consts::PI * 2.0
},
);
/// Create a new [`Dynamic`] from a [`bool`].
#[inline(always)]
pub const fn from_bool(value: bool) -> Self {
Self(Union::Bool(value, DEFAULT_TAG_VALUE, ReadWrite))
}
/// Create a new [`Dynamic`] from an [`INT`].
#[inline(always)]
pub const fn from_int(value: INT) -> Self {
Self(Union::Int(value, DEFAULT_TAG_VALUE, ReadWrite))
}
/// Create a new [`Dynamic`] from a [`char`].
#[inline(always)]
pub const fn from_char(value: char) -> Self {
Self(Union::Char(value, DEFAULT_TAG_VALUE, ReadWrite))
}
/// Create a new [`Dynamic`] from a [`FLOAT`].
///
/// Not available under `no_float`.
#[cfg(not(feature = "no_float"))]
#[inline(always)]
pub const fn from_float(value: FLOAT) -> Self {
Self(Union::Float(
FloatWrapper::new_const(value),
DEFAULT_TAG_VALUE, DEFAULT_TAG_VALUE,
ReadWrite, ReadWrite,
)); ))
}
/// Create a new [`Dynamic`] from a [`Decimal`](https://docs.rs/rust_decimal).
///
/// Exported under the `decimal` feature only.
#[cfg(feature = "decimal")]
#[inline(always)]
pub fn from_decimal(value: Decimal) -> Self {
Self(Union::Decimal(value.into(), DEFAULT_TAG_VALUE, ReadWrite))
}
/// Create a new [`Dynamic`] from an [`Instant`].
///
/// Not available under `no-std`.
#[cfg(not(feature = "no_std"))]
#[inline(always)]
pub fn from_timestamp(value: Instant) -> Self {
Self(Union::TimeStamp(value.into(), DEFAULT_TAG_VALUE, ReadWrite))
}
/// Get the [`AccessMode`] for this [`Dynamic`]. /// Get the [`AccessMode`] for this [`Dynamic`].
#[must_use] #[must_use]
@ -1882,10 +1906,7 @@ impl Dynamic {
/// This method is deprecated. Use [`into_string`][Dynamic::into_string] instead. /// This method is deprecated. Use [`into_string`][Dynamic::into_string] instead.
/// ///
/// This method will be removed in the next major version. /// This method will be removed in the next major version.
#[deprecated( #[deprecated(since = "1.0.1", note = "use `into_string` instead")]
since = "1.0.1",
note = "this method is deprecated; use `into_string` instead"
)]
#[inline(always)] #[inline(always)]
pub fn as_string(self) -> Result<String, &'static str> { pub fn as_string(self) -> Result<String, &'static str> {
self.into_string() self.into_string()
@ -1906,10 +1927,7 @@ impl Dynamic {
/// This method is deprecated. Use [`into_immutable_string`][Dynamic::into_immutable_string] instead. /// This method is deprecated. Use [`into_immutable_string`][Dynamic::into_immutable_string] instead.
/// ///
/// This method will be removed in the next major version. /// This method will be removed in the next major version.
#[deprecated( #[deprecated(since = "1.0.1", note = "use `into_immutable_string` instead")]
since = "1.0.1",
note = "this method is deprecated; use `into_string` instead"
)]
#[inline(always)] #[inline(always)]
pub fn as_immutable_string(self) -> Result<ImmutableString, &'static str> { pub fn as_immutable_string(self) -> Result<ImmutableString, &'static str> {
self.into_immutable_string() self.into_immutable_string()
@ -1994,6 +2012,13 @@ impl From<&ImmutableString> for Dynamic {
value.clone().into() value.clone().into()
} }
} }
impl FromStr for Dynamic {
type Err = ();
fn from_str(value: &str) -> Result<Self, Self::Err> {
Ok(Self(Union::Str(value.into(), DEFAULT_TAG_VALUE, ReadWrite)))
}
}
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
impl Dynamic { impl Dynamic {
/// Create a [`Dynamic`] from an [`Array`]. /// Create a [`Dynamic`] from an [`Array`].