Add Dynamic from shared.

This commit is contained in:
Stephen Chung 2021-05-11 21:38:07 +08:00
parent be052b2b26
commit 9585de4ae4
2 changed files with 30 additions and 10 deletions

View File

@ -16,6 +16,7 @@ New features
* Each `Dynamic` value can now contain arbitrary data (type `i16`) 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.
* `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`.
Enhancements
------------

View File

@ -1027,35 +1027,43 @@ impl Dynamic {
}
value = match unsafe_try_cast::<_, String>(value) {
Ok(s) => return (s).into(),
Err(val) => val,
Ok(s) => return s.into(),
Err(value) => value,
};
#[cfg(not(feature = "no_index"))]
{
value = match unsafe_try_cast::<_, Array>(value) {
Ok(array) => return (array).into(),
Err(val) => val,
Ok(array) => return array.into(),
Err(value) => value,
};
}
#[cfg(not(feature = "no_object"))]
{
value = match unsafe_try_cast::<_, Map>(value) {
Ok(map) => return (map).into(),
Err(val) => val,
Ok(map) => return map.into(),
Err(value) => value,
};
}
value = match unsafe_try_cast::<_, FnPtr>(value) {
Ok(fn_ptr) => return (fn_ptr).into(),
Err(val) => val,
Ok(fn_ptr) => return fn_ptr.into(),
Err(value) => value,
};
#[cfg(not(feature = "no_std"))]
{
value = match unsafe_try_cast::<_, Instant>(value) {
Ok(timestamp) => return (timestamp).into(),
Err(val) => val,
Ok(timestamp) => return timestamp.into(),
Err(value) => value,
};
}
#[cfg(not(feature = "no_closure"))]
{
value = match unsafe_try_cast::<_, crate::Shared<crate::Locked<Dynamic>>>(value) {
Ok(value) => return value.into(),
Err(value) => value,
};
}
@ -1974,3 +1982,14 @@ impl From<Instant> for Dynamic {
))
}
}
#[cfg(not(feature = "no_closure"))]
impl From<crate::Shared<crate::Locked<Dynamic>>> for Dynamic {
#[inline(always)]
fn from(value: crate::Shared<crate::Locked<Self>>) -> Self {
Self(Union::Shared(
value.into(),
DEFAULT_TAG,
AccessMode::ReadWrite,
))
}
}