Fixed the conversion code to use macros I wrote.

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.
This commit is contained in:
2017-02-15 17:51:13 -05:00
parent 8988f9f9ae
commit 03e9484e52
12 changed files with 684 additions and 1260 deletions

View File

@ -1,9 +1,7 @@
#![feature(convert)]
extern crate alchemy;
use alchemy::F32_BYTES;
use alchemy::{Converter, Endianess, PlatformEndian, Transmutable};
@ -47,12 +45,12 @@ pub fn use_transmutable()
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
// Convert the floating point number into an array of bytes.
buffer = num.to_bytes(endianess);
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_bytes(buffer.as_slice(), endianess);
final_num = f32::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_num);
}

View File

@ -65,7 +65,7 @@ pub fn main()
// Initialize the variables.
quat = Quaternion::<f64>::from_values(6.29f64, 1.9f64, 8.5f64, 7.11f64);
buffer = Vec::with_capacity(SIZE_OF_QUATERNION);
endianess = Endianess::PLATFORM;
endianess = Endianess::Platform;
println!("Transmuting a Quaternion:");
@ -74,11 +74,11 @@ pub fn main()
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
// Convert the Vector2 into an array of bytes.
buffer = quat.to_bytes(endianess);
buffer = quat.as_endian_bytes(endianess);
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a Vector2.
final_quat = Quaternion::from_bytes(buffer.as_slice(), endianess);
final_quat = Quaternion::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_quat);
}

View File

@ -1,9 +1,6 @@
#![feature(convert)]
extern crate alchemy;
use alchemy::U16_BYTES;
use alchemy::{Converter, Endianess, PlatformEndian, Transmutable};
use alchemy::platform_to_network_order;
@ -48,12 +45,12 @@ pub fn use_transmutable()
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
// Convert the short point number into an array of bytes.
buffer = num.to_bytes(endianess);
buffer = num.as_endian_bytes(endianess);
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a short number.
final_num = u16::from_bytes(buffer.as_slice(), endianess);
final_num = u16::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_num);
}

View File

@ -65,7 +65,7 @@ pub fn main()
// Initialize the variables.
vec = Vector2::<u64>::new(629u64, 1985u64);
buffer = Vec::with_capacity(SIZE_OF_VECTOR_2);
endianess = Endianess::PLATFORM;
endianess = Endianess::Platform;
println!("Transmuting a Vector2:");
@ -74,11 +74,11 @@ pub fn main()
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
// Convert the Vector2 into an array of bytes.
buffer = vec.to_bytes(endianess);
buffer = vec.as_endian_bytes(endianess);
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a Vector2.
final_vec = Vector2::from_bytes(buffer.as_slice(), endianess);
final_vec = Vector2::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_vec);
}