From 9863c4d40560439f4e80c6aa69f579a55274541f Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 23 Sep 2024 10:56:44 -0500 Subject: [PATCH] shared reference to mutable static --- crates/tests/misc/implement/tests/identity.rs | 82 +++++++++---------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/crates/tests/misc/implement/tests/identity.rs b/crates/tests/misc/implement/tests/identity.rs index 87ad3980f5..19ce6b3385 100644 --- a/crates/tests/misc/implement/tests/identity.rs +++ b/crates/tests/misc/implement/tests/identity.rs @@ -1,26 +1,23 @@ #![allow(non_snake_case)] +use std::sync::atomic::*; use windows::{core::*, Foundation::*}; -static mut COUNTER: isize = 0; +static COUNTER: AtomicIsize = AtomicIsize::new(0); #[implement(IStringable, IClosable)] struct Test(String); impl Test { fn new(value: &str) -> Self { - unsafe { - COUNTER += 1; - } + COUNTER.fetch_add(1, Ordering::Relaxed); Self(value.to_string()) } } impl Drop for Test { fn drop(&mut self) { - unsafe { - COUNTER -= 1; - } + COUNTER.fetch_sub(1, Ordering::Release); } } @@ -38,48 +35,47 @@ impl IClosable_Impl for Test_Impl { #[test] fn identity() -> Result<()> { - unsafe { - assert_eq!(COUNTER, 0); - { - let a: IStringable = Test::new("test").into(); - assert!(a.ToString()? == "test"); + assert_eq!(COUNTER.load(Ordering::Acquire), 0); + { + let a: IStringable = Test::new("test").into(); + assert_eq!(COUNTER.load(Ordering::Acquire), 1); + assert!(a.ToString()? == "test"); - let b: IClosable = a.cast()?; - b.Close()?; + let b: IClosable = a.cast()?; + b.Close()?; - let c: IUnknown = b.cast()?; + let c: IUnknown = b.cast()?; - let d: IInspectable = c.cast()?; + let d: IInspectable = c.cast()?; - assert!(a == d.cast()?); - } - { - let a: IUnknown = Test::new("test").into(); - let b: IClosable = a.cast()?; - let c: IStringable = b.cast()?; - assert!(c.ToString()? == "test"); - } - { - let a: IInspectable = Test::new("test").into(); - let b: IStringable = a.cast()?; - assert!(b.ToString()? == "test"); - } - { - let a: IInspectable = Test::new("test").into(); - assert_eq!(a.GetRuntimeClassName()?, "Windows.Foundation.IStringable"); + assert!(a == d.cast()?); + } + { + let a: IUnknown = Test::new("test").into(); + let b: IClosable = a.cast()?; + let c: IStringable = b.cast()?; + assert!(c.ToString()? == "test"); + } + { + let a: IInspectable = Test::new("test").into(); + let b: IStringable = a.cast()?; + assert!(b.ToString()? == "test"); + } + { + let a: IInspectable = Test::new("test").into(); + assert_eq!(a.GetRuntimeClassName()?, "Windows.Foundation.IStringable"); - let b: IStringable = a.cast()?; - let c: &IInspectable = &b.cast()?; - assert_eq!(c.GetRuntimeClassName()?, "Windows.Foundation.IStringable"); + let b: IStringable = a.cast()?; + let c: &IInspectable = &b.cast()?; + assert_eq!(c.GetRuntimeClassName()?, "Windows.Foundation.IStringable"); - let d: IClosable = a.cast()?; - let e: &IInspectable = (&d).into(); - assert_eq!(e.GetRuntimeClassName()?, "Windows.Foundation.IClosable"); + let d: IClosable = a.cast()?; + let e: &IInspectable = (&d).into(); + assert_eq!(e.GetRuntimeClassName()?, "Windows.Foundation.IClosable"); - let f: IInspectable = e.cast()?; - assert_eq!(f.GetRuntimeClassName()?, "Windows.Foundation.IStringable"); - } - assert_eq!(COUNTER, 0); - Ok(()) + let f: IInspectable = e.cast()?; + assert_eq!(f.GetRuntimeClassName()?, "Windows.Foundation.IStringable"); } + assert_eq!(COUNTER.load(Ordering::Acquire), 0); + Ok(()) }