@ -7,6 +7,8 @@ use getrandom::*;
mod parse ;
mod r #impl ;
const BYTES_BUFFER_SIZE : usize = 4096 ;
fn get < T : Default > ( ) -> Result < T , Error >
{
let mut value : T = Default ::default ( ) ;
@ -154,19 +156,37 @@ fn bytes(args: &[String]) -> Result<(), Error>
usage ( ) ;
} else {
let num = parse ::bytes ( & args [ 0 ] ) . expect ( "Failed to parse number of bytes" ) as usize ;
let mut mem = Vec ::with_capacity ( num ) ;
debug_assert_eq! ( mem . capacity ( ) , num ) ;
// SAFETY: populating uninitialised memory with random data. There are no reads to the uninitialised data.
unsafe {
mem . set_len ( num ) ;
populate ( & mut mem [ .. ] ) ? ;
}
debug_assert_eq! ( num , mem . len ( ) ) ;
if cfg! ( feature = "bytes-dynamic" ) {
let mut mem = Vec ::with_capacity ( num ) ;
debug_assert_eq! ( mem . capacity ( ) , num ) ;
// SAFETY: populating uninitialised memory with random data. There are no reads to the uninitialised data.
unsafe {
mem . set_len ( num ) ;
populate ( & mut mem [ .. ] ) ? ;
}
debug_assert_eq! ( num , mem . len ( ) ) ;
let stdout = std ::io ::stdout ( ) ;
use std ::io ::Write ;
stdout . lock ( ) . write_all ( & mem [ .. ] ) . expect ( "write error" ) ;
let stdout = std ::io ::stdout ( ) ;
use std ::io ::Write ;
stdout . lock ( ) . write_all ( & mem [ .. ] ) . expect ( "write error" ) ;
} else {
let mut buffer = [ 0 u8 ; BYTES_BUFFER_SIZE ] ;
let mut num = num ;
let stdout = std ::io ::stdout ( ) ;
let mut stdout = stdout . lock ( ) ;
while num > 0 {
let w = std ::cmp ::min ( num , BYTES_BUFFER_SIZE ) ;
getrandom ( & mut buffer [ .. w ] ) ? ;
use std ::io ::Write ;
stdout . write_all ( & buffer [ .. w ] ) . expect ( "write error" ) ;
num - = w ;
}
}
}
Ok ( ( ) )
}