@@ -227,43 +227,43 @@ impl<T: ?Sized + ErrorType> ErrorType for &mut T {
227227 type Error = T :: Error ;
228228}
229229
230- /// Error returned by [`Read::read_exact`]
230+ /// Error returned by [`Read::read_exact`] and [`BufRead::skip_until`]
231231#[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
232232#[ cfg_attr( feature = "defmt-03" , derive( defmt:: Format ) ) ]
233- pub enum ReadExactError < E > {
234- /// An EOF error was encountered before reading the exact amount of requested bytes .
233+ pub enum OperationError < E > {
234+ /// An EOF error was encountered before the operation could complete .
235235 UnexpectedEof ,
236236 /// Error returned by the inner Read.
237237 Other ( E ) ,
238238}
239239
240- impl < E > From < E > for ReadExactError < E > {
240+ impl < E > From < E > for OperationError < E > {
241241 fn from ( err : E ) -> Self {
242242 Self :: Other ( err)
243243 }
244244}
245245
246246#[ cfg( feature = "std" ) ]
247247#[ cfg_attr( docsrs, doc( cfg( feature = "std" ) ) ) ]
248- impl From < ReadExactError < std:: io:: Error > > for std:: io:: Error {
249- fn from ( err : ReadExactError < std:: io:: Error > ) -> Self {
248+ impl From < OperationError < std:: io:: Error > > for std:: io:: Error {
249+ fn from ( err : OperationError < std:: io:: Error > ) -> Self {
250250 match err {
251- ReadExactError :: UnexpectedEof => std:: io:: Error :: new (
251+ OperationError :: UnexpectedEof => std:: io:: Error :: new (
252252 std:: io:: ErrorKind :: UnexpectedEof ,
253253 "UnexpectedEof" . to_owned ( ) ,
254254 ) ,
255- ReadExactError :: Other ( e) => std:: io:: Error :: new ( e. kind ( ) , format ! ( "{e:?}" ) ) ,
255+ OperationError :: Other ( e) => std:: io:: Error :: new ( e. kind ( ) , format ! ( "{e:?}" ) ) ,
256256 }
257257 }
258258}
259259
260- impl < E : fmt:: Debug > fmt:: Display for ReadExactError < E > {
260+ impl < E : fmt:: Debug > fmt:: Display for OperationError < E > {
261261 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
262262 write ! ( f, "{self:?}" )
263263 }
264264}
265265
266- impl < E : fmt:: Debug > core:: error:: Error for ReadExactError < E > { }
266+ impl < E : fmt:: Debug > core:: error:: Error for OperationError < E > { }
267267
268268/// Errors that could be returned by `Write` on `&mut [u8]`.
269269#[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
@@ -340,18 +340,18 @@ pub trait Read: ErrorType {
340340 /// If you are using [`ReadReady`] to avoid blocking, you should not use this function.
341341 /// `ReadReady::read_ready()` returning true only guarantees the first call to `read()` will
342342 /// not block, so this function may still block in subsequent calls.
343- fn read_exact ( & mut self , mut buf : & mut [ u8 ] ) -> Result < ( ) , ReadExactError < Self :: Error > > {
343+ fn read_exact ( & mut self , mut buf : & mut [ u8 ] ) -> Result < ( ) , OperationError < Self :: Error > > {
344344 while !buf. is_empty ( ) {
345345 match self . read ( buf) {
346346 Ok ( 0 ) => break ,
347347 Ok ( n) => buf = & mut buf[ n..] ,
348- Err ( e) => return Err ( ReadExactError :: Other ( e) ) ,
348+ Err ( e) => return Err ( OperationError :: Other ( e) ) ,
349349 }
350350 }
351351 if buf. is_empty ( ) {
352352 Ok ( ( ) )
353353 } else {
354- Err ( ReadExactError :: UnexpectedEof )
354+ Err ( OperationError :: UnexpectedEof )
355355 }
356356 }
357357}
@@ -371,6 +371,36 @@ pub trait BufRead: ErrorType {
371371
372372 /// Tell this buffer that `amt` bytes have been consumed from the buffer, so they should no longer be returned in calls to `fill_buf`.
373373 fn consume ( & mut self , amt : usize ) ;
374+
375+ /// Skips all bytes until the delimiter `byte` or EOF is reached.
376+ ///
377+ /// This function will read (and discard) bytes from the underlying stream, blocking until the
378+ /// delimiter is found, or an EOF condition is reached.
379+ ///
380+ /// If successful, this function will return the total number of bytes read,
381+ /// including the delimiter byte.
382+ fn skip_until ( & mut self , delim : u8 ) -> Result < usize , OperationError < Self :: Error > > {
383+ let mut read: usize = 0 ;
384+ loop {
385+ let ( done, used) = {
386+ let available = self . fill_buf ( ) ?;
387+
388+ if available. is_empty ( ) {
389+ return Err ( OperationError :: UnexpectedEof )
390+ }
391+
392+ match available. iter ( ) . position ( |p| * p == delim) {
393+ Some ( i) => ( true , i + 1 ) ,
394+ None => ( false , available. len ( ) ) ,
395+ }
396+ } ;
397+ self . consume ( used) ;
398+ read += used;
399+ if done || used == 0 {
400+ return Ok ( read) ;
401+ }
402+ }
403+ }
374404}
375405
376406/// Blocking writer.
0 commit comments