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,6 +1,8 @@
use std::mem;
use std::ptr::copy_nonoverlapping;
use ::byte_sized::ByteSized;
use ::byte_sized::{U16_BYTES, U32_BYTES, U64_BYTES};
use ::converter::Converter;
@ -30,6 +32,7 @@ pub type PlatformEndian = LittleEndian;
/// Create an enumeration of the different
/// available endianesses.
#[derive(Clone, Copy)]
pub enum Endianess
{
/// Referes to BigEndian.
@ -52,8 +55,6 @@ macro_rules! read_bytes
{
($buffer: expr, $returnType: ident, $convertFunc: ident) =>
({
use std::$returnType;
// Make sure that there is enough space to read
// a value from the buffer.
assert!($buffer.len() == $returnType::BYTES);
@ -69,21 +70,19 @@ macro_rules! read_bytes
/// and writing them to a buffer.
macro_rules! write_bytes
{
($buffer: expr, $valueType: ident, $num: expr, $convertFunc: ident) =>
($buffer: expr, $valueType: ident, $numBytes: expr, $num: expr, $convertFunc: ident) =>
({
use std::$valueType;
assert!($buffer.len() >= $valueType::BYTES,
"Not enough room in the buffer to write to.");
unsafe
{
let size: usize;
let bytes: [u8; $valueType::BYTES];
let bytes: [u8; $numBytes];
size = $valueType::BYTES as usize;
size = $valueType::BYTES;
bytes =
mem::transmute::<_,[u8; $valueType::BYTES]>($num.$convertFunc());
mem::transmute::<_, [u8; $numBytes]>($num.$convertFunc());
copy_nonoverlapping::<u8>((&bytes).as_ptr(),
$buffer.as_mut_ptr(),
size);
@ -132,17 +131,17 @@ impl Converter for BigEndian
fn u16_to_bytes(buffer: &mut [u8], num: u16)
{
write_bytes!(buffer, u16, num, to_be);
write_bytes!(buffer, u16, U16_BYTES, num, to_be);
}
fn u32_to_bytes(buffer: &mut [u8], num: u32)
{
write_bytes!(buffer, u32, num, to_be);
write_bytes!(buffer, u32, U32_BYTES, num, to_be);
}
fn u64_to_bytes(buffer: &mut [u8], num: u64)
{
write_bytes!(buffer, u64, num, to_be);
write_bytes!(buffer, u64, U64_BYTES, num, to_be);
}
fn usize_to_bytes(buffer: &mut [u8], num: usize)
@ -201,17 +200,17 @@ impl Converter for LittleEndian
fn u16_to_bytes(buffer: &mut [u8], num: u16)
{
write_bytes!(buffer, u16, num, to_le);
write_bytes!(buffer, u16, U16_BYTES, num, to_le);
}
fn u32_to_bytes(buffer: &mut [u8], num: u32)
{
write_bytes!(buffer, u32, num, to_le);
write_bytes!(buffer, u32, U32_BYTES, num, to_le);
}
fn u64_to_bytes(buffer: &mut [u8], num: u64)
{
write_bytes!(buffer, u64, num, to_le);
write_bytes!(buffer, u64, U64_BYTES, num, to_le);
}
fn usize_to_bytes(buffer: &mut [u8], num: usize)