@@ -15,56 +15,17 @@ let cls = class!(NSObject);
15
15
#[ macro_export]
16
16
macro_rules! class {
17
17
( $name: ident) => ( {
18
- #[ allow( deprecated) ]
19
- #[ inline( always) ]
20
- fn get_class( name: & str ) -> Option <& ' static $crate:: runtime:: Class > {
21
- unsafe {
22
- static CLASS : :: std:: sync:: atomic:: AtomicUsize = :: std:: sync:: atomic:: AtomicUsize :: new( 0 ) ;
23
- // `Relaxed` should be fine since `objc_getClass` is thread-safe.
24
- let ptr = CLASS . load( :: std:: sync:: atomic:: Ordering :: Relaxed ) as * const $crate:: runtime:: Class ;
25
- if ptr. is_null( ) {
26
- let cls = $crate:: runtime:: objc_getClass( name. as_ptr( ) as * const _) ;
27
- CLASS . store( cls as usize , :: std:: sync:: atomic:: Ordering :: Relaxed ) ;
28
- if cls. is_null( ) { None } else { Some ( & * cls) }
29
- } else {
30
- Some ( & * ptr)
31
- }
32
- }
33
- }
34
- match get_class( concat!( stringify!( $name) , '\0' ) ) {
18
+ static CLASS : $crate:: __CachedClass = $crate:: __CachedClass:: new( ) ;
19
+ let name = concat!( stringify!( $name) , '\0' ) ;
20
+ #[ allow( unused_unsafe) ]
21
+ let cls = unsafe { CLASS . get( name) } ;
22
+ match cls {
35
23
Some ( cls) => cls,
36
24
None => panic!( "Class with name {} could not be found" , stringify!( $name) ) ,
37
25
}
38
26
} )
39
27
}
40
28
41
- #[ doc( hidden) ]
42
- #[ macro_export]
43
- macro_rules! sel_impl {
44
- // Declare a function to hide unsafety, otherwise we can trigger the
45
- // unused_unsafe lint; see rust-lang/rust#8472
46
- ( $name: expr) => ( {
47
- #[ allow( deprecated) ]
48
- #[ inline( always) ]
49
- fn register_sel( name: & str ) -> $crate:: runtime:: Sel {
50
- unsafe {
51
- static SEL : :: std:: sync:: atomic:: AtomicUsize = :: std:: sync:: atomic:: AtomicUsize :: new( 0 ) ;
52
- let ptr = SEL . load( :: std:: sync:: atomic:: Ordering :: Relaxed ) as * const :: std:: os:: raw:: c_void;
53
- // It should be fine to use `Relaxed` ordering here because `sel_registerName` is
54
- // thread-safe.
55
- if ptr. is_null( ) {
56
- let sel = $crate:: runtime:: sel_registerName( name. as_ptr( ) as * const _) ;
57
- SEL . store( sel. as_ptr( ) as usize , :: std:: sync:: atomic:: Ordering :: Relaxed ) ;
58
- sel
59
- } else {
60
- $crate:: runtime:: Sel :: from_ptr( ptr)
61
- }
62
- }
63
- }
64
- register_sel( $name)
65
- } )
66
- }
67
-
68
29
/**
69
30
Registers a selector, returning a `Sel`.
70
31
@@ -79,8 +40,18 @@ let sel = sel!(setObject:forKey:);
79
40
*/
80
41
#[ macro_export]
81
42
macro_rules! sel {
82
- ( $name: ident) => ( { $crate:: sel_impl!( concat!( stringify!( $name) , '\0' ) ) } ) ;
83
- ( $( $name: ident : ) +) => ( { $crate:: sel_impl!( concat!( $( stringify!( $name) , ':' ) ,+, '\0' ) ) } ) ;
43
+ ( $name: ident) => ( {
44
+ static SEL : $crate:: __CachedSel = $crate:: __CachedSel:: new( ) ;
45
+ let name = concat!( stringify!( $name) , '\0' ) ;
46
+ #[ allow( unused_unsafe) ]
47
+ unsafe { SEL . get( name) }
48
+ } ) ;
49
+ ( $( $name: ident : ) +) => ( {
50
+ static SEL : $crate:: __CachedSel = $crate:: __CachedSel:: new( ) ;
51
+ let name = concat!( $( stringify!( $name) , ':' ) ,+, '\0' ) ;
52
+ #[ allow( unused_unsafe) ]
53
+ unsafe { SEL . get( name) }
54
+ } ) ;
84
55
}
85
56
86
57
/**
0 commit comments