Make downcast_ref, downcast_mut and is<T> public.

This commit is contained in:
Stephen Chung 2020-04-05 09:56:52 +08:00
parent 29150faef2
commit ae9a975576

View File

@ -92,15 +92,13 @@ impl<T: crate::stdlib::any::Any + Clone + ?Sized> Any for T {
impl Variant { impl Variant {
/// Is this `Variant` a specific type? /// Is this `Variant` a specific type?
pub(crate) fn is<T: Any>(&self) -> bool { pub fn is<T: Any>(&self) -> bool {
let t = TypeId::of::<T>(); TypeId::of::<T>() == <Variant as Any>::type_id(self)
let boxed = <Variant as Any>::type_id(self);
t == boxed
} }
/// Get a reference of a specific type to the `Variant`. /// Get a reference of a specific type to the `Variant`.
pub(crate) fn downcast_ref<T: Any>(&self) -> Option<&T> { /// Returns `None` if the cast fails.
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
if self.is::<T>() { if self.is::<T>() {
unsafe { Some(&*(self as *const Variant as *const T)) } unsafe { Some(&*(self as *const Variant as *const T)) }
} else { } else {
@ -109,7 +107,8 @@ impl Variant {
} }
/// Get a mutable reference of a specific type to the `Variant`. /// Get a mutable reference of a specific type to the `Variant`.
pub(crate) fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> { /// Returns `None` if the cast fails.
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
if self.is::<T>() { if self.is::<T>() {
unsafe { Some(&mut *(self as *mut Variant as *mut T)) } unsafe { Some(&mut *(self as *mut Variant as *mut T)) }
} else { } else {