@@ -378,6 +378,7 @@ impl Writable for &OsStr {
378378}
379379
380380impl Writable for u64 {
381+ #[ inline]
381382 fn write_all_to ( & self , output : & mut impl OsWrite ) -> Result < ( ) , Error > {
382383 // The itoa crate is surprisingly much more efficient than a formatted write.
383384 // It speeds up `shuf -r -n1000000 -i1-1024` by 1.8×.
@@ -386,6 +387,15 @@ impl Writable for u64 {
386387 }
387388}
388389
390+ #[ cold]
391+ #[ inline( never) ]
392+ fn crash ( e : std:: io:: Error ) -> Box < dyn uucore:: error:: UError > {
393+ use uucore:: error:: FromIo ;
394+ let ctx = translate ! ( "shuf-error-write-failed" ) ;
395+ e. map_err_context ( move || ctx)
396+ }
397+
398+ #[ inline( never) ]
389399fn shuf_exec (
390400 input : & mut impl Shufable ,
391401 opts : & Options ,
@@ -402,17 +412,21 @@ fn shuf_exec(
402412 }
403413 for _ in 0 ..opts. head_count {
404414 let r = input. choose ( rng) ?;
405-
406- r. write_all_to ( output) . map_err_context ( ctx) ?;
407- output. write_all ( & [ opts. sep ] ) . map_err_context ( ctx) ?;
415+ if let Err ( e) = r. write_all_to ( output) {
416+ return Err ( crash ( e) ) ;
417+ }
418+ if let Err ( e) = output. write_all ( & [ opts. sep ] ) {
419+ return Err ( crash ( e) ) ;
420+ }
408421 }
409422 } else {
410423 let shuffled = input. partial_shuffle ( rng, opts. head_count ) ?;
424+ let sep = [ opts. sep ] ;
411425
412426 for r in shuffled {
413427 let r = r?;
414- r. write_all_to ( output) . map_err_context ( ctx ) ?;
415- output. write_all ( & [ opts . sep ] ) . map_err_context ( ctx ) ?;
428+ r. write_all_to ( output) . map_err ( crash ) ?;
429+ output. write_all ( & sep) . map_err ( crash ) ?;
416430 }
417431 }
418432 output. flush ( ) . map_err_context ( ctx) ?;
0 commit comments