From 1c1247ac9aba266de5caa1c410dc35881a42bc1f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 18 Dec 2021 12:28:56 +0800 Subject: [PATCH] Tidy up code. --- src/types/dynamic.rs | 259 +++++++++++++++++++++---------------------- 1 file changed, 124 insertions(+), 135 deletions(-) diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index 0c056d81..4fee4279 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -1031,6 +1031,24 @@ impl Dynamic { pub fn from_decimal(value: rust_decimal::Decimal) -> Self { Self(Union::Decimal(value.into(), DEFAULT_TAG_VALUE, ReadWrite)) } + /// Create a [`Dynamic`] from an [`Array`][crate::Array]. + #[cfg(not(feature = "no_index"))] + #[inline(always)] + pub fn from_array(array: crate::Array) -> Self { + Self(Union::Array(array.into(), DEFAULT_TAG_VALUE, ReadWrite)) + } + /// Create a [`Dynamic`] from a [`Blob`][crate::Blob]. + #[cfg(not(feature = "no_index"))] + #[inline(always)] + pub fn from_blob(blob: crate::Blob) -> Self { + Self(Union::Blob(blob.into(), DEFAULT_TAG_VALUE, ReadWrite)) + } + /// Create a [`Dynamic`] from a [`Map`][crate::Map]. + #[cfg(not(feature = "no_object"))] + #[inline(always)] + pub fn from_map(map: crate::Map) -> Self { + Self(Union::Map(map.into(), DEFAULT_TAG_VALUE, ReadWrite)) + } /// Create a new [`Dynamic`] from an [`Instant`]. /// /// Not available under `no-std`. @@ -1177,11 +1195,14 @@ impl Dynamic { /// # Notes /// /// Beware that you need to pass in an [`Array`][crate::Array] type for it to be recognized as an [`Array`][crate::Array]. - /// A [`Vec`][Vec] does not get automatically converted to an [`Array`][crate::Array], but will be a generic - /// restricted trait object instead, because [`Vec`][Vec] is not a supported standard type. + /// A [`Vec`][Vec] does not get automatically converted to an [`Array`][crate::Array], but + /// will be a custom type instead (stored as a trait object). Use `Into` to convert a + /// [`Vec`][Vec] into a [`Dynamic`] as an [`Array`][crate::Array] value. /// /// Similarly, passing in a [`HashMap`][std::collections::HashMap] or - /// [`BTreeMap`][std::collections::BTreeMap] will not get a [`Map`][crate::Map] but a trait object. + /// [`BTreeMap`][std::collections::BTreeMap] will not get a [`Map`][crate::Map] + /// but a custom type. Again, use `Into` to get a [`Dynamic`] with a + /// [`Map`][crate::Map] value. /// /// # Examples /// @@ -1256,7 +1277,7 @@ impl Dynamic { #[cfg(not(feature = "no_index"))] { value = match unsafe_try_cast::<_, crate::Blob>(value) { - Ok(blob) => return Self(Union::Blob(Box::new(blob), DEFAULT_TAG_VALUE, ReadWrite)), + Ok(blob) => return blob.into(), Err(value) => value, }; } @@ -1297,8 +1318,7 @@ impl Dynamic { )) } /// Turn the [`Dynamic`] value into a shared [`Dynamic`] value backed by an - /// [`Rc`][std::rc::Rc]`<`[`RefCell`][std::cell::RefCell]`<`[`Dynamic`]`>>` or - /// [`Arc`][std::sync::Arc]`<`[`RwLock`][std::sync::RwLock]`<`[`Dynamic`]`>>` + /// [`Rc>`][std::rc::Rc] or [`Arc>`][std::sync::Arc] /// depending on the `sync` feature. /// /// Not available under `no_closure`. @@ -2030,117 +2050,9 @@ impl Dynamic { _ => Err(self.type_name()), } } -} - -impl From<()> for Dynamic { - #[inline(always)] - fn from(value: ()) -> Self { - Self(Union::Unit(value, DEFAULT_TAG_VALUE, ReadWrite)) - } -} -impl From for Dynamic { - #[inline(always)] - fn from(value: bool) -> Self { - Self(Union::Bool(value, DEFAULT_TAG_VALUE, ReadWrite)) - } -} -impl From for Dynamic { - #[inline(always)] - fn from(value: INT) -> Self { - Self(Union::Int(value, DEFAULT_TAG_VALUE, ReadWrite)) - } -} -#[cfg(not(feature = "no_float"))] -impl From for Dynamic { - #[inline(always)] - fn from(value: crate::FLOAT) -> Self { - Self(Union::Float(value.into(), DEFAULT_TAG_VALUE, ReadWrite)) - } -} -#[cfg(not(feature = "no_float"))] -impl From> for Dynamic { - #[inline(always)] - fn from(value: crate::ast::FloatWrapper) -> Self { - Self(Union::Float(value, DEFAULT_TAG_VALUE, ReadWrite)) - } -} -#[cfg(feature = "decimal")] -impl From for Dynamic { - #[inline(always)] - fn from(value: rust_decimal::Decimal) -> Self { - Self(Union::Decimal(value.into(), DEFAULT_TAG_VALUE, ReadWrite)) - } -} -impl From for Dynamic { - #[inline(always)] - fn from(value: char) -> Self { - Self(Union::Char(value, DEFAULT_TAG_VALUE, ReadWrite)) - } -} -impl> From for Dynamic { - #[inline(always)] - fn from(value: S) -> Self { - Self(Union::Str(value.into(), DEFAULT_TAG_VALUE, ReadWrite)) - } -} -impl From<&ImmutableString> for Dynamic { - #[inline(always)] - fn from(value: &ImmutableString) -> Self { - value.clone().into() - } -} -impl FromStr for Dynamic { - type Err = (); - - fn from_str(value: &str) -> Result { - Ok(Self(Union::Str(value.into(), DEFAULT_TAG_VALUE, ReadWrite))) - } -} -#[cfg(not(feature = "no_index"))] -impl Dynamic { - /// Create a [`Dynamic`] from an [`Array`][crate::Array]. - #[inline(always)] - pub(crate) fn from_array(array: crate::Array) -> Self { - Self(Union::Array(array.into(), DEFAULT_TAG_VALUE, ReadWrite)) - } -} -#[cfg(not(feature = "no_index"))] -impl From> for Dynamic { - #[inline] - fn from(value: Vec) -> Self { - Self(Union::Array( - Box::new(value.into_iter().map(Dynamic::from).collect()), - DEFAULT_TAG_VALUE, - ReadWrite, - )) - } -} -#[cfg(not(feature = "no_index"))] -impl From<&[T]> for Dynamic { - #[inline] - fn from(value: &[T]) -> Self { - Self(Union::Array( - Box::new(value.iter().cloned().map(Dynamic::from).collect()), - DEFAULT_TAG_VALUE, - ReadWrite, - )) - } -} -#[cfg(not(feature = "no_index"))] -impl std::iter::FromIterator for Dynamic { - #[inline] - fn from_iter>(iter: X) -> Self { - Self(Union::Array( - Box::new(iter.into_iter().map(Dynamic::from).collect()), - DEFAULT_TAG_VALUE, - ReadWrite, - )) - } -} -#[cfg(not(feature = "no_index"))] -impl Dynamic { /// Convert the [`Dynamic`] into an [`Array`][crate::Array]. /// Returns the name of the actual type if the cast fails. + #[cfg(not(feature = "no_index"))] #[inline(always)] pub fn into_array(self) -> Result { match self.0 { @@ -2215,16 +2127,9 @@ impl Dynamic { _ => Err(self.type_name()), } } -} -#[cfg(not(feature = "no_index"))] -impl Dynamic { - /// Create a [`Dynamic`] from a [`Vec`]. - #[inline(always)] - pub fn from_blob(blob: crate::Blob) -> Self { - Self(Union::Blob(Box::new(blob), DEFAULT_TAG_VALUE, ReadWrite)) - } - /// Convert the [`Dynamic`] into a [`Vec`]. + /// Convert the [`Dynamic`] into a [`Blob`][crate::Blob]. /// Returns the name of the actual type if the cast fails. + #[cfg(not(feature = "no_index"))] #[inline(always)] pub fn into_blob(self) -> Result { match self.0 { @@ -2245,12 +2150,102 @@ impl Dynamic { } } } -#[cfg(not(feature = "no_object"))] -impl Dynamic { - /// Create a [`Dynamic`] from a [`Map`][crate::Map]. + +impl From<()> for Dynamic { #[inline(always)] - pub(crate) fn from_map(map: crate::Map) -> Self { - Self(Union::Map(map.into(), DEFAULT_TAG_VALUE, ReadWrite)) + fn from(value: ()) -> Self { + Self(Union::Unit(value, DEFAULT_TAG_VALUE, ReadWrite)) + } +} +impl From for Dynamic { + #[inline(always)] + fn from(value: bool) -> Self { + Self(Union::Bool(value, DEFAULT_TAG_VALUE, ReadWrite)) + } +} +impl From for Dynamic { + #[inline(always)] + fn from(value: INT) -> Self { + Self(Union::Int(value, DEFAULT_TAG_VALUE, ReadWrite)) + } +} +#[cfg(not(feature = "no_float"))] +impl From for Dynamic { + #[inline(always)] + fn from(value: crate::FLOAT) -> Self { + Self(Union::Float(value.into(), DEFAULT_TAG_VALUE, ReadWrite)) + } +} +#[cfg(not(feature = "no_float"))] +impl From> for Dynamic { + #[inline(always)] + fn from(value: crate::ast::FloatWrapper) -> Self { + Self(Union::Float(value, DEFAULT_TAG_VALUE, ReadWrite)) + } +} +#[cfg(feature = "decimal")] +impl From for Dynamic { + #[inline(always)] + fn from(value: rust_decimal::Decimal) -> Self { + Self(Union::Decimal(value.into(), DEFAULT_TAG_VALUE, ReadWrite)) + } +} +impl From for Dynamic { + #[inline(always)] + fn from(value: char) -> Self { + Self(Union::Char(value, DEFAULT_TAG_VALUE, ReadWrite)) + } +} +impl> From for Dynamic { + #[inline(always)] + fn from(value: S) -> Self { + Self(Union::Str(value.into(), DEFAULT_TAG_VALUE, ReadWrite)) + } +} +impl From<&ImmutableString> for Dynamic { + #[inline(always)] + fn from(value: &ImmutableString) -> Self { + value.clone().into() + } +} +impl FromStr for Dynamic { + type Err = (); + + fn from_str(value: &str) -> Result { + Ok(Self(Union::Str(value.into(), DEFAULT_TAG_VALUE, ReadWrite))) + } +} +#[cfg(not(feature = "no_index"))] +impl From> for Dynamic { + #[inline] + fn from(value: Vec) -> Self { + Self(Union::Array( + Box::new(value.into_iter().map(Dynamic::from).collect()), + DEFAULT_TAG_VALUE, + ReadWrite, + )) + } +} +#[cfg(not(feature = "no_index"))] +impl From<&[T]> for Dynamic { + #[inline] + fn from(value: &[T]) -> Self { + Self(Union::Array( + Box::new(value.iter().cloned().map(Dynamic::from).collect()), + DEFAULT_TAG_VALUE, + ReadWrite, + )) + } +} +#[cfg(not(feature = "no_index"))] +impl std::iter::FromIterator for Dynamic { + #[inline] + fn from_iter>(iter: X) -> Self { + Self(Union::Array( + Box::new(iter.into_iter().map(Dynamic::from).collect()), + DEFAULT_TAG_VALUE, + ReadWrite, + )) } } #[cfg(not(feature = "no_object"))] @@ -2329,12 +2324,6 @@ impl From for Dynamic { Self(Union::FnPtr(value.into(), DEFAULT_TAG_VALUE, ReadWrite)) } } -impl From> for Dynamic { - #[inline(always)] - fn from(value: Box) -> Self { - Self(Union::FnPtr(value, DEFAULT_TAG_VALUE, ReadWrite)) - } -} #[cfg(not(feature = "no_std"))] impl From for Dynamic { #[inline(always)]