Skip to content

Commit 9863c4d

Browse files
committed
shared reference to mutable static
1 parent 202bff2 commit 9863c4d

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed
Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
#![allow(non_snake_case)]
22

3+
use std::sync::atomic::*;
34
use windows::{core::*, Foundation::*};
45

5-
static mut COUNTER: isize = 0;
6+
static COUNTER: AtomicIsize = AtomicIsize::new(0);
67

78
#[implement(IStringable, IClosable)]
89
struct Test(String);
910

1011
impl Test {
1112
fn new(value: &str) -> Self {
12-
unsafe {
13-
COUNTER += 1;
14-
}
13+
COUNTER.fetch_add(1, Ordering::Relaxed);
1514
Self(value.to_string())
1615
}
1716
}
1817

1918
impl Drop for Test {
2019
fn drop(&mut self) {
21-
unsafe {
22-
COUNTER -= 1;
23-
}
20+
COUNTER.fetch_sub(1, Ordering::Release);
2421
}
2522
}
2623

@@ -38,48 +35,47 @@ impl IClosable_Impl for Test_Impl {
3835

3936
#[test]
4037
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");
4643

47-
let b: IClosable = a.cast()?;
48-
b.Close()?;
44+
let b: IClosable = a.cast()?;
45+
b.Close()?;
4946

50-
let c: IUnknown = b.cast()?;
47+
let c: IUnknown = b.cast()?;
5148

52-
let d: IInspectable = c.cast()?;
49+
let d: IInspectable = c.cast()?;
5350

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");
7067

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");
7471

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");
7875

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");
8478
}
79+
assert_eq!(COUNTER.load(Ordering::Acquire), 0);
80+
Ok(())
8581
}

0 commit comments

Comments
 (0)