44use std:: {
55 collections:: HashMap ,
66 marker:: { PhantomData , PhantomPinned } ,
7- mem:: { size_of , MaybeUninit } ,
7+ mem:: { MaybeUninit , size_of } ,
88 os:: raw:: c_void,
99 ptr:: null,
1010} ;
1111
1212use core_foundation:: {
1313 array:: { CFArrayGetCount , CFArrayGetValueAtIndex , CFArrayRef } ,
14- base:: { kCFAllocatorDefault , kCFAllocatorNull , CFAllocatorRef , CFRange , CFRelease , CFTypeRef } ,
14+ base:: { CFAllocatorRef , CFRange , CFRelease , CFTypeRef , kCFAllocatorDefault , kCFAllocatorNull } ,
1515 data:: { CFDataGetBytes , CFDataGetLength , CFDataRef } ,
1616 dictionary:: {
17- kCFTypeDictionaryKeyCallBacks , kCFTypeDictionaryValueCallBacks , CFDictionaryCreate ,
18- CFDictionaryCreateMutableCopy , CFDictionaryGetCount , CFDictionaryGetKeysAndValues ,
19- CFDictionaryGetValue , CFDictionaryRef , CFMutableDictionaryRef ,
17+ CFDictionaryCreate , CFDictionaryCreateMutableCopy , CFDictionaryGetCount ,
18+ CFDictionaryGetKeysAndValues , CFDictionaryGetValue , CFDictionaryRef , CFMutableDictionaryRef ,
19+ kCFTypeDictionaryKeyCallBacks , kCFTypeDictionaryValueCallBacks ,
2020 } ,
21- number:: { kCFNumberSInt32Type , CFNumberCreate , CFNumberRef } ,
22- string:: { kCFStringEncodingUTF8 , CFStringCreateWithBytesNoCopy , CFStringGetCString , CFStringRef } ,
21+ number:: { CFNumberCreate , CFNumberRef , kCFNumberSInt32Type } ,
22+ string:: { CFStringCreateWithBytesNoCopy , CFStringGetCString , CFStringRef , kCFStringEncodingUTF8 } ,
2323} ;
2424
2525pub type WithError < T > = Result < T , Box < dyn std:: error:: Error > > ;
@@ -88,7 +88,7 @@ pub fn cfdict_get_val(dict: CFDictionaryRef, key: &str) -> Option<CFTypeRef> {
8888
8989#[ link( name = "IOKit" , kind = "framework" ) ]
9090#[ rustfmt:: skip]
91- extern "C" {
91+ unsafe extern "C" {
9292 fn IOServiceMatching ( name : * const i8 ) -> CFMutableDictionaryRef ;
9393 fn IOServiceGetMatchingServices ( mainPort : u32 , matching : CFDictionaryRef , existing : * mut u32 ) -> i32 ;
9494 fn IOIteratorNext ( iterator : u32 ) -> u32 ;
@@ -107,7 +107,7 @@ type IOReportSubscriptionRef = *const IOReportSubscription;
107107
108108#[ link( name = "IOReport" , kind = "dylib" ) ]
109109#[ rustfmt:: skip]
110- extern "C" {
110+ unsafe extern "C" {
111111 fn IOReportCopyAllChannels ( a : u64 , b : u64 ) -> CFDictionaryRef ;
112112 fn IOReportCopyChannelsInGroup ( a : CFStringRef , b : CFStringRef , c : u64 , d : u64 , e : u64 ) -> CFDictionaryRef ;
113113 fn IOReportMergeChannels ( a : CFDictionaryRef , b : CFDictionaryRef , nil : CFTypeRef ) ;
@@ -251,9 +251,7 @@ impl IOReportIterator {
251251
252252impl Drop for IOReportIterator {
253253 fn drop ( & mut self ) {
254- unsafe {
255- CFRelease ( self . sample as _ ) ;
256- }
254+ unsafe { CFRelease ( self . sample as _ ) } ;
257255 }
258256}
259257
@@ -500,38 +498,40 @@ pub fn get_soc_info() -> WithError<SocInfo> {
500498
501499// MARK: IOReport
502500
503- unsafe fn cfio_get_chan ( items : Vec < ( & str , Option < & str > ) > ) -> WithError < CFMutableDictionaryRef > {
501+ fn cfio_get_chan ( items : Vec < ( & str , Option < & str > ) > ) -> WithError < CFMutableDictionaryRef > {
504502 // if no items are provided, return all channels
505503 if items. len ( ) == 0 {
506- let c = IOReportCopyAllChannels ( 0 , 0 ) ;
507- let r = CFDictionaryCreateMutableCopy ( kCFAllocatorDefault, CFDictionaryGetCount ( c) , c) ;
508- CFRelease ( c as _ ) ;
509- return Ok ( r) ;
504+ unsafe {
505+ let c = IOReportCopyAllChannels ( 0 , 0 ) ;
506+ let r = CFDictionaryCreateMutableCopy ( kCFAllocatorDefault, CFDictionaryGetCount ( c) , c) ;
507+ CFRelease ( c as _ ) ;
508+ return Ok ( r) ;
509+ }
510510 }
511511
512512 let mut channels = vec ! [ ] ;
513513 for ( group, subgroup) in items {
514514 let gname = cfstr ( group) ;
515515 let sname = subgroup. map_or ( null ( ) , |x| cfstr ( x) ) ;
516- let chan = IOReportCopyChannelsInGroup ( gname, sname, 0 , 0 , 0 ) ;
516+ let chan = unsafe { IOReportCopyChannelsInGroup ( gname, sname, 0 , 0 , 0 ) } ;
517517 channels. push ( chan) ;
518518
519- CFRelease ( gname as _ ) ;
519+ unsafe { CFRelease ( gname as _ ) } ;
520520 if subgroup. is_some ( ) {
521- CFRelease ( sname as _ ) ;
521+ unsafe { CFRelease ( sname as _ ) } ;
522522 }
523523 }
524524
525525 let chan = channels[ 0 ] ;
526526 for i in 1 ..channels. len ( ) {
527- IOReportMergeChannels ( chan, channels[ i] , null ( ) ) ;
527+ unsafe { IOReportMergeChannels ( chan, channels[ i] , null ( ) ) } ;
528528 }
529529
530- let size = CFDictionaryGetCount ( chan) ;
531- let chan = CFDictionaryCreateMutableCopy ( kCFAllocatorDefault, size, chan) ;
530+ let size = unsafe { CFDictionaryGetCount ( chan) } ;
531+ let chan = unsafe { CFDictionaryCreateMutableCopy ( kCFAllocatorDefault, size, chan) } ;
532532
533533 for i in 0 ..channels. len ( ) {
534- CFRelease ( channels[ i] as _ ) ;
534+ unsafe { CFRelease ( channels[ i] as _ ) } ;
535535 }
536536
537537 if cfdict_get_val ( chan, "IOReportChannels" ) . is_none ( ) {
@@ -541,14 +541,14 @@ unsafe fn cfio_get_chan(items: Vec<(&str, Option<&str>)>) -> WithError<CFMutable
541541 Ok ( chan)
542542}
543543
544- unsafe fn cfio_get_subs ( chan : CFMutableDictionaryRef ) -> WithError < IOReportSubscriptionRef > {
544+ fn cfio_get_subs ( chan : CFMutableDictionaryRef ) -> WithError < IOReportSubscriptionRef > {
545545 let mut s: MaybeUninit < CFMutableDictionaryRef > = MaybeUninit :: uninit ( ) ;
546- let rs = IOReportCreateSubscription ( std :: ptr :: null ( ) , chan, s. as_mut_ptr ( ) , 0 , std :: ptr :: null ( ) ) ;
547- if rs == std :: ptr :: null ( ) {
546+ let rs = unsafe { IOReportCreateSubscription ( null ( ) , chan, s. as_mut_ptr ( ) , 0 , null ( ) ) } ;
547+ if rs. is_null ( ) {
548548 return Err ( "Failed to create subscription" . into ( ) ) ;
549549 }
550550
551- s. assume_init ( ) ;
551+ unsafe { s. assume_init ( ) } ;
552552 Ok ( rs)
553553}
554554
@@ -560,9 +560,8 @@ pub struct IOReport {
560560
561561impl IOReport {
562562 pub fn new ( channels : Vec < ( & str , Option < & str > ) > ) -> WithError < Self > {
563- let chan = unsafe { cfio_get_chan ( channels) ? } ;
564- let subs = unsafe { cfio_get_subs ( chan) ? } ;
565-
563+ let chan = cfio_get_chan ( channels) ?;
564+ let subs = cfio_get_subs ( chan) ?;
566565 Ok ( Self { subs, chan, prev : None } )
567566 }
568567
@@ -647,7 +646,7 @@ const kIOHIDEventTypePower: i64 = 25;
647646
648647#[ link( name = "IOKit" , kind = "framework" ) ]
649648#[ rustfmt:: skip]
650- extern "C" {
649+ unsafe extern "C" {
651650 fn IOHIDEventSystemClientCreate ( allocator : CFAllocatorRef ) -> IOHIDEventSystemClientRef ;
652651 fn IOHIDEventSystemClientSetMatching ( a : IOHIDEventSystemClientRef , b : CFDictionaryRef ) -> i32 ;
653652 fn IOHIDEventSystemClientCopyServices ( a : IOHIDEventSystemClientRef ) -> CFArrayRef ;
@@ -728,16 +727,14 @@ impl IOHIDSensors {
728727
729728impl Drop for IOHIDSensors {
730729 fn drop ( & mut self ) {
731- unsafe {
732- CFRelease ( self . sensors as _ ) ;
733- }
730+ unsafe { CFRelease ( self . sensors as _ ) } ;
734731 }
735732}
736733
737734// MARK: SMC Bindings
738735
739736#[ link( name = "IOKit" , kind = "framework" ) ]
740- extern "C" {
737+ unsafe extern "C" {
741738 fn mach_task_self ( ) -> u32 ;
742739 fn IOServiceOpen ( device : u32 , a : u32 , b : u32 , c : * mut u32 ) -> i32 ;
743740 fn IOServiceClose ( conn : u32 ) -> i32 ;
0 commit comments