File tree 4 files changed +33
-10
lines changed
4 files changed +33
-10
lines changed Original file line number Diff line number Diff line change @@ -364,6 +364,27 @@ where
364
364
self . pos += n as u64 ;
365
365
Ok ( ( ) )
366
366
}
367
+
368
+ fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
369
+ let content = self . remaining_slice ( ) ;
370
+ let len = content. len ( ) ;
371
+ buf. try_reserve ( len) ?;
372
+ buf. extend_from_slice ( content) ;
373
+ self . pos += len as u64 ;
374
+
375
+ Ok ( len)
376
+ }
377
+
378
+ fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
379
+ let content =
380
+ crate :: str:: from_utf8 ( self . remaining_slice ( ) ) . map_err ( |_| io:: Error :: INVALID_UTF8 ) ?;
381
+ let len = content. len ( ) ;
382
+ buf. try_reserve ( len) ?;
383
+ buf. push_str ( content) ;
384
+ self . pos += len as u64 ;
385
+
386
+ Ok ( len)
387
+ }
367
388
}
368
389
369
390
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
Original file line number Diff line number Diff line change @@ -329,8 +329,9 @@ impl Read for &[u8] {
329
329
#[ inline]
330
330
fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
331
331
let content = str:: from_utf8 ( self ) . map_err ( |_| io:: Error :: INVALID_UTF8 ) ?;
332
- buf. push_str ( content) ;
333
332
let len = self . len ( ) ;
333
+ buf. try_reserve ( len) ?;
334
+ buf. push_str ( content) ;
334
335
* self = & self [ len..] ;
335
336
Ok ( len)
336
337
}
@@ -473,14 +474,8 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
473
474
474
475
#[ inline]
475
476
fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
476
- // We have to use a single contiguous slice because the `VecDequeue` might be split in the
477
- // middle of an UTF-8 character.
478
- let len = self . len ( ) ;
479
- let content = self . make_contiguous ( ) ;
480
- let string = str:: from_utf8 ( content) . map_err ( |_| io:: Error :: INVALID_UTF8 ) ?;
481
- buf. push_str ( string) ;
482
- self . clear ( ) ;
483
- Ok ( len)
477
+ // SAFETY: We only append to the buffer
478
+ unsafe { io:: append_to_string ( buf, |buf| self . read_to_end ( buf) ) }
484
479
}
485
480
}
486
481
Original file line number Diff line number Diff line change @@ -384,7 +384,10 @@ where
384
384
{
385
385
let mut g = Guard { len : buf. len ( ) , buf : buf. as_mut_vec ( ) } ;
386
386
let ret = f ( g. buf ) ;
387
- if str:: from_utf8 ( & g. buf [ g. len ..] ) . is_err ( ) {
387
+
388
+ // SAFETY: the caller promises to only append data to `buf`
389
+ let appended = g. buf . get_unchecked ( g. len ..) ;
390
+ if str:: from_utf8 ( appended) . is_err ( ) {
388
391
ret. and_then ( |_| Err ( Error :: INVALID_UTF8 ) )
389
392
} else {
390
393
g. len = g. buf . len ( ) ;
Original file line number Diff line number Diff line change @@ -486,6 +486,10 @@ impl Read for ChildStderr {
486
486
fn is_read_vectored ( & self ) -> bool {
487
487
self . inner . is_read_vectored ( )
488
488
}
489
+
490
+ fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
491
+ self . inner . read_to_end ( buf)
492
+ }
489
493
}
490
494
491
495
impl AsInner < AnonPipe > for ChildStderr {
You can’t perform that action at this time.
0 commit comments