Skip to content

Commit 588e291

Browse files
committed
Switch UNITY_INTERFACES to OnceLock for thread safety
1 parent 6a94486 commit 588e291

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

unity-native-plugin/src/interface.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
use unity_native_plugin_sys::*;
2+
use std::sync::OnceLock;
23

34
pub trait UnityInterface {
45
fn get_interface_guid() -> UnityInterfaceGUID;
56
fn new(interface: *const IUnityInterface) -> Self;
67
}
78

8-
static mut UNITY_INTERFACES: Option<UnityInterfaces> = None;
9+
static UNITY_INTERFACES: OnceLock<UnityInterfaces> = OnceLock::new();
910

1011
pub struct UnityInterfaces {
1112
interfaces: *mut unity_native_plugin_sys::IUnityInterfaces,
1213
}
1314

15+
// maybe thread safety
16+
unsafe impl Send for UnityInterfaces {}
17+
unsafe impl Sync for UnityInterfaces {}
18+
19+
1420
impl UnityInterfaces {
1521
pub fn get() -> &'static UnityInterfaces {
16-
unsafe { UNITY_INTERFACES.as_ref().unwrap() }
22+
UNITY_INTERFACES.get().unwrap()
1723
}
1824

1925
pub fn set_native_unity_interfaces(interfaces: *mut unity_native_plugin_sys::IUnityInterfaces) {
20-
unsafe {
21-
UNITY_INTERFACES = if !interfaces.is_null() {
22-
Some(UnityInterfaces { interfaces })
23-
} else {
24-
None
25-
}
26+
if !interfaces.is_null() {
27+
let unity_interfaces = UnityInterfaces { interfaces };
28+
let _ = UNITY_INTERFACES.set(unity_interfaces);
2629
}
2730
}
2831

0 commit comments

Comments
 (0)