diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index 62b88ac83..d00825c34 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -385,14 +385,17 @@ impl PartialEq for AnyValueRc { impl AnyValueRc { /// Returns a reference to the inner value without checking the type. - pub fn downcast_ref_unchecked(&self) -> &T { + /// + /// # Safety + /// The caller must ensure that the type of the inner value is `T`. + pub unsafe fn downcast_ref_unchecked(&self) -> &T { unsafe { &*(self.0.as_ref() as *const dyn AnyValue as *const T) } } /// Returns a reference to the inner value. pub fn downcast_ref(&self) -> Option<&T> { if self.0.our_typeid() == TypeId::of::() { - Some(self.downcast_ref_unchecked()) + Some(unsafe { self.downcast_ref_unchecked() }) } else { None } @@ -411,7 +414,9 @@ fn test_any_value_rc() { assert!(!a.is::()); assert_eq!(a.downcast_ref::(), Some(&1i32)); assert_eq!(a.downcast_ref::(), None); - assert_eq!(a.downcast_ref_unchecked::(), &1i32); + unsafe { + assert_eq!(a.downcast_ref_unchecked::(), &1i32); + } } #[cfg(feature = "serialize")]