@@ -20,6 +20,8 @@ pub enum DecodeError {
2020 InvalidTag { tag : u8 , sum_name : Option < String > } ,
2121 /// Expected data to be UTF-8 but it wasn't.
2222 InvalidUtf8 ,
23+ /// Expected the byte to be 0 or 1 to be a valid bool.
24+ InvalidBool ( u8 ) ,
2325 /// Custom error not in the other variants of `DecodeError`.
2426 Other ( String ) ,
2527}
@@ -40,6 +42,7 @@ impl fmt::Display for DecodeError {
4042 )
4143 }
4244 DecodeError :: InvalidUtf8 => f. write_str ( "invalid utf8" ) ,
45+ DecodeError :: InvalidBool ( byte) => write ! ( f, "byte {byte} not valid as `bool` (must be 0 or 1)" ) ,
4346 DecodeError :: Other ( err) => f. write_str ( err) ,
4447 }
4548 }
@@ -158,7 +161,7 @@ pub trait BufReader<'de> {
158161 /// Reads and returns a byte slice of `.len() = size` advancing the cursor.
159162 #[ inline]
160163 fn get_slice ( & mut self , size : usize ) -> Result < & ' de [ u8 ] , DecodeError > {
161- self . get_chunk ( size) . ok_or ( DecodeError :: BufferLength {
164+ self . get_chunk ( size) . ok_or_else ( || DecodeError :: BufferLength {
162165 for_type : "[u8]" ,
163166 expected : size,
164167 given : self . remaining ( ) ,
@@ -168,7 +171,7 @@ pub trait BufReader<'de> {
168171 /// Reads an array of type `[u8; N]` from the input.
169172 #[ inline]
170173 fn get_array < const N : usize > ( & mut self ) -> Result < & ' de [ u8 ; N ] , DecodeError > {
171- self . get_array_chunk ( ) . ok_or ( DecodeError :: BufferLength {
174+ self . get_array_chunk ( ) . ok_or_else ( || DecodeError :: BufferLength {
172175 for_type : "[u8; _]" ,
173176 expected : N ,
174177 given : self . remaining ( ) ,
0 commit comments