Code refactor, bug fixes, code docs.

This commit is contained in:
Stephen Chung
2020-03-04 22:00:01 +08:00
parent b4d56accd4
commit bb56a7a843
14 changed files with 949 additions and 676 deletions

View File

@@ -1,14 +1,21 @@
use std::any::{type_name, Any as StdAny, TypeId};
use std::any::{type_name, TypeId};
use std::fmt;
/// An raw value of any type.
pub type Variant = dyn Any;
/// A boxed dynamic type containing any value.
pub type Dynamic = Box<Variant>;
pub trait Any: StdAny {
/// A trait covering any type.
pub trait Any: std::any::Any {
/// Get the `TypeId` of this type.
fn type_id(&self) -> TypeId;
/// Get the name of this type.
fn type_name(&self) -> &'static str;
/// Convert into `Dynamic`.
fn into_dynamic(&self) -> Dynamic;
/// This type may only be implemented by `rhai`.
@@ -16,11 +23,7 @@ pub trait Any: StdAny {
fn _closed(&self) -> _Private;
}
impl<T> Any for T
where
T: Clone + StdAny + ?Sized,
{
#[inline]
impl<T: Clone + std::any::Any + ?Sized> Any for T {
fn type_id(&self) -> TypeId {
TypeId::of::<T>()
}
@@ -29,7 +32,6 @@ where
type_name::<T>()
}
#[inline]
fn into_dynamic(&self) -> Dynamic {
Box::new(self.clone())
}
@@ -40,20 +42,16 @@ where
}
impl Variant {
//#[inline]
// fn into_dynamic(&self) -> Box<Variant> {
// Any::into_dynamic(self)
// }
#[inline]
pub fn is<T: Any>(&self) -> bool {
/// Is this `Variant` a specific type?
pub(crate) fn is<T: Any>(&self) -> bool {
let t = TypeId::of::<T>();
let boxed = <Variant as Any>::type_id(self);
t == boxed
}
#[inline]
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
/// Get a reference of a specific type to the `Variant`.
pub(crate) fn downcast_ref<T: Any>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe { Some(&*(self as *const Variant as *const T)) }
} else {
@@ -61,8 +59,8 @@ impl Variant {
}
}
#[inline]
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
/// Get a mutable reference of a specific type to the `Variant`.
pub(crate) fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe { Some(&mut *(self as *mut Variant as *mut T)) }
} else {
@@ -71,23 +69,40 @@ impl Variant {
}
}
impl Clone for Dynamic {
fn clone(&self) -> Self {
Any::into_dynamic(self.as_ref())
}
}
impl fmt::Debug for Variant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad("?")
}
}
impl Clone for Dynamic {
fn clone(&self) -> Self {
Any::into_dynamic(self.as_ref())
}
}
/// An extension trait that allows down-casting a `Dynamic` value to a specific type.
pub trait AnyExt: Sized {
/// Get a copy of a `Dynamic` value as a specific type.
fn downcast<T: Any + Clone>(self) -> Result<Box<T>, Self>;
/// This type may only be implemented by `rhai`.
#[doc(hidden)]
fn _closed(&self) -> _Private;
}
impl AnyExt for Dynamic {
/// Get a copy of the `Dynamic` value as a specific type.
///
/// # Example
///
/// ```rust
/// use rhai::{Dynamic, Any, AnyExt};
///
/// let x: Dynamic = 42_u32.into_dynamic();
///
/// assert_eq!(*x.downcast::<u32>().unwrap(), 42);
/// ```
fn downcast<T: Any + Clone>(self) -> Result<Box<T>, Self> {
if self.is::<T>() {
unsafe {
@@ -98,9 +113,13 @@ impl AnyExt for Dynamic {
Err(self)
}
}
fn _closed(&self) -> _Private {
_Private
}
}
/// Private type which ensures that `rhai::Any` can only
/// Private type which ensures that `rhai::Any` and `rhai::AnyExt` can only
/// be implemented by this crate.
#[doc(hidden)]
pub struct _Private;