@@ -132,11 +132,11 @@ pub(crate) struct AsyncCursor {
132
132
/// Macro to generate functions to read scalar values from the cursor
133
133
macro_rules! impl_read_byteorder {
134
134
( $method_name: ident, $typ: ty) => {
135
- pub ( crate ) async fn $method_name( & mut self ) -> $typ {
136
- let mut buf = Cursor :: new( self . read( <$typ>:: BITS as usize / 8 ) . await ) ;
135
+ pub ( crate ) async fn $method_name( & mut self ) -> Result < $typ> {
136
+ let mut buf = Cursor :: new( self . read( <$typ>:: BITS as usize / 8 ) . await ? ) ;
137
137
match self . endianness {
138
- Endianness :: LittleEndian => buf. $method_name:: <LittleEndian >( ) . unwrap ( ) ,
139
- Endianness :: BigEndian => buf. $method_name:: <BigEndian >( ) . unwrap ( ) ,
138
+ Endianness :: LittleEndian => Ok ( buf. $method_name:: <LittleEndian >( ) ? ) ,
139
+ Endianness :: BigEndian => Ok ( buf. $method_name:: <BigEndian >( ) ? ) ,
140
140
}
141
141
}
142
142
} ;
@@ -157,7 +157,7 @@ impl AsyncCursor {
157
157
pub ( crate ) async fn try_open_tiff ( reader : Box < dyn AsyncFileReader > ) -> Result < Self > {
158
158
// Initialize with default endianness and then set later
159
159
let mut cursor = Self :: new ( reader, Default :: default ( ) ) ;
160
- let magic_bytes = cursor. read ( 2 ) . await ;
160
+ let magic_bytes = cursor. read ( 2 ) . await ? ;
161
161
162
162
// Should be b"II" for little endian or b"MM" for big endian
163
163
if magic_bytes == Bytes :: from_static ( b"II" ) {
@@ -179,22 +179,22 @@ impl AsyncCursor {
179
179
}
180
180
181
181
/// Read the given number of bytes, advancing the internal cursor state by the same amount.
182
- pub ( crate ) async fn read ( & mut self , length : usize ) -> Bytes {
182
+ pub ( crate ) async fn read ( & mut self , length : usize ) -> Result < Bytes > {
183
183
let range = self . offset as _ ..( self . offset + length) as _ ;
184
184
self . offset += length;
185
- self . reader . get_bytes ( range) . await . unwrap ( )
185
+ self . reader . get_bytes ( range) . await
186
186
}
187
187
188
- /// Read a u8 from the cursor
189
- pub ( crate ) async fn read_u8 ( & mut self ) -> u8 {
190
- let buf = self . read ( 1 ) . await ;
191
- Cursor :: new ( buf) . read_u8 ( ) . unwrap ( )
188
+ /// Read a u8 from the cursor, advancing the internal state by 1 byte.
189
+ pub ( crate ) async fn read_u8 ( & mut self ) -> Result < u8 > {
190
+ let buf = self . read ( 1 ) . await ? ;
191
+ Ok ( Cursor :: new ( buf) . read_u8 ( ) ? )
192
192
}
193
193
194
- /// Read a i8 from the cursor
195
- pub ( crate ) async fn read_i8 ( & mut self ) -> i8 {
196
- let buf = self . read ( 1 ) . await ;
197
- Cursor :: new ( buf) . read_i8 ( ) . unwrap ( )
194
+ /// Read a i8 from the cursor, advancing the internal state by 1 byte.
195
+ pub ( crate ) async fn read_i8 ( & mut self ) -> Result < i8 > {
196
+ let buf = self . read ( 1 ) . await ? ;
197
+ Ok ( Cursor :: new ( buf) . read_i8 ( ) ? )
198
198
}
199
199
200
200
impl_read_byteorder ! ( read_u16, u16 ) ;
@@ -204,20 +204,22 @@ impl AsyncCursor {
204
204
impl_read_byteorder ! ( read_i32, i32 ) ;
205
205
impl_read_byteorder ! ( read_i64, i64 ) ;
206
206
207
- pub ( crate ) async fn read_f32 ( & mut self ) -> f32 {
208
- let mut buf = Cursor :: new ( self . read ( 4 ) . await ) ;
209
- match self . endianness {
210
- Endianness :: LittleEndian => buf. read_f32 :: < LittleEndian > ( ) . unwrap ( ) ,
211
- Endianness :: BigEndian => buf. read_f32 :: < BigEndian > ( ) . unwrap ( ) ,
212
- }
207
+ pub ( crate ) async fn read_f32 ( & mut self ) -> Result < f32 > {
208
+ let mut buf = Cursor :: new ( self . read ( 4 ) . await ?) ;
209
+ let out = match self . endianness {
210
+ Endianness :: LittleEndian => buf. read_f32 :: < LittleEndian > ( ) ?,
211
+ Endianness :: BigEndian => buf. read_f32 :: < BigEndian > ( ) ?,
212
+ } ;
213
+ Ok ( out)
213
214
}
214
215
215
- pub ( crate ) async fn read_f64 ( & mut self ) -> f64 {
216
- let mut buf = Cursor :: new ( self . read ( 8 ) . await ) ;
217
- match self . endianness {
218
- Endianness :: LittleEndian => buf. read_f64 :: < LittleEndian > ( ) . unwrap ( ) ,
219
- Endianness :: BigEndian => buf. read_f64 :: < BigEndian > ( ) . unwrap ( ) ,
220
- }
216
+ pub ( crate ) async fn read_f64 ( & mut self ) -> Result < f64 > {
217
+ let mut buf = Cursor :: new ( self . read ( 8 ) . await ?) ;
218
+ let out = match self . endianness {
219
+ Endianness :: LittleEndian => buf. read_f64 :: < LittleEndian > ( ) ?,
220
+ Endianness :: BigEndian => buf. read_f64 :: < BigEndian > ( ) ?,
221
+ } ;
222
+ Ok ( out)
221
223
}
222
224
223
225
#[ allow( dead_code) ]
0 commit comments