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);
}

View File

@ -1,5 +1,3 @@
#![feature(convert)]
extern crate alchemy;
extern crate sigils;
@ -62,25 +60,25 @@ pub fn main()
let quat: Quaternion<f64>;
let final_quat: Quaternion<f64>;
let endianess: Endianess;
let mut buffer: [u8; SIZE_OF_QUATERNION];
let mut buffer: Vec<u8>;
// Initialize the variables.
quat = Quaternion::<f64>::from_values(6.29f64, 1.9f64, 8.5f64, 7.11f64);
buffer = [0u8; SIZE_OF_QUATERNION];
buffer = Vec::with_capacity(SIZE_OF_QUATERNION);
endianess = Endianess::PLATFORM;
println!("Transmuting a Quaternion:");
println!("Converting the value {} into and out of an array of bytes.",
quat);
println!("Buffer starts as: {}", stringify_array(&buffer));
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
// Convert the Vector2 into an array of bytes.
quat.to_bytes(&mut buffer, endianess);
buffer = quat.to_bytes(endianess);
println!("Buffer contains: {}", stringify_array(&buffer));
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a Vector2.
final_quat = Quaternion::from_bytes(&buffer, endianess);
final_quat = Quaternion::from_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_quat);
}

View File

@ -1,5 +1,3 @@
#![feature(convert)]
extern crate alchemy;
extern crate sigils;
@ -62,25 +60,25 @@ pub fn main()
let vec: Vector2<u64>;
let final_vec: Vector2<u64>;
let endianess: Endianess;
let mut buffer: [u8; SIZE_OF_VECTOR_2];
let mut buffer: Vec<u8>;
// Initialize the variables.
vec = Vector2::<u64>::new(629u64, 1985u64);
buffer = [0u8; SIZE_OF_VECTOR_2];
buffer = Vec::with_capacity(SIZE_OF_VECTOR_2);
endianess = Endianess::PLATFORM;
println!("Transmuting a Vector2:");
println!("Converting the value {} into and out of an array of bytes.",
vec);
println!("Buffer starts as: {}", stringify_array(&buffer));
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
// Convert the Vector2 into an array of bytes.
vec.to_bytes(&mut buffer, endianess);
buffer = vec.to_bytes(endianess);
println!("Buffer contains: {}", stringify_array(&buffer));
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a Vector2.
final_vec = Vector2::from_bytes(&buffer, endianess);
final_vec = Vector2::from_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_vec);
}