@@ -13,17 +13,20 @@ use p2p::{
1313 traits:: { ServiceHandle , ServiceProtocol } ,
1414 ProtocolId ,
1515} ;
16- use std:: { sync:: Once , thread} ;
16+ use std:: {
17+ sync:: { Once , OnceLock } ,
18+ thread,
19+ } ;
1720use tokio_util:: codec:: length_delimited:: Builder ;
1821
1922static START_SECIO : Once = Once :: new ( ) ;
2023static START_NO_SECIO : Once = Once :: new ( ) ;
2124
22- static mut SECIO_CONTROL : Option < ServiceControl > = None ;
23- static mut NO_SECIO_CONTROL : Option < ServiceControl > = None ;
25+ static SECIO_CONTROL : OnceLock < ServiceControl > = OnceLock :: new ( ) ;
26+ static NO_SECIO_CONTROL : OnceLock < ServiceControl > = OnceLock :: new ( ) ;
2427
25- static mut SECIO_RECV : Option < crossbeam_channel:: Receiver < Notify > > = None ;
26- static mut NO_SECIO_RECV : Option < crossbeam_channel:: Receiver < Notify > > = None ;
28+ static SECIO_RECV : OnceLock < crossbeam_channel:: Receiver < Notify > > = OnceLock :: new ( ) ;
29+ static NO_SECIO_RECV : OnceLock < crossbeam_channel:: Receiver < Notify > > = OnceLock :: new ( ) ;
2730
2831#[ derive( Debug , PartialEq ) ]
2932enum Notify {
@@ -134,10 +137,8 @@ pub fn init() {
134137 } ) ;
135138
136139 assert_eq ! ( client_receiver. recv( ) , Ok ( Notify :: Connected ) ) ;
137- unsafe {
138- SECIO_CONTROL = Some ( control. into ( ) ) ;
139- SECIO_RECV = Some ( client_receiver) ;
140- }
140+ assert ! ( SECIO_CONTROL . set( control. into( ) ) . is_ok( ) ) ;
141+ assert ! ( SECIO_RECV . set( client_receiver) . is_ok( ) ) ;
141142 } ) ;
142143
143144 // init no secio two peers
@@ -174,43 +175,37 @@ pub fn init() {
174175 } ) ;
175176
176177 assert_eq ! ( client_receiver. recv( ) , Ok ( Notify :: Connected ) ) ;
177- unsafe {
178- NO_SECIO_CONTROL = Some ( control. into ( ) ) ;
179- NO_SECIO_RECV = Some ( client_receiver) ;
180- }
178+ assert ! ( NO_SECIO_CONTROL . set( control. into( ) ) . is_ok( ) ) ;
179+ assert ! ( NO_SECIO_RECV . set( client_receiver) . is_ok( ) ) ;
181180 } ) ;
182181}
183182
184183fn secio_and_send_data ( data : & [ u8 ] ) {
185- unsafe {
186- SECIO_CONTROL . as_mut ( ) . map ( |control| {
187- control. filter_broadcast (
188- TargetSession :: All ,
189- ProtocolId :: new ( 1 ) ,
190- Bytes :: from ( data. to_owned ( ) ) ,
191- )
192- } ) ;
193- if let Some ( rev) = SECIO_RECV . as_ref ( ) {
194- assert_eq ! (
195- rev. recv( ) ,
196- Ok ( Notify :: Message ( bytes:: Bytes :: from( data. to_owned( ) ) ) )
197- )
198- }
184+ SECIO_CONTROL . get ( ) . map ( |control| {
185+ control. filter_broadcast (
186+ TargetSession :: All ,
187+ ProtocolId :: new ( 1 ) ,
188+ Bytes :: from ( data. to_owned ( ) ) ,
189+ )
190+ } ) ;
191+ if let Some ( rev) = SECIO_RECV . get ( ) {
192+ assert_eq ! (
193+ rev. recv( ) ,
194+ Ok ( Notify :: Message ( bytes:: Bytes :: from( data. to_owned( ) ) ) )
195+ )
199196 }
200197}
201198
202199fn no_secio_and_send_data ( data : & [ u8 ] ) {
203- unsafe {
204- NO_SECIO_CONTROL . as_mut ( ) . map ( |control| {
205- control. filter_broadcast ( TargetSession :: All , 1 . into ( ) , Bytes :: from ( data. to_owned ( ) ) )
206- } ) ;
200+ NO_SECIO_CONTROL . get ( ) . map ( |control| {
201+ control. filter_broadcast ( TargetSession :: All , 1 . into ( ) , Bytes :: from ( data. to_owned ( ) ) )
202+ } ) ;
207203
208- if let Some ( rev) = NO_SECIO_RECV . as_ref ( ) {
209- assert_eq ! (
210- rev. recv( ) ,
211- Ok ( Notify :: Message ( bytes:: Bytes :: from( data. to_owned( ) ) ) )
212- )
213- }
204+ if let Some ( rev) = NO_SECIO_RECV . get ( ) {
205+ assert_eq ! (
206+ rev. recv( ) ,
207+ Ok ( Notify :: Message ( bytes:: Bytes :: from( data. to_owned( ) ) ) )
208+ )
214209 }
215210}
216211
0 commit comments