Dynamic::read renamed to Dynamic::clone_inner_data

This commit is contained in:
Ilya Lakhin 2020-07-31 13:10:05 +07:00
parent 89f75bbf0c
commit 5d1f5cc2b4
3 changed files with 46 additions and 46 deletions

View File

@ -709,44 +709,13 @@ impl Dynamic {
self.try_cast::<T>().unwrap()
}
/// 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.
#[inline(always)]
pub fn read_lock<T: Variant + Clone>(&self) -> Option<DynamicReadLock<T>> {
match self.0 {
Union::Shared(ref cell) => {
let type_id = TypeId::of::<T>();
if type_id != TypeId::of::<Dynamic>() && cell.value_type_id != type_id {
return None
}
#[cfg(not(feature = "sync"))]
return Some(DynamicReadLock(DynamicReadLockInner::Guard(
cell.container.borrow()
)));
#[cfg(feature = "sync")]
return Some(DynamicReadLock(DynamicReadLockInner::Guard(
cell.container.read().unwrap()
)));
},
_ => {
self.downcast_ref().map(|reference| {
DynamicReadLock(DynamicReadLockInner::Reference(reference))
})
}
}
}
/// 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<T: Variant + Clone>(&self) -> Option<T> {
pub fn clone_inner_data<T: Variant + Clone>(&self) -> Option<T> {
match self.0 {
Union::Shared(ref cell) => {
let type_id = TypeId::of::<T>();
@ -780,6 +749,37 @@ impl Dynamic {
}
}
/// 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.
#[inline(always)]
pub fn read_lock<T: Variant + Clone>(&self) -> Option<DynamicReadLock<T>> {
match self.0 {
Union::Shared(ref cell) => {
let type_id = TypeId::of::<T>();
if type_id != TypeId::of::<Dynamic>() && cell.value_type_id != type_id {
return None
}
#[cfg(not(feature = "sync"))]
return Some(DynamicReadLock(DynamicReadLockInner::Guard(
cell.container.borrow()
)));
#[cfg(feature = "sync")]
return Some(DynamicReadLock(DynamicReadLockInner::Guard(
cell.container.read().unwrap()
)));
},
_ => {
self.downcast_ref().map(|reference| {
DynamicReadLock(DynamicReadLockInner::Reference(reference))
})
}
}
}
/// 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<INT, &'static str> {
match self.0 {
Union::Int(n) => Ok(n),
Union::Shared(_) => self.read::<INT>().ok_or_else(|| self.type_name()),
Union::Shared(_) => self.clone_inner_data::<INT>().ok_or_else(|| self.type_name()),
_ => Err(self.type_name()),
}
}
@ -975,7 +975,7 @@ impl Dynamic {
pub fn as_float(&self) -> Result<FLOAT, &'static str> {
match self.0 {
Union::Float(n) => Ok(n),
Union::Shared(_) => self.read::<FLOAT>().ok_or_else(|| self.type_name()),
Union::Shared(_) => self.clone_inner_data::<FLOAT>().ok_or_else(|| self.type_name()),
_ => Err(self.type_name()),
}
}
@ -985,7 +985,7 @@ impl Dynamic {
pub fn as_bool(&self) -> Result<bool, &'static str> {
match self.0 {
Union::Bool(b) => Ok(b),
Union::Shared(_) => self.read::<bool>().ok_or_else(|| self.type_name()),
Union::Shared(_) => self.clone_inner_data::<bool>().ok_or_else(|| self.type_name()),
_ => Err(self.type_name()),
}
}
@ -995,7 +995,7 @@ impl Dynamic {
pub fn as_char(&self) -> Result<char, &'static str> {
match self.0 {
Union::Char(n) => Ok(n),
Union::Shared(_) => self.read::<char>().ok_or_else(|| self.type_name()),
Union::Shared(_) => self.clone_inner_data::<char>().ok_or_else(|| self.type_name()),
_ => Err(self.type_name()),
}
}

View File

@ -658,7 +658,7 @@ impl Engine {
))
} else if _fn_name == KEYWORD_TAKE {
// take call
return Ok((obj.read::<Dynamic>().unwrap(), false));
return Ok((obj.clone_inner_data::<Dynamic>().unwrap(), false));
} else {
#[cfg(not(feature = "no_object"))]
let redirected;

View File

@ -340,7 +340,7 @@ impl<'a> Scope<'a> {
/// ```
pub fn get_value<T: Variant + Clone>(&self, name: &str) -> Option<T> {
self.get_entry(name)
.and_then(|Entry { value, .. }| value.read::<T>())
.and_then(|Entry { value, .. }| value.clone_inner_data::<T>())
}
/// Update the value of the named entry.