Skip to content

Commit

Permalink
Auto merge of #159 - jrmuizel:generic, r=jdm
Browse files Browse the repository at this point in the history
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.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/159)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Feb 2, 2018
2 parents 97c95bf + ec118b3 commit 2e17240
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core-foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<T> CFArray<T> {
/// Creates a new `CFArray` with the given elements, which must be `CFType` objects.
pub fn from_CFTypes(elems: &[T]) -> CFArray<T> where T: TCFType {
Expand Down
3 changes: 2 additions & 1 deletion core-foundation/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<T: TCFType>(&self) -> Option<T> {
pub fn downcast<T: ConcreteCFType>(&self) -> Option<T> {
if self.instance_of::<T>() {
unsafe {
let reference = T::Ref::from_void_ptr(self.0);
Expand Down
6 changes: 6 additions & 0 deletions core-foundation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
(
Expand Down Expand Up @@ -86,6 +90,8 @@ macro_rules! impl_TCFType {
}

impl Eq for $ty { }

unsafe impl $crate::ConcreteCFType for $ty { }
}
}

Expand Down

0 comments on commit 2e17240

Please sign in to comment.