2016-01-07 05:10:50 -05:00
|
|
|
extern crate alchemy;
|
|
|
|
extern crate sigils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use alchemy::U64_BYTES;
|
|
|
|
use alchemy::{Endianess, Transmutable};
|
|
|
|
use sigils::vector::Vector2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// The size of 2 u64 whole numbers.
|
|
|
|
/// This would be different if the contained type
|
|
|
|
/// was something different, like an i16 or f32.
|
|
|
|
const SIZE_OF_VECTOR_2: usize = U64_BYTES * 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// This just help pretty up the printing of an array of bytes.
|
|
|
|
fn stringify_array(buffer: &[u8]) -> String
|
|
|
|
{
|
|
|
|
let mut result: String;
|
|
|
|
let mut count: usize;
|
|
|
|
|
|
|
|
// Create a new string that starts with just
|
|
|
|
// the array opening bracket.
|
|
|
|
result = String::new();
|
|
|
|
result.push_str("[");
|
|
|
|
|
|
|
|
// Loop through the buffer keeping track
|
|
|
|
// of our place in it.
|
|
|
|
count = 0usize;
|
|
|
|
for byte in buffer
|
|
|
|
{
|
|
|
|
// Handle priting the last value differently.
|
|
|
|
if count >= buffer.len() - 1
|
|
|
|
{
|
|
|
|
result.push_str(byte.to_string().as_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.push_str(byte.to_string().as_str());
|
|
|
|
result.push_str(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark that we are going to look at
|
|
|
|
// the next byte in the array.
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the array closing bracket and
|
|
|
|
// return the new String.
|
|
|
|
result.push_str("]");
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn main()
|
|
|
|
{
|
|
|
|
let vec: Vector2<u64>;
|
|
|
|
let final_vec: Vector2<u64>;
|
|
|
|
let endianess: Endianess;
|
2016-04-14 13:33:07 -04:00
|
|
|
let mut buffer: Vec<u8>;
|
2016-01-07 05:10:50 -05:00
|
|
|
|
|
|
|
// Initialize the variables.
|
|
|
|
vec = Vector2::<u64>::new(629u64, 1985u64);
|
2016-04-14 13:33:07 -04:00
|
|
|
buffer = Vec::with_capacity(SIZE_OF_VECTOR_2);
|
2016-01-07 05:10:50 -05:00
|
|
|
endianess = Endianess::PLATFORM;
|
|
|
|
|
|
|
|
println!("Transmuting a Vector2:");
|
|
|
|
|
2016-01-07 17:49:21 -05:00
|
|
|
println!("Converting the value {} into and out of an array of bytes.",
|
|
|
|
vec);
|
2016-04-14 13:33:07 -04:00
|
|
|
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
|
2016-01-07 05:10:50 -05:00
|
|
|
|
|
|
|
// Convert the Vector2 into an array of bytes.
|
2016-04-14 13:33:07 -04:00
|
|
|
buffer = vec.to_bytes(endianess);
|
2016-01-07 05:10:50 -05:00
|
|
|
|
2016-04-14 13:33:07 -04:00
|
|
|
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
|
2016-01-07 05:10:50 -05:00
|
|
|
|
|
|
|
// Convert the array of bytes into a Vector2.
|
2016-04-14 13:33:07 -04:00
|
|
|
final_vec = Vector2::from_bytes(buffer.as_slice(), endianess);
|
2016-01-07 17:49:21 -05:00
|
|
|
println!("The buffer converts back to: {}", final_vec);
|
2016-01-07 05:10:50 -05:00
|
|
|
}
|