diff --git a/src/any.rs b/src/any.rs index 1a82d237..cbb3e444 100644 --- a/src/any.rs +++ b/src/any.rs @@ -709,6 +709,46 @@ impl Dynamic { self.try_cast::().unwrap() } + /// Get a copy of a specific type to the `Dynamic`. + /// Casting to `Dynamic` returns a clone of the value in case of NON-shared + /// Dynamic. In case of Shared Dynamic returns a clone of the inner data of + /// Shared Dynamic. + /// Returns `None` if the cast fails. + #[inline(always)] + pub fn clone_inner_data(&self) -> Option { + match self.0 { + Union::Shared(ref cell) => { + let type_id = TypeId::of::(); + + if type_id != TypeId::of::() && cell.value_type_id != type_id { + return None + } + + #[cfg(not(feature = "sync"))] + return Some(cell + .container + .borrow() + .deref() + .downcast_ref::() + .unwrap() + .clone()); + + #[cfg(feature = "sync")] + return Some(cell + .container + .read() + .unwrap() + .deref() + .downcast_ref::() + .unwrap() + .clone()); + }, + _ => { + self.downcast_ref().cloned() + } + } + } + /// Get a reference of a specific type to the `Dynamic`. /// Casting to `Dynamic` just returns a reference to it. /// Returns `None` if the cast fails. @@ -740,46 +780,6 @@ impl Dynamic { } } - /// Get a copy of a specific type to the `Dynamic`. - /// Casting to `Dynamic` returns a clone of the value in case of NON-shared - /// Dynamic. In case of Shared Dynamic returns a clone of the inner data of - /// Shared Dynamic. - /// Returns `None` if the cast fails. - #[inline(always)] - pub fn read(&self) -> Option { - match self.0 { - Union::Shared(ref cell) => { - let type_id = TypeId::of::(); - - if type_id != TypeId::of::() && cell.value_type_id != type_id { - return None - } - - #[cfg(not(feature = "sync"))] - return Some(cell - .container - .borrow() - .deref() - .downcast_ref::() - .unwrap() - .clone()); - - #[cfg(feature = "sync")] - return Some(cell - .container - .read() - .unwrap() - .deref() - .downcast_ref::() - .unwrap() - .clone()); - }, - _ => { - self.downcast_ref().cloned() - } - } - } - /// Get a mutable reference of a specific type to the `Dynamic`. /// Casting to `Dynamic` just returns a mutable reference to it. /// Returns `None` if the cast fails. @@ -964,7 +964,7 @@ impl Dynamic { pub fn as_int(&self) -> Result { match self.0 { Union::Int(n) => Ok(n), - Union::Shared(_) => self.read::().ok_or_else(|| self.type_name()), + Union::Shared(_) => self.clone_inner_data::().ok_or_else(|| self.type_name()), _ => Err(self.type_name()), } } @@ -975,7 +975,7 @@ impl Dynamic { pub fn as_float(&self) -> Result { match self.0 { Union::Float(n) => Ok(n), - Union::Shared(_) => self.read::().ok_or_else(|| self.type_name()), + Union::Shared(_) => self.clone_inner_data::().ok_or_else(|| self.type_name()), _ => Err(self.type_name()), } } @@ -985,7 +985,7 @@ impl Dynamic { pub fn as_bool(&self) -> Result { match self.0 { Union::Bool(b) => Ok(b), - Union::Shared(_) => self.read::().ok_or_else(|| self.type_name()), + Union::Shared(_) => self.clone_inner_data::().ok_or_else(|| self.type_name()), _ => Err(self.type_name()), } } @@ -995,7 +995,7 @@ impl Dynamic { pub fn as_char(&self) -> Result { match self.0 { Union::Char(n) => Ok(n), - Union::Shared(_) => self.read::().ok_or_else(|| self.type_name()), + Union::Shared(_) => self.clone_inner_data::().ok_or_else(|| self.type_name()), _ => Err(self.type_name()), } } diff --git a/src/fn_call.rs b/src/fn_call.rs index e0dd1184..bed49230 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -658,7 +658,7 @@ impl Engine { )) } else if _fn_name == KEYWORD_TAKE { // take call - return Ok((obj.read::().unwrap(), false)); + return Ok((obj.clone_inner_data::().unwrap(), false)); } else { #[cfg(not(feature = "no_object"))] let redirected; diff --git a/src/scope.rs b/src/scope.rs index 2fb3df21..b1b9a3b3 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -340,7 +340,7 @@ impl<'a> Scope<'a> { /// ``` pub fn get_value(&self, name: &str) -> Option { self.get_entry(name) - .and_then(|Entry { value, .. }| value.read::()) + .and_then(|Entry { value, .. }| value.clone_inner_data::()) } /// Update the value of the named entry.