The previous code was relying on the to_be and to_le functions from the standard library. These now rely on macros that I wrote to handle the bit packing and unpacking. These were macros and not functions because it is really hard to define the types and capabilities needed to turn them into general functions. As such, the macros should never be published publically and the Converters should be used for conversions. The Transmutable property should be used for serialization and internally the primitive types, Elements, all use the Converter functions and any other objects should use the Transmutable primitives to build their binary forms.
107 lines
2.5 KiB
Rust
107 lines
2.5 KiB
Rust
extern crate alchemy;
|
|
|
|
|
|
|
|
use alchemy::{Converter, Endianess, PlatformEndian, Transmutable};
|
|
|
|
|
|
|
|
fn use_converter()
|
|
{
|
|
let num: f32;
|
|
let final_num: f32;
|
|
let mut buffer: Vec<u8>;
|
|
|
|
// Initialize the variables.
|
|
num = 6.291985f32;
|
|
buffer = Vec::new();
|
|
|
|
println!("Converting the value {} into and out of an array of bytes.", num);
|
|
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
|
|
|
|
// Convert the floating point number into an array of bytes.
|
|
buffer = PlatformEndian::f32_to_bytes(num);
|
|
|
|
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.as_slice());
|
|
println!("The buffer converts back to: {}", final_num);
|
|
}
|
|
|
|
pub fn use_transmutable()
|
|
{
|
|
let num: f32;
|
|
let final_num: f32;
|
|
let endianess: Endianess;
|
|
let mut buffer: Vec<u8>;
|
|
|
|
// Initialize the variables.
|
|
num = 6.291985f32;
|
|
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.as_slice()));
|
|
|
|
// Convert the floating point number into an array of bytes.
|
|
buffer = num.as_endian_bytes(endianess);
|
|
|
|
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
|
|
|
|
// Convert the array of bytes into a floating point number.
|
|
final_num = f32::from_endian_bytes(buffer.as_slice(), endianess);
|
|
println!("The buffer converts back to: {}", final_num);
|
|
}
|
|
|
|
/// 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()
|
|
{
|
|
println!("Using converter:");
|
|
use_converter();
|
|
|
|
println!("");
|
|
|
|
println!("Using transmutable:");
|
|
use_transmutable();
|
|
}
|
|
|