Skip to content

Commit

Permalink
shared reference to mutable static
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 23, 2024
1 parent 202bff2 commit 9863c4d
Showing 1 changed file with 39 additions and 43 deletions.
82 changes: 39 additions & 43 deletions crates/tests/misc/implement/tests/identity.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand All @@ -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(())
}

0 comments on commit 9863c4d

Please sign in to comment.