Skip to content

Commit 3c0b6bc

Browse files
committed
Update to Rust 2024 edition and enhance thread safety with OnceLock
1 parent 9a85c31 commit 3c0b6bc

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

unity-native-plugin-tester/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
name = "unity-native-plugin-tester"
33
version = "0.8.0"
44
authors = ["Yasuhiro Taniuchi"]
5-
edition = "2021"
5+
edition = "2024"
66
license = "MIT"
77
description = "Unity Native Plugin API Tester Library"
88
homepage = "https://github.com/aosoft/unity-native-plugin-rs"
9-
repository = "https://github.com/aosoft/unity-native-plugin-tester"
9+
repository = "https://github.com/aosoft/unity-native-plugin-rs"
1010
readme = "README.md"
1111
categories = ["api-bindings", "game-engines"]
1212
keywords = ["unity", "ffi"]

unity-native-plugin-tester/src/interface.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::any::Any;
22
use std::collections::HashMap;
3+
use std::fmt::Debug;
34
use std::os::raw::c_ulonglong;
45
use std::rc::Rc;
6+
use std::sync::OnceLock;
57
use unity_native_plugin_sys::*;
68

79
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
@@ -24,6 +26,17 @@ pub struct TesterContextInterfaces {
2426
interfaces: IUnityInterfaces,
2527
}
2628

29+
impl Debug for TesterContextInterfaces {
30+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31+
f.debug_struct("TesterContextInterfaces").finish()
32+
}
33+
}
34+
35+
36+
// maybe thread safety
37+
unsafe impl Send for TesterContextInterfaces {}
38+
unsafe impl Sync for TesterContextInterfaces {}
39+
2740
impl TesterContextInterfaces {
2841
pub fn new() -> Self {
2942
TesterContextInterfaces {
@@ -54,15 +67,15 @@ impl TesterContextInterfaces {
5467
}
5568

5669
pub fn register_interface<T: UnityInterfaceBase + UnityInterfaceID>(
57-
&mut self,
70+
&self,
5871
interface: Option<Rc<dyn UnityInterfaceBase>>,
5972
) {
6073
let guid = T::get_interface_guid();
6174
self.register_interface_split(guid.m_GUIDHigh, guid.m_GUIDLow, interface);
6275
}
6376

6477
pub fn register_interface_split(
65-
&mut self,
78+
&self,
6679
high: ::std::os::raw::c_ulonglong,
6780
low: ::std::os::raw::c_ulonglong,
6881
interface: Option<Rc<dyn UnityInterfaceBase>>,
@@ -75,11 +88,11 @@ impl TesterContextInterfaces {
7588
}
7689
}
7790

78-
static mut UNITY_INTERFACES: Option<TesterContextInterfaces> = None;
91+
static UNITY_INTERFACES: OnceLock<TesterContextInterfaces> = OnceLock::new();
7992

8093
extern "system" fn get_interface(guid: UnityInterfaceGUID) -> *mut IUnityInterface {
8194
unsafe {
82-
if let Some(i) = UNITY_INTERFACES.as_ref().unwrap().get_interface(guid) {
95+
if let Some(i) = UNITY_INTERFACES.get().unwrap().get_interface(guid) {
8396
i.as_ref().get_unity_interface()
8497
} else {
8598
std::ptr::null_mut()
@@ -95,7 +108,7 @@ extern "system" fn get_interface_split(
95108
) -> *mut IUnityInterface {
96109
unsafe {
97110
if let Some(i) = UNITY_INTERFACES
98-
.as_ref()
111+
.get()
99112
.unwrap()
100113
.get_interface_split(high, low)
101114
{
@@ -113,9 +126,9 @@ extern "system" fn register_interface_split(
113126
) {
114127
}
115128

116-
pub unsafe fn get_unity_interfaces() -> &'static mut TesterContextInterfaces {
129+
pub unsafe fn get_unity_interfaces() -> &'static TesterContextInterfaces {
117130
unsafe {
118-
UNITY_INTERFACES.as_mut().unwrap()
131+
UNITY_INTERFACES.get().unwrap()
119132
}
120133
}
121134

@@ -132,15 +145,15 @@ pub unsafe fn get_unity_interface<T: UnityInterfaceBase + UnityInterfaceID>() ->
132145

133146
pub fn initialize_unity_interfaces() {
134147
unsafe {
135-
UNITY_INTERFACES = Some(TesterContextInterfaces::new());
148+
UNITY_INTERFACES.set(TesterContextInterfaces::new()).unwrap();
136149
unity_native_plugin::interface::UnityInterfaces::set_native_unity_interfaces(
137150
crate::interface::get_unity_interfaces().interfaces(),
138151
);
139152
}
140153
}
141154

142155
pub fn finalize_unity_interfaces() {
143-
unsafe {
144-
UNITY_INTERFACES = None;
145-
}
156+
/*unsafe {
157+
UNITY_INTERFACES.res = None;
158+
}*/
146159
}

0 commit comments

Comments
 (0)