Any conversion to bytes now uses a Vec<u8>.

This was changed to make everything easier to use. The mutable buffers
were not working well for combined types. It was move to a growable array
to make these more advanced combined types, other structures, easier to
implement.
This commit is contained in:
Jason Travis Smith
2016-04-14 13:33:07 -04:00
parent e9ab0bdece
commit 4291a1ef5d
6 changed files with 313 additions and 172 deletions

View File

@ -12,22 +12,22 @@ fn use_converter()
{
let num: f32;
let final_num: f32;
let mut buffer: [u8; F32_BYTES];
let mut buffer: Vec<u8>;
// Initialize the variables.
num = 6.291985f32;
buffer = [0u8; F32_BYTES];
buffer = Vec::new();
println!("Converting the value {} into and out of an array of bytes.", num);
println!("Buffer starts as: {}", stringify_array(&buffer));
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
// Convert the floating point number into an array of bytes.
PlatformEndian::f32_to_bytes(&mut buffer, num);
buffer = PlatformEndian::f32_to_bytes(num);
println!("Buffer contains: {}", stringify_array(&buffer));
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a floating point number.
final_num = PlatformEndian::bytes_to_f32(&buffer);
final_num = PlatformEndian::bytes_to_f32(buffer.as_slice());
println!("The buffer converts back to: {}", final_num);
}
@ -36,23 +36,23 @@ pub fn use_transmutable()
let num: f32;
let final_num: f32;
let endianess: Endianess;
let mut buffer: [u8; F32_BYTES];
let mut buffer: Vec<u8>;
// Initialize the variables.
num = 6.291985f32;
buffer = [0u8; F32_BYTES];
buffer = Vec::new();
endianess = Endianess::PLATFORM;
println!("Converting the value {} into and out of an array of bytes.", num);
println!("Buffer starts as: {}", stringify_array(&buffer));
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
// Convert the floating point number into an array of bytes.
num.to_bytes(&mut buffer, endianess);
buffer = num.to_bytes(endianess);
println!("Buffer contains: {}", stringify_array(&buffer));
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a floating point number.
final_num = f32::from_bytes(&buffer, endianess);
final_num = f32::from_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_num);
}