diff --git a/Cargo.lock b/Cargo.lock index f56dd6a..53e7705 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,7 +33,7 @@ dependencies = [ [[package]] name = "sigils" version = "0.1.0" -source = "git+https://gitlab.com/CyberMages/sigils.git#9306a73f9a0d03a15f0f870fee18024880c7cf2a" +source = "git+https://gitlab.com/CyberMages/sigils.git#e64c3b0fbecca351e0a078fefc4273a0f0b8e6ad" [[package]] name = "winapi" diff --git a/examples/convert_quaternion.rs b/examples/convert_quaternion.rs index 4f353bb..a9344cf 100644 --- a/examples/convert_quaternion.rs +++ b/examples/convert_quaternion.rs @@ -71,9 +71,8 @@ pub fn main() println!("Transmuting a Quaternion:"); - println!("Converting the value [{1}, <{2}, {3}, {4}>] {0}", - "into and out of an array of bytes.", - quat.scalar, quat.vector.x, quat.vector.y, quat.vector.z); + println!("Converting the value {} into and out of an array of bytes.", + quat); println!("Buffer starts as: {}", stringify_array(&buffer)); // Convert the Vector2 into an array of bytes. @@ -83,7 +82,5 @@ pub fn main() // Convert the array of bytes into a Vector2. final_quat = Quaternion::from_bytes(&buffer, endianess); - println!("The buffer converts back to: [{}, <{}, {}, {}>]", - final_quat.scalar, final_quat.vector.x, - final_quat.vector.y, final_quat.vector.z); + println!("The buffer converts back to: {}", final_quat); } diff --git a/examples/convert_vector2.rs b/examples/convert_vector2.rs index 1679afb..b528d82 100644 --- a/examples/convert_vector2.rs +++ b/examples/convert_vector2.rs @@ -71,8 +71,8 @@ pub fn main() println!("Transmuting a Vector2:"); - println!("Converting the value [{}, {}] into and out of an array of bytes.", - vec.x, vec.y); + println!("Converting the value {} into and out of an array of bytes.", + vec); println!("Buffer starts as: {}", stringify_array(&buffer)); // Convert the Vector2 into an array of bytes. @@ -82,5 +82,5 @@ pub fn main() // Convert the array of bytes into a Vector2. final_vec = Vector2::from_bytes(&buffer, endianess); - println!("The buffer converts back to: [{}, {}]", final_vec.x, final_vec.y); + println!("The buffer converts back to: {}", final_vec); } diff --git a/src/byte_sized.rs b/src/byte_sized.rs index c9f66bc..f53bbd5 100644 --- a/src/byte_sized.rs +++ b/src/byte_sized.rs @@ -84,6 +84,7 @@ pub trait ByteSized } + macro_rules! byte_sized_impl { ($(($varType: ty, $numBytes: expr))*) => diff --git a/src/lib.rs b/src/lib.rs index 323fa0b..fbe872c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,8 @@ +//! The Alchemy library is a data type to byte converter library. +//! Alchemy handles converting numbers to and from bytes +//! in either big or little endian format. #![feature(associated_consts)] +#![warn(missing_docs)] extern crate sigils; diff --git a/src/transmutable.rs b/src/transmutable.rs index 006adcd..340fa13 100644 --- a/src/transmutable.rs +++ b/src/transmutable.rs @@ -8,6 +8,20 @@ use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess}; +// From and Into are not used because we need to also +// know the endianess to use for converting. +/// A type that can be converted to and from bytes. +pub trait Transmutable +{ + /// Transmute this type to an array of bytes. + fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess); + + /// Transmute an array of bytes to this type. + fn from_bytes(buffer: &[u8], endianess: Endianess) -> Self; +} + + + /// Handles the repetative endianess matching /// for the primitive number types when converting /// a number to bytes. @@ -69,20 +83,6 @@ macro_rules! handle_endianess_from_bytes -// From and Into are not used because we need to also -// know the endianess to use for converting. -/// A type that can be converted to and from bytes. -pub trait Transmutable -{ - /// Transmute this type to an array of bytes. - fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess); - - /// Transmute an array of bytes to this type. - fn from_bytes(buffer: &[u8], endianess: Endianess) -> Self; -} - - - impl Transmutable for u8 { #[allow(unused_variables)] diff --git a/tests/converter.rs b/tests/converter.rs index 8c7f87c..71717ad 100644 --- a/tests/converter.rs +++ b/tests/converter.rs @@ -44,24 +44,24 @@ macro_rules! create_thread /// macro_rules! full_check_impl { - ($T: ident, $read_func: ident, $write_func: ident, $numBytes: expr) => + ($varType: ident, $read_func: ident, $write_func: ident, $numBytes: expr) => { { - use std::$T; + use std::$varType; use rand::ThreadRng; use rand::distributions::{IndependentSample, Range}; use alchemy::{Converter, BigEndian, LittleEndian}; - let range: Range<$T>; + let range: Range<$varType>; let mut rng: ThreadRng; rng = rand::thread_rng(); - range = Range::new($T::MIN, $T::MAX); + range = Range::new($varType::MIN, $varType::MAX); for _ in 0..EXHAUSTIVE_RUNS { - let val: $T; - let final_big_val: $T; - let final_little_val: $T; + let val: $varType; + let final_big_val: $varType; + let final_little_val: $varType; let mut buffer: [u8; $numBytes]; buffer = [0u8; $numBytes]; @@ -90,20 +90,21 @@ macro_rules! full_check_impl /// macro_rules! full_check { - ($func_name: ident, $T: ident, $read_func: ident, $write_func: ident) => + ($func_name: ident, $varType: ident, $read_func: ident, + $write_func: ident) => { fn $func_name() -> bool { - full_check_impl!($T, $read_func, $write_func, $T::BYTES) + full_check_impl!($varType, $read_func, $write_func, $varType::BYTES) } }; - ($func_name: ident, $T: ident, + ($func_name: ident, $varType: ident, $read_func: ident, $write_func: ident, $numBytes: expr) => { fn $func_name() -> bool { - full_check_impl!($T, $read_func, $write_func, $numBytes) + full_check_impl!($varType, $read_func, $write_func, $numBytes) } }; } @@ -111,7 +112,7 @@ macro_rules! full_check /// This should only be called by the macro below. macro_rules! overflow_impl { - ($numBytes: expr, $T: ident, + ($numBytes: expr, $varType: ident, $read_func: ident, $write_func: ident) => { use alchemy::{Converter, BigEndian, LittleEndian}; @@ -146,7 +147,7 @@ macro_rules! overflow_impl let mut buffer: [u8; $numBytes - 1]; buffer = [0u8; $numBytes - 1]; - BigEndian::$write_func(&mut buffer, $T::zero()); + BigEndian::$write_func(&mut buffer, $varType::zero()); } #[test] @@ -156,7 +157,7 @@ macro_rules! overflow_impl let mut buffer: [u8; $numBytes - 1]; buffer = [0u8; $numBytes - 1]; - LittleEndian::$write_func(&mut buffer, $T::zero()); + LittleEndian::$write_func(&mut buffer, $varType::zero()); } }; } @@ -164,14 +165,14 @@ macro_rules! overflow_impl /// This macro tries to test for buffer overflow happening. macro_rules! test_buffer_overflow { - ($mod_name: ident, $T: ident, + ($mod_name: ident, $varType: ident, $read_func: ident, $write_func: ident, $numBytes: ident) => { mod $mod_name { use alchemy::$numBytes; - overflow_impl!($numBytes, $T, $read_func, $write_func); + overflow_impl!($numBytes, $varType, $read_func, $write_func); } } }