@@ -497,43 +497,26 @@ fn print_fast<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
497497}
498498
499499#[ cfg_attr( any( target_os = "linux" , target_os = "android" ) , inline( never) ) ] // splice fast-path does not require this allocation
500- #[ cfg( any( unix, target_os = "wasi" ) ) ]
501500fn print_unbuffered < R : FdReadable > (
502501 handle : & mut InputHandle < R > ,
503502 stdout : io:: Stdout ,
504503) -> CatResult < ( ) > {
504+ #[ cfg( not( any( unix, target_os = "wasi" ) ) ) ]
505+ let mut stdout = stdout. lock ( ) ;
505506 // todo: since there is no cost by 0-fill, we could use larger heap buffer for throughput
507+ #[ cfg( any( unix, target_os = "wasi" ) ) ]
506508 let mut buf = [ std:: mem:: MaybeUninit :: < u8 > :: uninit ( ) ; 1024 * 64 ] ;
507- // use raw syscall to remove buffering
508- loop {
509- match rustix:: io:: read ( & handle. reader , & mut buf) {
510- Ok ( ( [ ] , _) ) => return Ok ( ( ) ) ,
511- Ok ( ( filled, _) ) => {
512- uucore:: io:: write_all_raw ( & stdout, filled) . inspect_err ( handle_broken_pipe) ?;
513- }
514- Err ( e) if e. kind ( ) != ErrorKind :: Interrupted => return Err ( e. into ( ) ) ,
515- _ => { }
516- }
517- }
518- }
519-
520- #[ cfg( not( any( unix, target_os = "wasi" ) ) ) ]
521- fn print_unbuffered < R : FdReadable > (
522- handle : & mut InputHandle < R > ,
523- stdout : io:: Stdout ,
524- ) -> CatResult < ( ) > {
525- let mut stdout = stdout. lock ( ) ;
509+ #[ cfg( not( any( unix, target_os = "wasi" ) ) ) ]
526510 let mut buf = [ 0 ; 1024 * 64 ] ;
527511 loop {
528- match handle. reader . read ( & mut buf) {
529- Ok ( 0 ) => return Ok ( ( ) ) ,
530- Ok ( n) => {
531- stdout
532- . write_all ( & buf[ ..n] )
533- . inspect_err ( handle_broken_pipe) ?;
534- // we cannot use rustix::io on Windows
535- // really bad workaround for unbuffered write <https://github.com/uutils/coreutils/issues/12188>
536- stdout. flush ( ) . inspect_err ( handle_broken_pipe) ?;
512+ #[ cfg( any( unix, target_os = "wasi" ) ) ]
513+ let res = rustix:: io:: read ( & handle. reader , & mut buf) . map ( |( f, _) | f) ;
514+ #[ cfg( not( any( unix, target_os = "wasi" ) ) ) ]
515+ let res = handle. reader . read ( & mut buf) . map ( |n| & buf[ ..n] ) ;
516+ match res {
517+ Ok ( [ ] ) => return Ok ( ( ) ) ,
518+ Ok ( filled) => {
519+ uucore:: io:: write_all_raw ( & stdout, filled) . inspect_err ( handle_broken_pipe) ?;
537520 }
538521 Err ( e) if e. kind ( ) != ErrorKind :: Interrupted => return Err ( e. into ( ) ) ,
539522 _ => { }
0 commit comments