From ec118b30e186a22b9b8cf88afcf1e80061996219 Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Thu, 1 Feb 2018 13:04:28 -0500 Subject: [PATCH] Add a ConcreteCFType trait We'll use this to distinguish between generic and concrete CFTypes. This lets us prevent downcast()ing to arbitrary generic types. --- core-foundation/src/array.rs | 3 +++ core-foundation/src/base.rs | 3 ++- core-foundation/src/lib.rs | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/core-foundation/src/array.rs b/core-foundation/src/array.rs index 962b2dd..345258b 100644 --- a/core-foundation/src/array.rs +++ b/core-foundation/src/array.rs @@ -19,6 +19,7 @@ use std::marker::PhantomData; use std; use std::ops::Deref; use std::fmt::{Debug, Formatter}; +use ConcreteCFType; use base::{CFIndexConvertible, TCFType, TCFTypeRef, CFRange}; @@ -102,6 +103,8 @@ impl<'a, T: FromVoid> ExactSizeIterator for CFArrayIterator<'a, T> { impl_TCFTypeGeneric!(CFArray, CFArrayRef, CFArrayGetTypeID); impl_CFTypeDescriptionGeneric!(CFArray); +unsafe impl ConcreteCFType for CFArray<*const c_void> {} + impl CFArray { /// Creates a new `CFArray` with the given elements, which must be `CFType` objects. pub fn from_CFTypes(elems: &[T]) -> CFArray where T: TCFType { diff --git a/core-foundation/src/base.rs b/core-foundation/src/base.rs index 981e61a..facdc18 100644 --- a/core-foundation/src/base.rs +++ b/core-foundation/src/base.rs @@ -13,6 +13,7 @@ use std::mem; pub use core_foundation_sys::base::*; use string::CFString; +use ConcreteCFType; pub trait CFIndexConvertible { /// Always use this method to construct a `CFIndex` value. It performs bounds checking to @@ -61,7 +62,7 @@ impl CFType { /// [`Box::downcast`]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast /// [`CFPropertyList::downcast`]: ../propertylist/struct.CFPropertyList.html#method.downcast #[inline] - pub fn downcast(&self) -> Option { + pub fn downcast(&self) -> Option { if self.instance_of::() { unsafe { let reference = T::Ref::from_void_ptr(self.0); diff --git a/core-foundation/src/lib.rs b/core-foundation/src/lib.rs index 20890c8..fa1d8a3 100644 --- a/core-foundation/src/lib.rs +++ b/core-foundation/src/lib.rs @@ -14,6 +14,10 @@ extern crate libc; #[cfg(feature = "with-chrono")] extern crate chrono; +use base::TCFType; + +pub unsafe trait ConcreteCFType: TCFType {} + #[macro_export] macro_rules! declare_TCFType { ( @@ -86,6 +90,8 @@ macro_rules! impl_TCFType { } impl Eq for $ty { } + + unsafe impl $crate::ConcreteCFType for $ty { } } }