@@ -132,11 +132,11 @@ pub(crate) struct AsyncCursor {
132132/// Macro to generate functions to read scalar values from the cursor
133133macro_rules! impl_read_byteorder {
134134 ( $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 ? ) ;
137137 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 >( ) ? ) ,
140140 }
141141 }
142142 } ;
@@ -157,7 +157,7 @@ impl AsyncCursor {
157157 pub ( crate ) async fn try_open_tiff ( reader : Box < dyn AsyncFileReader > ) -> Result < Self > {
158158 // Initialize with default endianness and then set later
159159 let mut cursor = Self :: new ( reader, Default :: default ( ) ) ;
160- let magic_bytes = cursor. read ( 2 ) . await ;
160+ let magic_bytes = cursor. read ( 2 ) . await ? ;
161161
162162 // Should be b"II" for little endian or b"MM" for big endian
163163 if magic_bytes == Bytes :: from_static ( b"II" ) {
@@ -179,22 +179,22 @@ impl AsyncCursor {
179179 }
180180
181181 /// 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 > {
183183 let range = self . offset as _ ..( self . offset + length) as _ ;
184184 self . offset += length;
185- self . reader . get_bytes ( range) . await . unwrap ( )
185+ self . reader . get_bytes ( range) . await
186186 }
187187
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 ( ) ? )
192192 }
193193
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 ( ) ? )
198198 }
199199
200200 impl_read_byteorder ! ( read_u16, u16 ) ;
@@ -204,20 +204,22 @@ impl AsyncCursor {
204204 impl_read_byteorder ! ( read_i32, i32 ) ;
205205 impl_read_byteorder ! ( read_i64, i64 ) ;
206206
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)
213214 }
214215
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)
221223 }
222224
223225 #[ allow( dead_code) ]
0 commit comments