From 4d894d0f2898e3a743d10564791851c44d8d1796 Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Tue, 2 Nov 2021 01:38:38 +0000 Subject: [PATCH] Bug 1737896 - Update to core-foundation 0.9.2. r=mstange Differential Revision: https://phabricator.services.mozilla.com/D129570 --- Cargo.lock | 8 +- .../core-foundation-sys/.cargo-checksum.json | 2 +- .../rust/core-foundation-sys/Cargo.toml | 2 +- .../rust/core-foundation-sys/src/bundle.rs | 1 + .../rust/core-foundation-sys/src/data.rs | 6 ++ .../rust/core-foundation-sys/src/lib.rs | 1 + .../rust/core-foundation-sys/src/mach_port.rs | 20 +++++ .../rust/core-foundation-sys/src/string.rs | 6 +- .../rust/core-foundation/.cargo-checksum.json | 2 +- third_party/rust/core-foundation/Cargo.toml | 2 +- third_party/rust/core-foundation/src/array.rs | 2 +- .../rust/core-foundation/src/bundle.rs | 18 +++++ third_party/rust/core-foundation/src/data.rs | 81 +++++++++++++++++++ .../rust/core-foundation/src/dictionary.rs | 2 +- third_party/rust/core-foundation/src/lib.rs | 1 + .../rust/core-foundation/src/mach_port.rs | 28 +++++++ .../rust/core-foundation/src/propertylist.rs | 2 + .../rust/core-foundation/src/runloop.rs | 2 +- 18 files changed, 174 insertions(+), 12 deletions(-) create mode 100644 third_party/rust/core-foundation-sys/src/mach_port.rs create mode 100644 third_party/rust/core-foundation/src/mach_port.rs diff --git a/Cargo.lock b/Cargo.lock index 59399f0d7fdc..919470030d98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -709,9 +709,9 @@ checksum = "a2df960f5d869b2dd8532793fde43eb5427cceb126c929747a26823ab0eeb536" [[package]] name = "core-foundation" -version = "0.9.1" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a89e2ae426ea83155dccf10c0fa6b1463ef6d5fcb44cee0b224a408fa640a62" +checksum = "6888e10551bb93e424d8df1d07f1a8b4fceb0001a3a4b048bfc47554946f47b3" dependencies = [ "core-foundation-sys", "libc", @@ -719,9 +719,9 @@ dependencies = [ [[package]] name = "core-foundation-sys" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" +checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" [[package]] name = "core-graphics" diff --git a/third_party/rust/core-foundation-sys/.cargo-checksum.json b/third_party/rust/core-foundation-sys/.cargo-checksum.json index 3b04722467c7..0bdf99086488 100644 --- a/third_party/rust/core-foundation-sys/.cargo-checksum.json +++ b/third_party/rust/core-foundation-sys/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"380a7141d91af6d200eece8c518d2b60e98818025302982d733e459ccf2cde19","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"62065228e42caebca7e7d7db1204cbb867033de5982ca4009928915e4095f3a3","build.rs":"9433ed3b20cc99e716dda4c6d8507c29bc04882544cbbea8d4e48ba80fd0fa12","src/array.rs":"40c305658e16b07d86353a5ad34e7b5fb98720f19cc2b5173360d6a61ce2642f","src/attributed_string.rs":"48c2d161d6f54136ae895a9f52efad1f831718ea331b875d25498039158a2d51","src/base.rs":"7b21281e9a01903c79a7891ee4e1865a11504b281b468815d1a3838e1744a3c2","src/bundle.rs":"be1ce281cb415b3a46a0f961e5a30ded5d295a09c0a4c7f27efa6721e2516c69","src/characterset.rs":"a10bbb42ddc74b3dc50b43ae6a50cc9d9a1cd08b975a1ce1b092be4bf64448a6","src/data.rs":"7cf4ddbc62635434fd3552739ffae9dde5f5d34f0ad0bb818068d3ac26403784","src/date.rs":"c064ee4c3ebd1927532c34871e2e41179d6e3c3e400f6b409a18ad9e2337477f","src/dictionary.rs":"3327a6f90f1e0db5e3fde1973e2df4143ca896716a816d03f2b17c8e988c5159","src/error.rs":"6205ebeb7631daa8bcd560862b6daa10f640c8c117ce5f6f7184f268dcbcb42a","src/filedescriptor.rs":"49580654b657811fade7adaa256f5f895cb011c9baa4731e2f44a6ec7fdba235","src/lib.rs":"f815234d32327532da600f47e9a006550d8993dcd695b803d0660316899f9319","src/messageport.rs":"e9227d5907cba8e29cdeea41bcb3ae5c7840220442953ab19aace31a84542f47","src/number.rs":"f28040accfbbec99c4e55f411facac7cde4ad89298af2d7d907312f18e4263bf","src/propertylist.rs":"7ec928438826c4ce40befedf3de0a37c8ecbc0fc17896dfa629d5864000b2cfe","src/runloop.rs":"26ca33e2472d191f583e01c24e8cd262f54de8b542fbe7278f33ab08b2925794","src/set.rs":"116c2f18008bfbeecac570d366dbd95b8fe5b9373e3e1bdd2c1d588314d776c5","src/string.rs":"ef2c408bf1fcea5d9106329fc48d659aabbdbca05eb121dfa27a221829bc4b89","src/timezone.rs":"c7dd9557646b7ff2bfd6a98bf92142b8304125b4e78dd651b687abc8da191159","src/url.rs":"4358f756ed3d5e9afd5a7f3e2e9115adc6133b47dc7ce59d6ebb32c6610b0e8f","src/uuid.rs":"82f75efa73d0842dff2e13d299c166c6593a77fcb69c4b7629a2df1c17ae507d"},"package":"ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"} \ No newline at end of file +{"files":{"Cargo.toml":"ddcd13ba60bd9368949654be284efac4a55e482b7233e42c9d2fef97ff825eb8","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"62065228e42caebca7e7d7db1204cbb867033de5982ca4009928915e4095f3a3","build.rs":"9433ed3b20cc99e716dda4c6d8507c29bc04882544cbbea8d4e48ba80fd0fa12","src/array.rs":"40c305658e16b07d86353a5ad34e7b5fb98720f19cc2b5173360d6a61ce2642f","src/attributed_string.rs":"48c2d161d6f54136ae895a9f52efad1f831718ea331b875d25498039158a2d51","src/base.rs":"7b21281e9a01903c79a7891ee4e1865a11504b281b468815d1a3838e1744a3c2","src/bundle.rs":"2a3413e649e1cf5eb1557fa5e3e6d442389caa2978a4a0c2c7afdfa4c388c0bf","src/characterset.rs":"a10bbb42ddc74b3dc50b43ae6a50cc9d9a1cd08b975a1ce1b092be4bf64448a6","src/data.rs":"2b121f102b4c2778475f4902016820c4a70e2f8ac532af53913561d42757fa2e","src/date.rs":"c064ee4c3ebd1927532c34871e2e41179d6e3c3e400f6b409a18ad9e2337477f","src/dictionary.rs":"3327a6f90f1e0db5e3fde1973e2df4143ca896716a816d03f2b17c8e988c5159","src/error.rs":"6205ebeb7631daa8bcd560862b6daa10f640c8c117ce5f6f7184f268dcbcb42a","src/filedescriptor.rs":"49580654b657811fade7adaa256f5f895cb011c9baa4731e2f44a6ec7fdba235","src/lib.rs":"e0b6b9d16f6cae1328de772e056e90003b0a5aa5859f5cde58f6aac4683bb543","src/mach_port.rs":"bd413d44772eaeb24e74e1b7b4f234b159b916caa0896c8e82fe54ce1b9deeb6","src/messageport.rs":"e9227d5907cba8e29cdeea41bcb3ae5c7840220442953ab19aace31a84542f47","src/number.rs":"f28040accfbbec99c4e55f411facac7cde4ad89298af2d7d907312f18e4263bf","src/propertylist.rs":"7ec928438826c4ce40befedf3de0a37c8ecbc0fc17896dfa629d5864000b2cfe","src/runloop.rs":"26ca33e2472d191f583e01c24e8cd262f54de8b542fbe7278f33ab08b2925794","src/set.rs":"116c2f18008bfbeecac570d366dbd95b8fe5b9373e3e1bdd2c1d588314d776c5","src/string.rs":"b2856d0bf7a8583370d18d3f041ac5eb00fd6a1aabc69503e7c11a93d42036d8","src/timezone.rs":"c7dd9557646b7ff2bfd6a98bf92142b8304125b4e78dd651b687abc8da191159","src/url.rs":"4358f756ed3d5e9afd5a7f3e2e9115adc6133b47dc7ce59d6ebb32c6610b0e8f","src/uuid.rs":"82f75efa73d0842dff2e13d299c166c6593a77fcb69c4b7629a2df1c17ae507d"},"package":"5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"} \ No newline at end of file diff --git a/third_party/rust/core-foundation-sys/Cargo.toml b/third_party/rust/core-foundation-sys/Cargo.toml index 51956da42e37..bd17103b5652 100644 --- a/third_party/rust/core-foundation-sys/Cargo.toml +++ b/third_party/rust/core-foundation-sys/Cargo.toml @@ -12,7 +12,7 @@ [package] name = "core-foundation-sys" -version = "0.8.2" +version = "0.8.3" authors = ["The Servo Project Developers"] build = "build.rs" description = "Bindings to Core Foundation for macOS" diff --git a/third_party/rust/core-foundation-sys/src/bundle.rs b/third_party/rust/core-foundation-sys/src/bundle.rs index 45c64295d33c..29f8e1261f52 100644 --- a/third_party/rust/core-foundation-sys/src/bundle.rs +++ b/third_party/rust/core-foundation-sys/src/bundle.rs @@ -35,4 +35,5 @@ extern { pub fn CFBundleCopyPrivateFrameworksURL(bundle: CFBundleRef) -> CFURLRef; pub fn CFBundleCopySharedSupportURL(bundle: CFBundleRef) -> CFURLRef; pub fn CFBundleCopyBundleURL(bundle: CFBundleRef) -> CFURLRef; + pub fn CFBundleCopyResourcesDirectoryURL(bundle: CFBundleRef) -> CFURLRef; } diff --git a/third_party/rust/core-foundation-sys/src/data.rs b/third_party/rust/core-foundation-sys/src/data.rs index 7a62c9b82eef..e5ed0dc9f027 100644 --- a/third_party/rust/core-foundation-sys/src/data.rs +++ b/third_party/rust/core-foundation-sys/src/data.rs @@ -27,6 +27,12 @@ extern { pub fn CFDataGetBytePtr(theData: CFDataRef) -> *const u8; pub fn CFDataGetBytes(theData: CFDataRef, range: CFRange, buffer: *mut u8); pub fn CFDataGetLength(theData: CFDataRef) -> CFIndex; + pub fn CFDataCreateWithBytesNoCopy( + allocator: CFAllocatorRef, + bytes: *const u8, + length: CFIndex, + allocator: CFAllocatorRef, + ) -> CFDataRef; pub fn CFDataGetTypeID() -> CFTypeID; } diff --git a/third_party/rust/core-foundation-sys/src/lib.rs b/third_party/rust/core-foundation-sys/src/lib.rs index 9b1056b1ab5b..f9a1884169c4 100644 --- a/third_party/rust/core-foundation-sys/src/lib.rs +++ b/third_party/rust/core-foundation-sys/src/lib.rs @@ -29,3 +29,4 @@ pub mod string; pub mod timezone; pub mod url; pub mod uuid; +pub mod mach_port; diff --git a/third_party/rust/core-foundation-sys/src/mach_port.rs b/third_party/rust/core-foundation-sys/src/mach_port.rs new file mode 100644 index 000000000000..2341fd4e74d1 --- /dev/null +++ b/third_party/rust/core-foundation-sys/src/mach_port.rs @@ -0,0 +1,20 @@ +pub use base::{CFAllocatorRef, CFIndex, CFTypeID}; +use runloop::CFRunLoopSourceRef; +use std::os::raw::c_void; + +#[repr(C)] +pub struct __CFMachPort(c_void); +pub type CFMachPortRef = *const __CFMachPort; + +extern "C" { + /* + * CFMachPort.h + */ + pub fn CFMachPortCreateRunLoopSource( + allocator: CFAllocatorRef, + port: CFMachPortRef, + order: CFIndex, + ) -> CFRunLoopSourceRef; + + pub fn CFMachPortGetTypeID() -> CFTypeID; +} diff --git a/third_party/rust/core-foundation-sys/src/string.rs b/third_party/rust/core-foundation-sys/src/string.rs index 8b92024a913e..d4f655078462 100644 --- a/third_party/rust/core-foundation-sys/src/string.rs +++ b/third_party/rust/core-foundation-sys/src/string.rs @@ -219,7 +219,11 @@ extern { contentsDeallocator: CFAllocatorRef) -> CFStringRef; //fn CFStringCreateWithCharacters - //fn CFStringCreateWithCharactersNoCopy + pub fn CFStringCreateWithCharactersNoCopy(alloc: CFAllocatorRef, + chars: *const UniChar, + numChars: CFIndex, + contentsDeallocator: CFAllocatorRef) + -> CFStringRef; pub fn CFStringCreateWithCString(alloc: CFAllocatorRef, cStr: *const c_char, encoding: CFStringEncoding) diff --git a/third_party/rust/core-foundation/.cargo-checksum.json b/third_party/rust/core-foundation/.cargo-checksum.json index e0d209de285f..a2ffe0868393 100644 --- a/third_party/rust/core-foundation/.cargo-checksum.json +++ b/third_party/rust/core-foundation/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"0ddd9a3c5e8b40befdd1dd5efdfc95cacff3d244ed472e413c19fc61c2e17697","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"62065228e42caebca7e7d7db1204cbb867033de5982ca4009928915e4095f3a3","src/array.rs":"221e81de15d95ca453df39466ba65faf879f2923202145958dfcebe63bfef97f","src/attributed_string.rs":"c04c5a3c912b6ab58d3081fa5dfa8fda07957b26396ebc907bba5ad07dc1087d","src/base.rs":"9ed46d59afdb22ecc65504ccd77f1c98108ea0ff96fad568dadb7ed49fde588c","src/boolean.rs":"f5b4d295aaf6cc3681212afebeae2974c5d551125badcc066e6089ac0d2a0873","src/bundle.rs":"7c26060c6ca64347b96800498e663d622f8dc1aa551a2f18cffa258cd27272a0","src/characterset.rs":"a3ffb46c5463c99813eebfc177a65794f44a1b9343534e28bbccd262034714c2","src/data.rs":"a1d7e6430c745731fc7b2aee49af9f14e79c8a288951c82ea6507689a3356af8","src/date.rs":"a02f6cca7b6e8fbabc7713177429012658dba5146556e14a08e7baaf81958d83","src/dictionary.rs":"ed8080eb00507e342521fb2c513a83c5706e883c3f8d67d8f38e9a0ed685c7eb","src/error.rs":"22c21087d9c2c103d1bcb2c4af33b684317bbda5d4747902e510d410f8c1a14b","src/filedescriptor.rs":"6c2087a8e101bfb31a6b916759a69a82852b18b2174c818f6ec9211ccdcaf1e6","src/lib.rs":"867b9b5acbc8dcf1d9f61a936cec5c26762a0af444fa83cafdf9f3ab1ee979eb","src/number.rs":"5a6cc9036c156e7e9d787864dd671087677abf4175cd840cbf123ccd89abcafe","src/propertylist.rs":"9f182135d9eb21421673280112d5676051bf7f1c2548c6da1ff879ea0bfb4704","src/runloop.rs":"048772c566cf3a1ec3e478958293aa9f3af38ff40b71ab7695f86b61d12f2023","src/set.rs":"da75b31f894bd04c0518211e113ca21a2781cd6ac71018104eeb354dd2f1a19e","src/string.rs":"bea7f3fd2528377442d1bebfeacf7e905d879320ae662fe6a8ef917b6a8e9c36","src/timezone.rs":"6dcf188db76303292b519d6283b71b71c7b3d6ed1afddc28de16d727075f5ef1","src/url.rs":"c544725399d3fbc4de54fd9d1c987e02f81760effd08c5c64c8deacd11478950","src/uuid.rs":"c3457e43fc22ba0f3fcab3ee8108500585ba005dbcbfa7edad0d389701ddf379","tests/use_macro_outside_crate.rs":"ed5e74ac9e988f2609c0b785189f1e6368d693f6401603ad065521f3ab51df94"},"package":"0a89e2ae426ea83155dccf10c0fa6b1463ef6d5fcb44cee0b224a408fa640a62"} \ No newline at end of file +{"files":{"Cargo.toml":"e2d42485d210d0c29001720940bd0255cb413d40992ae7f99c51a40eb4c9ab32","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"62065228e42caebca7e7d7db1204cbb867033de5982ca4009928915e4095f3a3","src/array.rs":"e5e6ea11e660e5e6676568c9b5b4595633c16fad770755b13bb4fde6c7436a4e","src/attributed_string.rs":"c04c5a3c912b6ab58d3081fa5dfa8fda07957b26396ebc907bba5ad07dc1087d","src/base.rs":"9ed46d59afdb22ecc65504ccd77f1c98108ea0ff96fad568dadb7ed49fde588c","src/boolean.rs":"f5b4d295aaf6cc3681212afebeae2974c5d551125badcc066e6089ac0d2a0873","src/bundle.rs":"c290a87d8bc43d8c9e1935c18e6d7127f46fd211cc2b041ed9e7b618b9df270c","src/characterset.rs":"a3ffb46c5463c99813eebfc177a65794f44a1b9343534e28bbccd262034714c2","src/data.rs":"c8d2ec30d8ac6249ef84afa0f7638d950b5c70d687aeb4ba9e3e46806aa872aa","src/date.rs":"a02f6cca7b6e8fbabc7713177429012658dba5146556e14a08e7baaf81958d83","src/dictionary.rs":"780599de45d64e8f4bca18ad50fc4a909f6e9aab1c3f2cea451f469d766f6782","src/error.rs":"22c21087d9c2c103d1bcb2c4af33b684317bbda5d4747902e510d410f8c1a14b","src/filedescriptor.rs":"6c2087a8e101bfb31a6b916759a69a82852b18b2174c818f6ec9211ccdcaf1e6","src/lib.rs":"2d0b007b264642cbad31e8c901f5f2235ed55515ef73e573a8d86306a7cd4f06","src/mach_port.rs":"87aa8dd28060bbb976197e7cc981eb733762b9bcc29eddd742f44aa8141e71b1","src/number.rs":"5a6cc9036c156e7e9d787864dd671087677abf4175cd840cbf123ccd89abcafe","src/propertylist.rs":"3c964d976709562b96f391265130c0f8fced307267a2b6385bab469048518b55","src/runloop.rs":"809292d501cce954803b48e5a4c951c4f356d19b3e93845bc2edeab2fe944829","src/set.rs":"da75b31f894bd04c0518211e113ca21a2781cd6ac71018104eeb354dd2f1a19e","src/string.rs":"bea7f3fd2528377442d1bebfeacf7e905d879320ae662fe6a8ef917b6a8e9c36","src/timezone.rs":"6dcf188db76303292b519d6283b71b71c7b3d6ed1afddc28de16d727075f5ef1","src/url.rs":"c544725399d3fbc4de54fd9d1c987e02f81760effd08c5c64c8deacd11478950","src/uuid.rs":"c3457e43fc22ba0f3fcab3ee8108500585ba005dbcbfa7edad0d389701ddf379","tests/use_macro_outside_crate.rs":"ed5e74ac9e988f2609c0b785189f1e6368d693f6401603ad065521f3ab51df94"},"package":"6888e10551bb93e424d8df1d07f1a8b4fceb0001a3a4b048bfc47554946f47b3"} \ No newline at end of file diff --git a/third_party/rust/core-foundation/Cargo.toml b/third_party/rust/core-foundation/Cargo.toml index 66aabee04682..0a9505edbbec 100644 --- a/third_party/rust/core-foundation/Cargo.toml +++ b/third_party/rust/core-foundation/Cargo.toml @@ -12,7 +12,7 @@ [package] name = "core-foundation" -version = "0.9.1" +version = "0.9.2" authors = ["The Servo Project Developers"] description = "Bindings to Core Foundation for macOS" homepage = "https://github.com/servo/core-foundation-rs" diff --git a/third_party/rust/core-foundation/src/array.rs b/third_party/rust/core-foundation/src/array.rs index f13ea198c83d..d66ffc5b8213 100644 --- a/third_party/rust/core-foundation/src/array.rs +++ b/third_party/rust/core-foundation/src/array.rs @@ -57,7 +57,7 @@ impl<'a, T: FromVoid> ExactSizeIterator for CFArrayIterator<'a, T> { } impl_TCFType!(CFArray, CFArrayRef, CFArrayGetTypeID); -impl_CFTypeDescription!(CFArray); +impl_CFTypeDescription!(CFArray); unsafe impl ConcreteCFType for CFArray<*const c_void> {} diff --git a/third_party/rust/core-foundation/src/bundle.rs b/third_party/rust/core-foundation/src/bundle.rs index e79e3c30572f..b9ab1f65f678 100644 --- a/third_party/rust/core-foundation/src/bundle.rs +++ b/third_party/rust/core-foundation/src/bundle.rs @@ -99,6 +99,24 @@ impl CFBundle { Some(PathBuf::from(url.get_file_system_path(kCFURLPOSIXPathStyle).to_string())) } + /// Bundle's resources location + pub fn bundle_resources_url(&self) -> Option { + unsafe { + let bundle_url = CFBundleCopyResourcesDirectoryURL(self.0); + if bundle_url.is_null() { + None + } else { + Some(TCFType::wrap_under_create_rule(bundle_url)) + } + } + } + + /// Bundle's resources location + pub fn resources_path(&self) -> Option { + let url = self.bundle_resources_url()?; + Some(PathBuf::from(url.get_file_system_path(kCFURLPOSIXPathStyle).to_string())) + } + pub fn private_frameworks_url(&self) -> Option { unsafe { let fw_url = CFBundleCopyPrivateFrameworksURL(self.0); diff --git a/third_party/rust/core-foundation/src/data.rs b/third_party/rust/core-foundation/src/data.rs index 2b5010a52d23..c510c7434d71 100644 --- a/third_party/rust/core-foundation/src/data.rs +++ b/third_party/rust/core-foundation/src/data.rs @@ -14,6 +14,8 @@ use core_foundation_sys::base::CFIndex; use core_foundation_sys::base::{kCFAllocatorDefault}; use std::ops::Deref; use std::slice; +use std::sync::Arc; + use base::{CFIndexConvertible, TCFType}; @@ -26,6 +28,7 @@ impl_TCFType!(CFData, CFDataRef, CFDataGetTypeID); impl_CFTypeDescription!(CFData); impl CFData { + /// Creates a CFData around a copy `buffer` pub fn from_buffer(buffer: &[u8]) -> CFData { unsafe { let data_ref = CFDataCreate(kCFAllocatorDefault, @@ -35,6 +38,41 @@ impl CFData { } } + /// Creates a CFData referencing `buffer` without creating a copy + pub fn from_arc + Sync + Send>(buffer: Arc) -> Self { + use std::os::raw::c_void; + use crate::base::{CFAllocator, CFAllocatorContext}; + + unsafe { + let ptr = (*buffer).as_ref().as_ptr() as *const _; + let len = (*buffer).as_ref().len().to_CFIndex(); + let info = Arc::into_raw(buffer) as *mut c_void; + + extern "C" fn deallocate(_: *mut c_void, info: *mut c_void) { + unsafe { + drop(Arc::from_raw(info as *mut T)); + } + } + + // Use a separate allocator for each allocation because + // we need `info` to do the deallocation vs. `ptr` + let allocator = CFAllocator::new(CFAllocatorContext { + info, + version: 0, + retain: None, + reallocate: None, + release: None, + copyDescription: None, + allocate: None, + deallocate: Some(deallocate::), + preferredSize: None, + }); + let data_ref = + CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, ptr, len, allocator.as_CFTypeRef()); + TCFType::wrap_under_create_rule(data_ref) + } + } + /// Returns a pointer to the underlying bytes in this data. Note that this byte buffer is /// read-only. #[inline] @@ -61,3 +99,46 @@ impl Deref for CFData { self.bytes() } } + +#[cfg(test)] +mod test { + use super::CFData; + use std::sync::Arc; + + #[test] + fn test_data_provider() { + let l = vec![5]; + CFData::from_arc(Arc::new(l)); + + let l = vec![5]; + CFData::from_arc(Arc::new(l.into_boxed_slice())); + + // Make sure the buffer is actually dropped + use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; + struct VecWrapper { + inner: Vec, + dropped: Arc, + } + + impl Drop for VecWrapper { + fn drop(&mut self) { + self.dropped.store(true, SeqCst) + } + } + + impl std::convert::AsRef<[u8]> for VecWrapper { + fn as_ref(&self) -> &[u8] { + &self.inner + } + } + + let dropped = Arc::new(AtomicBool::default()); + let l = Arc::new(VecWrapper {inner: vec![5], dropped: dropped.clone() }); + let m = l.clone(); + let dp = CFData::from_arc(l); + drop(m); + assert!(!dropped.load(SeqCst)); + drop(dp); + assert!(dropped.load(SeqCst)) + } +} diff --git a/third_party/rust/core-foundation/src/dictionary.rs b/third_party/rust/core-foundation/src/dictionary.rs index f954b71b26a2..efcbba1178c3 100644 --- a/third_party/rust/core-foundation/src/dictionary.rs +++ b/third_party/rust/core-foundation/src/dictionary.rs @@ -31,7 +31,7 @@ impl Drop for CFDictionary { } impl_TCFType!(CFDictionary, CFDictionaryRef, CFDictionaryGetTypeID); -impl_CFTypeDescription!(CFDictionary); +impl_CFTypeDescription!(CFDictionary); unsafe impl ConcreteCFType for CFDictionary<*const c_void, *const c_void> {} diff --git a/third_party/rust/core-foundation/src/lib.rs b/third_party/rust/core-foundation/src/lib.rs index eaab9a039e66..b93593899633 100644 --- a/third_party/rust/core-foundation/src/lib.rs +++ b/third_party/rust/core-foundation/src/lib.rs @@ -233,3 +233,4 @@ pub mod propertylist; pub mod runloop; pub mod timezone; pub mod uuid; +pub mod mach_port; diff --git a/third_party/rust/core-foundation/src/mach_port.rs b/third_party/rust/core-foundation/src/mach_port.rs new file mode 100644 index 000000000000..6112e3aae01a --- /dev/null +++ b/third_party/rust/core-foundation/src/mach_port.rs @@ -0,0 +1,28 @@ +use base::TCFType; +use core_foundation_sys::base::kCFAllocatorDefault; +use runloop::CFRunLoopSource; +pub use core_foundation_sys::mach_port::*; + + +declare_TCFType! { + /// An immutable numeric value. + CFMachPort, CFMachPortRef +} +impl_TCFType!(CFMachPort, CFMachPortRef, CFMachPortGetTypeID); +impl_CFTypeDescription!(CFMachPort); + +impl CFMachPort { + pub fn create_runloop_source( + &self, + order: CFIndex, + ) -> Result { + unsafe { + let runloop_source_ref = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, self.0, order); + if runloop_source_ref.is_null() { + Err(()) + } else { + Ok(CFRunLoopSource::wrap_under_create_rule(runloop_source_ref)) + } + } + } +} diff --git a/third_party/rust/core-foundation/src/propertylist.rs b/third_party/rust/core-foundation/src/propertylist.rs index 9f34f5658231..e8fceac58d23 100644 --- a/third_party/rust/core-foundation/src/propertylist.rs +++ b/third_party/rust/core-foundation/src/propertylist.rs @@ -113,6 +113,8 @@ declare_TCFType!{ CFPropertyList, CFPropertyListRef } +impl_CFTypeDescription!(CFPropertyList); + impl CFPropertyList { #[inline] pub fn as_concrete_TypeRef(&self) -> CFPropertyListRef { diff --git a/third_party/rust/core-foundation/src/runloop.rs b/third_party/rust/core-foundation/src/runloop.rs index 5b27028d827d..8f22c090e232 100644 --- a/third_party/rust/core-foundation/src/runloop.rs +++ b/third_party/rust/core-foundation/src/runloop.rs @@ -180,7 +180,7 @@ mod test { CFRunLoop::run_current(); let elapsed = elapsed_rx.try_recv().unwrap(); println!("wait_200_milliseconds, elapsed: {}", elapsed); - assert!(elapsed > 0.19 && elapsed < 0.30); + assert!(elapsed > 0.19 && elapsed < 0.35); } struct Info {