@@ -15,56 +15,17 @@ let cls = class!(NSObject);
1515#[ macro_export]
1616macro_rules! class {
1717 ( $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 {
3523 Some ( cls) => cls,
3624 None => panic!( "Class with name {} could not be found" , stringify!( $name) ) ,
3725 }
3826 } )
3927}
4028
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-
6829/**
6930Registers a selector, returning a `Sel`.
7031
@@ -79,8 +40,18 @@ let sel = sel!(setObject:forKey:);
7940*/
8041#[ macro_export]
8142macro_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+ } ) ;
8455}
8556
8657/**
0 commit comments