1
1
#![ allow( non_snake_case) ]
2
2
3
+ use std:: sync:: atomic:: * ;
3
4
use windows:: { core:: * , Foundation :: * } ;
4
5
5
- static mut COUNTER : isize = 0 ;
6
+ static COUNTER : AtomicIsize = AtomicIsize :: new ( 0 ) ;
6
7
7
8
#[ implement( IStringable , IClosable ) ]
8
9
struct Test ( String ) ;
9
10
10
11
impl Test {
11
12
fn new ( value : & str ) -> Self {
12
- unsafe {
13
- COUNTER += 1 ;
14
- }
13
+ COUNTER . fetch_add ( 1 , Ordering :: Relaxed ) ;
15
14
Self ( value. to_string ( ) )
16
15
}
17
16
}
18
17
19
18
impl Drop for Test {
20
19
fn drop ( & mut self ) {
21
- unsafe {
22
- COUNTER -= 1 ;
23
- }
20
+ COUNTER . fetch_sub ( 1 , Ordering :: Release ) ;
24
21
}
25
22
}
26
23
@@ -38,48 +35,47 @@ impl IClosable_Impl for Test_Impl {
38
35
39
36
#[ test]
40
37
fn identity ( ) -> Result < ( ) > {
41
- unsafe {
42
- assert_eq ! ( COUNTER , 0 ) ;
43
- {
44
- let a : IStringable = Test :: new ( "test" ) . into ( ) ;
45
- assert ! ( a. ToString ( ) ? == "test" ) ;
38
+ assert_eq ! ( COUNTER . load ( Ordering :: Acquire ) , 0 ) ;
39
+ {
40
+ let a : IStringable = Test :: new ( "test" ) . into ( ) ;
41
+ assert_eq ! ( COUNTER . load ( Ordering :: Acquire ) , 1 ) ;
42
+ assert ! ( a. ToString ( ) ? == "test" ) ;
46
43
47
- let b: IClosable = a. cast ( ) ?;
48
- b. Close ( ) ?;
44
+ let b: IClosable = a. cast ( ) ?;
45
+ b. Close ( ) ?;
49
46
50
- let c: IUnknown = b. cast ( ) ?;
47
+ let c: IUnknown = b. cast ( ) ?;
51
48
52
- let d: IInspectable = c. cast ( ) ?;
49
+ let d: IInspectable = c. cast ( ) ?;
53
50
54
- assert ! ( a == d. cast( ) ?) ;
55
- }
56
- {
57
- let a: IUnknown = Test :: new ( "test" ) . into ( ) ;
58
- let b: IClosable = a. cast ( ) ?;
59
- let c: IStringable = b. cast ( ) ?;
60
- assert ! ( c. ToString ( ) ? == "test" ) ;
61
- }
62
- {
63
- let a: IInspectable = Test :: new ( "test" ) . into ( ) ;
64
- let b: IStringable = a. cast ( ) ?;
65
- assert ! ( b. ToString ( ) ? == "test" ) ;
66
- }
67
- {
68
- let a: IInspectable = Test :: new ( "test" ) . into ( ) ;
69
- assert_eq ! ( a. GetRuntimeClassName ( ) ?, "Windows.Foundation.IStringable" ) ;
51
+ assert ! ( a == d. cast( ) ?) ;
52
+ }
53
+ {
54
+ let a: IUnknown = Test :: new ( "test" ) . into ( ) ;
55
+ let b: IClosable = a. cast ( ) ?;
56
+ let c: IStringable = b. cast ( ) ?;
57
+ assert ! ( c. ToString ( ) ? == "test" ) ;
58
+ }
59
+ {
60
+ let a: IInspectable = Test :: new ( "test" ) . into ( ) ;
61
+ let b: IStringable = a. cast ( ) ?;
62
+ assert ! ( b. ToString ( ) ? == "test" ) ;
63
+ }
64
+ {
65
+ let a: IInspectable = Test :: new ( "test" ) . into ( ) ;
66
+ assert_eq ! ( a. GetRuntimeClassName ( ) ?, "Windows.Foundation.IStringable" ) ;
70
67
71
- let b: IStringable = a. cast ( ) ?;
72
- let c: & IInspectable = & b. cast ( ) ?;
73
- assert_eq ! ( c. GetRuntimeClassName ( ) ?, "Windows.Foundation.IStringable" ) ;
68
+ let b: IStringable = a. cast ( ) ?;
69
+ let c: & IInspectable = & b. cast ( ) ?;
70
+ assert_eq ! ( c. GetRuntimeClassName ( ) ?, "Windows.Foundation.IStringable" ) ;
74
71
75
- let d: IClosable = a. cast ( ) ?;
76
- let e: & IInspectable = ( & d) . into ( ) ;
77
- assert_eq ! ( e. GetRuntimeClassName ( ) ?, "Windows.Foundation.IClosable" ) ;
72
+ let d: IClosable = a. cast ( ) ?;
73
+ let e: & IInspectable = ( & d) . into ( ) ;
74
+ assert_eq ! ( e. GetRuntimeClassName ( ) ?, "Windows.Foundation.IClosable" ) ;
78
75
79
- let f: IInspectable = e. cast ( ) ?;
80
- assert_eq ! ( f. GetRuntimeClassName ( ) ?, "Windows.Foundation.IStringable" ) ;
81
- }
82
- assert_eq ! ( COUNTER , 0 ) ;
83
- Ok ( ( ) )
76
+ let f: IInspectable = e. cast ( ) ?;
77
+ assert_eq ! ( f. GetRuntimeClassName ( ) ?, "Windows.Foundation.IStringable" ) ;
84
78
}
79
+ assert_eq ! ( COUNTER . load( Ordering :: Acquire ) , 0 ) ;
80
+ Ok ( ( ) )
85
81
}
0 commit comments