1
1
#![ deny( unsafe_op_in_unsafe_fn) ]
2
- use core:: { cell:: RefCell , ptr:: NonNull } ;
2
+ use core:: { cell:: OnceCell , ptr:: NonNull } ;
3
3
4
4
use icrate:: {
5
5
AppKit :: {
@@ -26,20 +26,29 @@ use objc2::{
26
26
sel, ClassType ,
27
27
} ;
28
28
29
- type IdCell < T > = Box < RefCell < Option < Id < T > > > > ;
29
+ type IdCell < T > = Box < OnceCell < Id < T > > > ;
30
30
31
31
macro_rules! idcell {
32
+ ( $name: ident => $this: expr) => {
33
+ $this. $name. set( $name) . expect( & format!(
34
+ "ivar should not already be initialized: `{}`" ,
35
+ stringify!( $name)
36
+ ) ) ;
37
+ } ;
32
38
( $name: ident <= $this: expr) => {
33
- let $name = $this. $name. borrow( ) ;
34
- let $name = $name
35
- . as_ref( )
36
- . expect( concat!( stringify!( $name) , " ivar should be initialized" ) ) ;
39
+ #[ rustfmt:: skip]
40
+ let Some ( $name) = $this. $name. get( ) else {
41
+ unreachable!(
42
+ "ivar should be initialized: `{}`" ,
43
+ stringify!( $name)
44
+ )
45
+ } ;
37
46
} ;
38
47
}
39
48
40
49
declare_class ! (
41
50
struct Delegate {
42
- text_field : IvarDrop <IdCell <NSTextField >, "_text_field " >,
51
+ nav_url : IvarDrop <IdCell <NSTextField >, "_nav_url " >,
43
52
web_view: IvarDrop <IdCell <WKWebView >, "_web_view" >,
44
53
window: IvarDrop <IdCell <NSWindow >, "_window" >,
45
54
}
@@ -56,7 +65,7 @@ declare_class!(
56
65
unsafe fn init( this: * mut Self ) -> Option <NonNull <Self >> {
57
66
let this: Option <& mut Self > = msg_send![ super ( this) , init] ;
58
67
this. map( |this| {
59
- Ivar :: write( & mut this. text_field , IdCell :: default ( ) ) ;
68
+ Ivar :: write( & mut this. nav_url , IdCell :: default ( ) ) ;
60
69
Ivar :: write( & mut this. web_view, IdCell :: default ( ) ) ;
61
70
Ivar :: write( & mut this. window, IdCell :: default ( ) ) ;
62
71
NonNull :: from( this)
@@ -232,9 +241,9 @@ declare_class!(
232
241
web_view. loadRequest( & request) ;
233
242
}
234
243
235
- self . text_field . replace ( Some ( nav_url) ) ;
236
- self . web_view . replace ( Some ( web_view) ) ;
237
- self . window . replace ( Some ( window) ) ;
244
+ idcell! ( nav_url => self ) ;
245
+ idcell! ( web_view => self ) ;
246
+ idcell! ( window => self ) ;
238
247
}
239
248
}
240
249
@@ -268,10 +277,10 @@ declare_class!(
268
277
web_view: & WKWebView ,
269
278
_navigation: Option <& WKNavigation >,
270
279
) {
271
- idcell!( text_field <= self ) ;
280
+ idcell!( nav_url <= self ) ;
272
281
unsafe {
273
282
if let Some ( url) = web_view. URL ( ) . and_then( |url| url. absoluteString( ) ) {
274
- text_field . setStringValue( & url) ;
283
+ nav_url . setStringValue( & url) ;
275
284
}
276
285
}
277
286
}
0 commit comments