Created a trait for specifying byte size for a type.

There is now a ByteSized trait that can be used to specify how many
bytes it takes to represent a piece of data. This has been implemented
for all of the primitive number types. With this information Transmutable
was able to create a Vector2 wrapper that will need to be tested. If it
works then soon after Vectors and other Sigils types can be turned into
Transmutable types.

This also has the changes for the original tests and example
to use the new ByteSized information.
This commit is contained in:
Jason Travis Smith
2016-01-06 23:37:18 -05:00
parent d9a3e21aec
commit ed44f4f861
6 changed files with 106 additions and 65 deletions

View File

@ -1,5 +1,4 @@
#![feature(zero_one)]
#![feature(num_bits_bytes)]
extern crate rand;
@ -10,6 +9,10 @@ extern crate sigils;
use std::thread::{Builder, JoinHandle};
use alchemy::{U16_BYTES, U32_BYTES, U64_BYTES};
use alchemy::{I16_BYTES, I32_BYTES, I64_BYTES};
use alchemy::{USIZE_BYTES, ISIZE_BYTES};
use alchemy::{F32_BYTES, F64_BYTES};
const EXHAUSTIVE_RUNS: u64 = 10000u64;
@ -92,7 +95,6 @@ macro_rules! full_check
{
fn $func_name() -> bool
{
use std::$T;
full_check_impl!($T, $read_func, $write_func, $T::BYTES)
}
};
@ -113,8 +115,6 @@ macro_rules! overflow_impl
($numBytes: expr, $T: ident,
$read_func: ident, $write_func: ident) =>
{
use std::num::Zero;
use alchemy::{Converter, BigEndian, LittleEndian};
@ -166,56 +166,49 @@ macro_rules! overflow_impl
macro_rules! test_buffer_overflow
{
($mod_name: ident, $T: ident,
$read_func: ident, $write_func: ident) =>
$read_func: ident, $write_func: ident, $numBytes: ident) =>
{
mod $mod_name
{
use std::$T;
overflow_impl!($T::BYTES, $T, $read_func, $write_func);
}
};
use alchemy::$numBytes;
use std::num::Zero;
($mod_name: ident, $T: ident,
$read_func: ident, $write_func: ident, $numBytes: expr) =>
{
mod $mod_name
{
overflow_impl!($numBytes, $T, $read_func, $write_func);
}
};
}
}
// Test the different data types for buffer overflow.
test_buffer_overflow!(overflow_u16, u16, bytes_to_u16, u16_to_bytes);
test_buffer_overflow!(overflow_u32, u32, bytes_to_u32, u32_to_bytes);
test_buffer_overflow!(overflow_u64, u64, bytes_to_u64, u64_to_bytes);
test_buffer_overflow!(overflow_u16, u16, bytes_to_u16, u16_to_bytes, U16_BYTES);
test_buffer_overflow!(overflow_u32, u32, bytes_to_u32, u32_to_bytes, U32_BYTES);
test_buffer_overflow!(overflow_u64, u64, bytes_to_u64, u64_to_bytes, U64_BYTES);
test_buffer_overflow!(overflow_usize, usize,
bytes_to_usize, usize_to_bytes, 1);
bytes_to_usize, usize_to_bytes, U8_BYTES);
test_buffer_overflow!(overflow_i16, i16, bytes_to_i16, i16_to_bytes);
test_buffer_overflow!(overflow_i32, i32, bytes_to_i32, i32_to_bytes);
test_buffer_overflow!(overflow_i64, i64, bytes_to_i64, i64_to_bytes);
test_buffer_overflow!(overflow_i16, i16, bytes_to_i16, i16_to_bytes, I16_BYTES);
test_buffer_overflow!(overflow_i32, i32, bytes_to_i32, i32_to_bytes, I32_BYTES);
test_buffer_overflow!(overflow_i64, i64, bytes_to_i64, i64_to_bytes, I64_BYTES);
test_buffer_overflow!(overflow_isize, isize,
bytes_to_isize, isize_to_bytes, 1);
bytes_to_isize, isize_to_bytes, U8_BYTES);
test_buffer_overflow!(overflow_f32, f32, bytes_to_f32, f32_to_bytes, 4);
test_buffer_overflow!(overflow_f64, f64, bytes_to_f64, f64_to_bytes, 8);
test_buffer_overflow!(overflow_f32, f32, bytes_to_f32, f32_to_bytes, F32_BYTES);
test_buffer_overflow!(overflow_f64, f64, bytes_to_f64, f64_to_bytes, F64_BYTES);
// Create the exhaustive check functions for the integer types.
full_check!(check_u16, u16, bytes_to_u16, u16_to_bytes);
full_check!(check_u32, u32, bytes_to_u32, u32_to_bytes);
full_check!(check_u64, u64, bytes_to_u64, u64_to_bytes);
full_check!(check_usize, usize, bytes_to_usize, usize_to_bytes);
full_check!(check_u16, u16, bytes_to_u16, u16_to_bytes, U16_BYTES);
full_check!(check_u32, u32, bytes_to_u32, u32_to_bytes, U32_BYTES);
full_check!(check_u64, u64, bytes_to_u64, u64_to_bytes, U64_BYTES);
full_check!(check_usize, usize, bytes_to_usize, usize_to_bytes, USIZE_BYTES);
full_check!(check_i16, i16, bytes_to_i16, i16_to_bytes);
full_check!(check_i32, i32, bytes_to_i32, i32_to_bytes);
full_check!(check_i64, i64, bytes_to_i64, i64_to_bytes);
full_check!(check_isize, isize, bytes_to_isize, isize_to_bytes);
full_check!(check_i16, i16, bytes_to_i16, i16_to_bytes, I16_BYTES);
full_check!(check_i32, i32, bytes_to_i32, i32_to_bytes, I32_BYTES);
full_check!(check_i64, i64, bytes_to_i64, i64_to_bytes, I64_BYTES);
full_check!(check_isize, isize, bytes_to_isize, isize_to_bytes, ISIZE_BYTES);
full_check!(check_f32, f32, bytes_to_f32, f32_to_bytes, 4);
full_check!(check_f64, f64, bytes_to_f64, f64_to_bytes, 8);
full_check!(check_f32, f32, bytes_to_f32, f32_to_bytes, F32_BYTES);
full_check!(check_f64, f64, bytes_to_f64, f64_to_bytes, F64_BYTES);
#[test]