diff --git a/crates/libs/sys/src/lib.rs b/crates/libs/sys/src/lib.rs index 396dd24e1b..1dd5d5a334 100644 --- a/crates/libs/sys/src/lib.rs +++ b/crates/libs/sys/src/lib.rs @@ -8,6 +8,8 @@ Learn more about Rust for Windows here: 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(()) }