11use crate :: io:: { AssetReader , AssetReaderError , PathStream , Reader } ;
2+ use async_channel:: { Sender , Receiver } ;
23use bevy_utils:: HashMap ;
3- use concurrent_queue:: ConcurrentQueue ;
44use parking_lot:: RwLock ;
55use std:: { path:: Path , sync:: Arc } ;
66
@@ -10,7 +10,7 @@ use std::{path::Path, sync::Arc};
1010/// This is built primarily for unit tests.
1111pub struct GatedReader < R : AssetReader > {
1212 reader : R ,
13- gates : Arc < RwLock < HashMap < Box < Path > , ConcurrentQueue < ( ) > > > > ,
13+ gates : Arc < RwLock < HashMap < Box < Path > , ( Sender < ( ) > , Receiver < ( ) > ) > > > ,
1414}
1515
1616impl < R : AssetReader + Clone > Clone for GatedReader < R > {
@@ -24,7 +24,7 @@ impl<R: AssetReader + Clone> Clone for GatedReader<R> {
2424
2525/// Opens path "gates" for a [`GatedReader`].
2626pub struct GateOpener {
27- gates : Arc < RwLock < HashMap < Box < Path > , ConcurrentQueue < ( ) > > > > ,
27+ gates : Arc < RwLock < HashMap < Box < Path > , ( Sender < ( ) > , Receiver < ( ) > ) > > > ,
2828}
2929
3030impl GateOpener {
@@ -34,8 +34,9 @@ impl GateOpener {
3434 let mut gates = self . gates . write ( ) ;
3535 let gates = gates
3636 . entry_ref ( path. as_ref ( ) )
37- . or_insert_with ( ConcurrentQueue :: unbounded) ;
38- gates. push ( ( ) ) . unwrap ( ) ;
37+ . or_insert_with ( async_channel:: unbounded) ;
38+ // Should never fail as these channels are always initialized as unbounded.
39+ gates. 0 . try_send ( ( ) ) . unwrap ( ) ;
3940 }
4041}
4142
@@ -56,13 +57,15 @@ impl<R: AssetReader> GatedReader<R> {
5657
5758impl < R : AssetReader > AssetReader for GatedReader < R > {
5859 async fn read < ' a > ( & ' a self , path : & ' a Path ) -> Result < Box < Reader < ' a > > , AssetReaderError > {
59- {
60+ let receiver = {
6061 let mut gates = self . gates . write ( ) ;
6162 let gates = gates
6263 . entry_ref ( path. as_ref ( ) )
63- . or_insert_with ( ConcurrentQueue :: unbounded) ;
64- gates. pop ( ) . unwrap ( ) ;
65- }
64+ . or_insert_with ( async_channel:: unbounded) ;
65+ gates. 1 . clone ( )
66+ } ;
67+ // Should never fail as these channels are always initialized as unbounded and never closed.
68+ receiver. recv ( ) . await . unwrap ( ) ;
6669 let result = self . reader . read ( path) . await ?;
6770 Ok ( result)
6871 }
0 commit comments