1
1
use crate :: io:: { AssetReader , AssetReaderError , PathStream , Reader } ;
2
2
use alloc:: { borrow:: ToOwned , boxed:: Box , sync:: Arc , vec:: Vec } ;
3
- use bevy_platform:: collections:: HashMap ;
3
+ use bevy_platform:: {
4
+ collections:: HashMap ,
5
+ sync:: { PoisonError , RwLock } ,
6
+ } ;
4
7
use core:: { pin:: Pin , task:: Poll } ;
5
8
use futures_io:: AsyncRead ;
6
9
use futures_lite:: { ready, Stream } ;
7
- use parking_lot:: RwLock ;
8
10
use std:: path:: { Path , PathBuf } ;
9
11
10
12
use super :: AsyncSeekForward ;
@@ -44,13 +46,17 @@ impl Dir {
44
46
if let Some ( parent) = path. parent ( ) {
45
47
dir = self . get_or_insert_dir ( parent) ;
46
48
}
47
- dir. 0 . write ( ) . assets . insert (
48
- path. file_name ( ) . unwrap ( ) . to_string_lossy ( ) . into ( ) ,
49
- Data {
50
- value : value. into ( ) ,
51
- path : path. to_owned ( ) ,
52
- } ,
53
- ) ;
49
+ dir. 0
50
+ . write ( )
51
+ . unwrap_or_else ( PoisonError :: into_inner)
52
+ . assets
53
+ . insert (
54
+ path. file_name ( ) . unwrap ( ) . to_string_lossy ( ) . into ( ) ,
55
+ Data {
56
+ value : value. into ( ) ,
57
+ path : path. to_owned ( ) ,
58
+ } ,
59
+ ) ;
54
60
}
55
61
56
62
/// Removes the stored asset at `path` and returns the `Data` stored if found and otherwise `None`.
@@ -60,21 +66,29 @@ impl Dir {
60
66
dir = self . get_or_insert_dir ( parent) ;
61
67
}
62
68
let key: Box < str > = path. file_name ( ) . unwrap ( ) . to_string_lossy ( ) . into ( ) ;
63
- dir. 0 . write ( ) . assets . remove ( & key)
69
+ dir. 0
70
+ . write ( )
71
+ . unwrap_or_else ( PoisonError :: into_inner)
72
+ . assets
73
+ . remove ( & key)
64
74
}
65
75
66
76
pub fn insert_meta ( & self , path : & Path , value : impl Into < Value > ) {
67
77
let mut dir = self . clone ( ) ;
68
78
if let Some ( parent) = path. parent ( ) {
69
79
dir = self . get_or_insert_dir ( parent) ;
70
80
}
71
- dir. 0 . write ( ) . metadata . insert (
72
- path. file_name ( ) . unwrap ( ) . to_string_lossy ( ) . into ( ) ,
73
- Data {
74
- value : value. into ( ) ,
75
- path : path. to_owned ( ) ,
76
- } ,
77
- ) ;
81
+ dir. 0
82
+ . write ( )
83
+ . unwrap_or_else ( PoisonError :: into_inner)
84
+ . metadata
85
+ . insert (
86
+ path. file_name ( ) . unwrap ( ) . to_string_lossy ( ) . into ( ) ,
87
+ Data {
88
+ value : value. into ( ) ,
89
+ path : path. to_owned ( ) ,
90
+ } ,
91
+ ) ;
78
92
}
79
93
80
94
pub fn get_or_insert_dir ( & self , path : & Path ) -> Dir {
@@ -84,7 +98,7 @@ impl Dir {
84
98
full_path. push ( c) ;
85
99
let name = c. as_os_str ( ) . to_string_lossy ( ) . into ( ) ;
86
100
dir = {
87
- let dirs = & mut dir. 0 . write ( ) . dirs ;
101
+ let dirs = & mut dir. 0 . write ( ) . unwrap_or_else ( PoisonError :: into_inner ) . dirs ;
88
102
dirs. entry ( name)
89
103
. or_insert_with ( || Dir :: new ( full_path. clone ( ) ) )
90
104
. clone ( )
@@ -98,7 +112,13 @@ impl Dir {
98
112
let mut dir = self . clone ( ) ;
99
113
for p in path. components ( ) {
100
114
let component = p. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
101
- let next_dir = dir. 0 . read ( ) . dirs . get ( component) ?. clone ( ) ;
115
+ let next_dir = dir
116
+ . 0
117
+ . read ( )
118
+ . unwrap_or_else ( PoisonError :: into_inner)
119
+ . dirs
120
+ . get ( component) ?
121
+ . clone ( ) ;
102
122
dir = next_dir;
103
123
}
104
124
Some ( dir)
@@ -110,8 +130,14 @@ impl Dir {
110
130
dir = dir. get_dir ( parent) ?;
111
131
}
112
132
113
- path. file_name ( )
114
- . and_then ( |f| dir. 0 . read ( ) . assets . get ( f. to_str ( ) . unwrap ( ) ) . cloned ( ) )
133
+ path. file_name ( ) . and_then ( |f| {
134
+ dir. 0
135
+ . read ( )
136
+ . unwrap_or_else ( PoisonError :: into_inner)
137
+ . assets
138
+ . get ( f. to_str ( ) . unwrap ( ) )
139
+ . cloned ( )
140
+ } )
115
141
}
116
142
117
143
pub fn get_metadata ( & self , path : & Path ) -> Option < Data > {
@@ -120,12 +146,22 @@ impl Dir {
120
146
dir = dir. get_dir ( parent) ?;
121
147
}
122
148
123
- path. file_name ( )
124
- . and_then ( |f| dir. 0 . read ( ) . metadata . get ( f. to_str ( ) . unwrap ( ) ) . cloned ( ) )
149
+ path. file_name ( ) . and_then ( |f| {
150
+ dir. 0
151
+ . read ( )
152
+ . unwrap_or_else ( PoisonError :: into_inner)
153
+ . metadata
154
+ . get ( f. to_str ( ) . unwrap ( ) )
155
+ . cloned ( )
156
+ } )
125
157
}
126
158
127
159
pub fn path ( & self ) -> PathBuf {
128
- self . 0 . read ( ) . path . to_owned ( )
160
+ self . 0
161
+ . read ( )
162
+ . unwrap_or_else ( PoisonError :: into_inner)
163
+ . path
164
+ . to_owned ( )
129
165
}
130
166
}
131
167
@@ -153,7 +189,7 @@ impl Stream for DirStream {
153
189
_cx : & mut core:: task:: Context < ' _ > ,
154
190
) -> Poll < Option < Self :: Item > > {
155
191
let this = self . get_mut ( ) ;
156
- let dir = this. dir . 0 . read ( ) ;
192
+ let dir = this. dir . 0 . read ( ) . unwrap_or_else ( PoisonError :: into_inner ) ;
157
193
158
194
let dir_index = this. dir_index ;
159
195
if let Some ( dir_path) = dir
0 commit comments