33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
55
6- // spell-checker:ignore (ToDO) cmdline evec nonrepeating seps shufable rvec fdata
6+ // spell-checker:ignore (ToDO) cmdline codegen-units evec nonrepeating seps shufable rvec fdata
77
88use std:: ffi:: { OsStr , OsString } ;
99use std:: fs:: File ;
@@ -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,13 +387,23 @@ impl Writable for u64 {
386387 }
387388}
388389
390+ #[ cold]
391+ #[ inline( never) ]
392+ fn error_write_failed ( 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)] //maybe, codegen-units=1 needs it?
389399fn shuf_exec (
390400 input : & mut impl Shufable ,
391401 opts : & Options ,
392402 rng : & mut WrappedRng ,
393403 output : & mut BufWriter < Box < dyn OsWrite > > ,
394404) -> UResult < ( ) > {
395405 let ctx = || translate ! ( "shuf-error-write-failed" ) ;
406+ let sep = [ opts. sep ] ;
396407 if opts. repeat {
397408 if input. is_empty ( ) {
398409 return Err ( USimpleError :: new (
@@ -402,17 +413,21 @@ fn shuf_exec(
402413 }
403414 for _ in 0 ..opts. head_count {
404415 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) ?;
416+ if let Err ( e) = r. write_all_to ( output) {
417+ return Err ( error_write_failed ( e) ) ;
418+ }
419+ if let Err ( e) = output. write_all ( & sep) {
420+ return Err ( error_write_failed ( e) ) ;
421+ }
408422 }
409423 } else {
410424 let shuffled = input. partial_shuffle ( rng, opts. head_count ) ?;
425+ let sep = [ opts. sep ] ;
411426
412427 for r in shuffled {
413428 let r = r?;
414- r. write_all_to ( output) . map_err_context ( ctx ) ?;
415- output. write_all ( & [ opts . sep ] ) . map_err_context ( ctx ) ?;
429+ r. write_all_to ( output) . map_err ( error_write_failed ) ?;
430+ output. write_all ( & sep) . map_err ( error_write_failed ) ?;
416431 }
417432 }
418433 output. flush ( ) . map_err_context ( ctx) ?;
0 commit comments