From 6c63acc55a51f1df5e9154df616f326c872fc076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Thu, 1 Feb 2018 17:18:53 +0100 Subject: [PATCH] Add test showing how downcasting to CFArray with generic is prohibited --- core-foundation/src/base.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core-foundation/src/base.rs b/core-foundation/src/base.rs index 768751008..6f4b64ad9 100644 --- a/core-foundation/src/base.rs +++ b/core-foundation/src/base.rs @@ -64,6 +64,21 @@ impl CFType { /// assert!(cf_type.downcast::().is_none()); /// ``` /// + /// ```compile_fail + /// # use core_foundation::array::CFArray; + /// # use core_foundation::base::TCFType; + /// # use core_foundation::boolean::CFBoolean; + /// # use core_foundation::string::CFString; + /// # + /// let boolean_array = CFArray::from_CFTypes(&[CFBoolean::true_value()]).into_CFType(); + /// + /// // This downcast is not allowed and causes compiler error, since it would cause undefined + /// // behavior to access the elements of the array as a CFString: + /// let invalid_string_array = boolean_array + /// .downcast_into::>() + /// .unwrap(); + /// ``` + /// /// [`Box::downcast`]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast /// [`CFPropertyList::downcast`]: ../propertylist/struct.CFPropertyList.html#method.downcast #[inline] @@ -82,7 +97,7 @@ impl CFType { /// /// [`downcast`]: #method.downcast #[inline] - pub fn downcast_into(self) -> Option { + pub fn downcast_into(self) -> Option { if self.instance_of::() { unsafe { let reference = T::Ref::from_void_ptr(self.0);