11use std:: any:: Any ;
22use std:: collections:: HashMap ;
3+ use std:: fmt:: Debug ;
34use std:: os:: raw:: c_ulonglong;
45use std:: rc:: Rc ;
6+ use std:: sync:: OnceLock ;
57use 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+
2740impl 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
8093extern "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
133146pub 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
142155pub fn finalize_unity_interfaces ( ) {
143- unsafe {
144- UNITY_INTERFACES = None ;
145- }
156+ /* unsafe {
157+ UNITY_INTERFACES.res = None;
158+ }*/
146159}
0 commit comments