Skip to content

Fix bug in CFArray::to_untyped that made the retain count wrong #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core-foundation-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "core-foundation-sys"
description = "Bindings to Core Foundation for OS X"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.4.5"
version = "0.4.6"
authors = ["The Servo Project Developers"]
license = "MIT / Apache-2.0"
build = "build.rs"
Expand Down
4 changes: 2 additions & 2 deletions core-foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ name = "core-foundation"
description = "Bindings to Core Foundation for OS X"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.4.5"
version = "0.4.6"
authors = ["The Servo Project Developers"]
license = "MIT / Apache-2.0"

[dependencies.core-foundation-sys]
path = "../core-foundation-sys"
version = "0.4.5"
version = "0.4.6"

[dependencies]
libc = "0.2"
Expand Down
125 changes: 79 additions & 46 deletions core-foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ impl<T> CFArray<T> {
}
}

#[deprecated(note = "please use `as_untyped` instead")]
pub fn to_untyped(self) -> CFArray {
CFArray(self.0, PhantomData)
unsafe { CFArray::wrap_under_get_rule(self.0) }
}

pub fn as_untyped(&self) -> CFArray {
unsafe { CFArray::wrap_under_get_rule(self.0) }
}

/// Iterates over the elements of this `CFArray`.
Expand Down Expand Up @@ -150,53 +155,81 @@ impl<'a, T: FromVoid> IntoIterator for &'a CFArray<T> {
}
}

#[test]
fn should_box_and_unbox() {
use number::CFNumber;

let n0 = CFNumber::from(0);
let n1 = CFNumber::from(1);
let n2 = CFNumber::from(2);
let n3 = CFNumber::from(3);
let n4 = CFNumber::from(4);
let n5 = CFNumber::from(5);

let arr = CFArray::from_CFTypes(&[
n0.as_CFType(),
n1.as_CFType(),
n2.as_CFType(),
n3.as_CFType(),
n4.as_CFType(),
n5.as_CFType(),
]);

assert!(arr.get_all_values() == &[n0.as_CFTypeRef(),
n1.as_CFTypeRef(),
n2.as_CFTypeRef(),
n3.as_CFTypeRef(),
n4.as_CFTypeRef(),
n5.as_CFTypeRef()]);

unsafe {
let mut sum = 0;

let mut iter = arr.iter();
assert_eq!(iter.len(), 6);
assert!(iter.next().is_some());
assert_eq!(iter.len(), 5);

for elem in iter {
let number: CFNumber = TCFType::wrap_under_get_rule(mem::transmute(elem));
sum += number.to_i64().unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem;

assert!(sum == 15);
#[test]
fn to_untyped_correct_retain_count() {
let array = CFArray::<CFType>::from_CFTypes(&[]);
assert_eq!(array.retain_count(), 1);

for elem in arr.iter() {
let number: CFNumber = TCFType::wrap_under_get_rule(mem::transmute(elem));
sum += number.to_i64().unwrap()
}
let untyped_array = array.to_untyped();
assert_eq!(untyped_array.retain_count(), 1);
}

#[test]
fn as_untyped_correct_retain_count() {
let array = CFArray::<CFType>::from_CFTypes(&[]);
assert_eq!(array.retain_count(), 1);

let untyped_array = array.as_untyped();
assert_eq!(array.retain_count(), 2);
assert_eq!(untyped_array.retain_count(), 2);

mem::drop(array);
assert_eq!(untyped_array.retain_count(), 1);
}

assert!(sum == 30);
#[test]
fn should_box_and_unbox() {
use number::CFNumber;

let n0 = CFNumber::from(0);
let n1 = CFNumber::from(1);
let n2 = CFNumber::from(2);
let n3 = CFNumber::from(3);
let n4 = CFNumber::from(4);
let n5 = CFNumber::from(5);

let arr = CFArray::from_CFTypes(&[
n0.as_CFType(),
n1.as_CFType(),
n2.as_CFType(),
n3.as_CFType(),
n4.as_CFType(),
n5.as_CFType(),
]);

assert!(arr.get_all_values() == &[n0.as_CFTypeRef(),
n1.as_CFTypeRef(),
n2.as_CFTypeRef(),
n3.as_CFTypeRef(),
n4.as_CFTypeRef(),
n5.as_CFTypeRef()]);

unsafe {
let mut sum = 0;

let mut iter = arr.iter();
assert_eq!(iter.len(), 6);
assert!(iter.next().is_some());
assert_eq!(iter.len(), 5);

for elem in iter {
let number: CFNumber = TCFType::wrap_under_get_rule(mem::transmute(elem));
sum += number.to_i64().unwrap()
}

assert!(sum == 15);

for elem in arr.iter() {
let number: CFNumber = TCFType::wrap_under_get_rule(mem::transmute(elem));
sum += number.to_i64().unwrap()
}

assert!(sum == 30);
}
}
}
18 changes: 14 additions & 4 deletions core-foundation/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,20 @@ impl TCFType<CFTypeRef> for CFType {
// FIXME(pcwalton): Is this right?
0
}
}

#[inline]
fn instance_of<OtherConcreteTypeRef,OtherCFType:TCFType<OtherConcreteTypeRef>>(&self) -> bool {
// Since this is the root of the type hierarchy, we always answer yes.
true

#[cfg(test)]
mod tests {
use super::*;
use boolean::CFBoolean;

#[test]
fn cftype_instance_of() {
let string = CFString::from_static_string("foo");
let cftype = string.as_CFType();

assert!(cftype.instance_of::<_, CFString>());
assert!(!cftype.instance_of::<_, CFBoolean>());
}
}