From 31fcb64bf16c5a239c9108271043a407507f4a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Tue, 28 Nov 2017 15:54:19 +0100 Subject: [PATCH 1/4] Fix bug in CFArray::to_untyped that made the retain count wrong --- core-foundation/src/array.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core-foundation/src/array.rs b/core-foundation/src/array.rs index 17fb6af..da37a11 100644 --- a/core-foundation/src/array.rs +++ b/core-foundation/src/array.rs @@ -94,8 +94,13 @@ impl CFArray { } } + #[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`. From 4e9cbd73879fa155db8b0cebfb4c1aea873ff875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Tue, 28 Nov 2017 16:32:16 +0100 Subject: [PATCH 2/4] Add test for as_untyped --- core-foundation/src/array.rs | 110 ++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 41 deletions(-) diff --git a/core-foundation/src/array.rs b/core-foundation/src/array.rs index da37a11..e681c68 100644 --- a/core-foundation/src/array.rs +++ b/core-foundation/src/array.rs @@ -155,53 +155,81 @@ impl<'a, T: FromVoid> IntoIterator for &'a CFArray { } } -#[test] -fn should_box_and_unbox() { - use number::CFNumber; +#[cfg(test)] +mod tests { + use super::*; + use std::mem; - 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); + #[test] + fn to_untyped_correct_retain_count() { + let array = CFArray::::from_CFTypes(&[]); + assert_eq!(array.retain_count(), 1); - let arr = CFArray::from_CFTypes(&[ - n0.as_CFType(), - n1.as_CFType(), - n2.as_CFType(), - n3.as_CFType(), - n4.as_CFType(), - n5.as_CFType(), - ]); + let untyped_array = array.to_untyped(); + assert_eq!(untyped_array.retain_count(), 1); + } - assert!(arr.get_all_values() == &[n0.as_CFTypeRef(), - n1.as_CFTypeRef(), - n2.as_CFTypeRef(), - n3.as_CFTypeRef(), - n4.as_CFTypeRef(), - n5.as_CFTypeRef()]); + #[test] + fn as_untyped_correct_retain_count() { + let array = CFArray::::from_CFTypes(&[]); + assert_eq!(array.retain_count(), 1); - unsafe { - let mut sum = 0; + let untyped_array = array.as_untyped(); + assert_eq!(array.retain_count(), 2); + assert_eq!(untyped_array.retain_count(), 2); - let mut iter = arr.iter(); - assert_eq!(iter.len(), 6); - assert!(iter.next().is_some()); - assert_eq!(iter.len(), 5); + mem::drop(array); + assert_eq!(untyped_array.retain_count(), 1); + } - for elem in iter { - let number: CFNumber = TCFType::wrap_under_get_rule(mem::transmute(elem)); - sum += number.to_i64().unwrap() + #[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); } - - 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); } } From f0b8535e4e657c205a541e5e5a7a709b74f5908e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Tue, 28 Nov 2017 16:33:26 +0100 Subject: [PATCH 3/4] Fix bug in CFType::instance_of. It's not an instance of all other types --- core-foundation/src/base.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/core-foundation/src/base.rs b/core-foundation/src/base.rs index 4b56f1f..3d945e9 100644 --- a/core-foundation/src/base.rs +++ b/core-foundation/src/base.rs @@ -178,10 +178,20 @@ impl TCFType for CFType { // FIXME(pcwalton): Is this right? 0 } +} - #[inline] - fn instance_of>(&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>()); } } From c33598dc990b9c54cd3b0e67cf0356363a11ff45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Tue, 28 Nov 2017 16:38:23 +0100 Subject: [PATCH 4/4] Bump version to 0.4.6 --- core-foundation-sys/Cargo.toml | 2 +- core-foundation/Cargo.toml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core-foundation-sys/Cargo.toml b/core-foundation-sys/Cargo.toml index e100819..3d997e5 100644 --- a/core-foundation-sys/Cargo.toml +++ b/core-foundation-sys/Cargo.toml @@ -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" diff --git a/core-foundation/Cargo.toml b/core-foundation/Cargo.toml index 87ca5e0..3d90ca9 100644 --- a/core-foundation/Cargo.toml +++ b/core-foundation/Cargo.toml @@ -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"