Any conversion to bytes now uses a Vec<u8>.
This was changed to make everything easier to use. The mutable buffers were not working well for combined types. It was move to a growable array to make these more advanced combined types, other structures, easier to implement.
This commit is contained in:
parent
e9ab0bdece
commit
4291a1ef5d
@ -12,22 +12,22 @@ fn use_converter()
|
|||||||
{
|
{
|
||||||
let num: f32;
|
let num: f32;
|
||||||
let final_num: f32;
|
let final_num: f32;
|
||||||
let mut buffer: [u8; F32_BYTES];
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Initialize the variables.
|
// Initialize the variables.
|
||||||
num = 6.291985f32;
|
num = 6.291985f32;
|
||||||
buffer = [0u8; F32_BYTES];
|
buffer = Vec::new();
|
||||||
|
|
||||||
println!("Converting the value {} into and out of an array of bytes.", num);
|
println!("Converting the value {} into and out of an array of bytes.", num);
|
||||||
println!("Buffer starts as: {}", stringify_array(&buffer));
|
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
|
||||||
|
|
||||||
// Convert the floating point number into an array of bytes.
|
// Convert the floating point number into an array of bytes.
|
||||||
PlatformEndian::f32_to_bytes(&mut buffer, num);
|
buffer = PlatformEndian::f32_to_bytes(num);
|
||||||
|
|
||||||
println!("Buffer contains: {}", stringify_array(&buffer));
|
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
|
||||||
|
|
||||||
// Convert the array of bytes into a floating point number.
|
// Convert the array of bytes into a floating point number.
|
||||||
final_num = PlatformEndian::bytes_to_f32(&buffer);
|
final_num = PlatformEndian::bytes_to_f32(buffer.as_slice());
|
||||||
println!("The buffer converts back to: {}", final_num);
|
println!("The buffer converts back to: {}", final_num);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,23 +36,23 @@ pub fn use_transmutable()
|
|||||||
let num: f32;
|
let num: f32;
|
||||||
let final_num: f32;
|
let final_num: f32;
|
||||||
let endianess: Endianess;
|
let endianess: Endianess;
|
||||||
let mut buffer: [u8; F32_BYTES];
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Initialize the variables.
|
// Initialize the variables.
|
||||||
num = 6.291985f32;
|
num = 6.291985f32;
|
||||||
buffer = [0u8; F32_BYTES];
|
buffer = Vec::new();
|
||||||
endianess = Endianess::PLATFORM;
|
endianess = Endianess::PLATFORM;
|
||||||
|
|
||||||
println!("Converting the value {} into and out of an array of bytes.", num);
|
println!("Converting the value {} into and out of an array of bytes.", num);
|
||||||
println!("Buffer starts as: {}", stringify_array(&buffer));
|
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
|
||||||
|
|
||||||
// Convert the floating point number into an array of bytes.
|
// Convert the floating point number into an array of bytes.
|
||||||
num.to_bytes(&mut buffer, endianess);
|
buffer = num.to_bytes(endianess);
|
||||||
|
|
||||||
println!("Buffer contains: {}", stringify_array(&buffer));
|
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
|
||||||
|
|
||||||
// Convert the array of bytes into a floating point number.
|
// Convert the array of bytes into a floating point number.
|
||||||
final_num = f32::from_bytes(&buffer, endianess);
|
final_num = f32::from_bytes(buffer.as_slice(), endianess);
|
||||||
println!("The buffer converts back to: {}", final_num);
|
println!("The buffer converts back to: {}", final_num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
#![feature(convert)]
|
|
||||||
|
|
||||||
extern crate alchemy;
|
extern crate alchemy;
|
||||||
extern crate sigils;
|
extern crate sigils;
|
||||||
|
|
||||||
@ -62,25 +60,25 @@ pub fn main()
|
|||||||
let quat: Quaternion<f64>;
|
let quat: Quaternion<f64>;
|
||||||
let final_quat: Quaternion<f64>;
|
let final_quat: Quaternion<f64>;
|
||||||
let endianess: Endianess;
|
let endianess: Endianess;
|
||||||
let mut buffer: [u8; SIZE_OF_QUATERNION];
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Initialize the variables.
|
// Initialize the variables.
|
||||||
quat = Quaternion::<f64>::from_values(6.29f64, 1.9f64, 8.5f64, 7.11f64);
|
quat = Quaternion::<f64>::from_values(6.29f64, 1.9f64, 8.5f64, 7.11f64);
|
||||||
buffer = [0u8; SIZE_OF_QUATERNION];
|
buffer = Vec::with_capacity(SIZE_OF_QUATERNION);
|
||||||
endianess = Endianess::PLATFORM;
|
endianess = Endianess::PLATFORM;
|
||||||
|
|
||||||
println!("Transmuting a Quaternion:");
|
println!("Transmuting a Quaternion:");
|
||||||
|
|
||||||
println!("Converting the value {} into and out of an array of bytes.",
|
println!("Converting the value {} into and out of an array of bytes.",
|
||||||
quat);
|
quat);
|
||||||
println!("Buffer starts as: {}", stringify_array(&buffer));
|
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
|
||||||
|
|
||||||
// Convert the Vector2 into an array of bytes.
|
// Convert the Vector2 into an array of bytes.
|
||||||
quat.to_bytes(&mut buffer, endianess);
|
buffer = quat.to_bytes(endianess);
|
||||||
|
|
||||||
println!("Buffer contains: {}", stringify_array(&buffer));
|
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
|
||||||
|
|
||||||
// Convert the array of bytes into a Vector2.
|
// Convert the array of bytes into a Vector2.
|
||||||
final_quat = Quaternion::from_bytes(&buffer, endianess);
|
final_quat = Quaternion::from_bytes(buffer.as_slice(), endianess);
|
||||||
println!("The buffer converts back to: {}", final_quat);
|
println!("The buffer converts back to: {}", final_quat);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
#![feature(convert)]
|
|
||||||
|
|
||||||
extern crate alchemy;
|
extern crate alchemy;
|
||||||
extern crate sigils;
|
extern crate sigils;
|
||||||
|
|
||||||
@ -62,25 +60,25 @@ pub fn main()
|
|||||||
let vec: Vector2<u64>;
|
let vec: Vector2<u64>;
|
||||||
let final_vec: Vector2<u64>;
|
let final_vec: Vector2<u64>;
|
||||||
let endianess: Endianess;
|
let endianess: Endianess;
|
||||||
let mut buffer: [u8; SIZE_OF_VECTOR_2];
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Initialize the variables.
|
// Initialize the variables.
|
||||||
vec = Vector2::<u64>::new(629u64, 1985u64);
|
vec = Vector2::<u64>::new(629u64, 1985u64);
|
||||||
buffer = [0u8; SIZE_OF_VECTOR_2];
|
buffer = Vec::with_capacity(SIZE_OF_VECTOR_2);
|
||||||
endianess = Endianess::PLATFORM;
|
endianess = Endianess::PLATFORM;
|
||||||
|
|
||||||
println!("Transmuting a Vector2:");
|
println!("Transmuting a Vector2:");
|
||||||
|
|
||||||
println!("Converting the value {} into and out of an array of bytes.",
|
println!("Converting the value {} into and out of an array of bytes.",
|
||||||
vec);
|
vec);
|
||||||
println!("Buffer starts as: {}", stringify_array(&buffer));
|
println!("Buffer starts as: {}", stringify_array(buffer.as_slice()));
|
||||||
|
|
||||||
// Convert the Vector2 into an array of bytes.
|
// Convert the Vector2 into an array of bytes.
|
||||||
vec.to_bytes(&mut buffer, endianess);
|
buffer = vec.to_bytes(endianess);
|
||||||
|
|
||||||
println!("Buffer contains: {}", stringify_array(&buffer));
|
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
|
||||||
|
|
||||||
// Convert the array of bytes into a Vector2.
|
// Convert the array of bytes into a Vector2.
|
||||||
final_vec = Vector2::from_bytes(&buffer, endianess);
|
final_vec = Vector2::from_bytes(buffer.as_slice(), endianess);
|
||||||
println!("The buffer converts back to: {}", final_vec);
|
println!("The buffer converts back to: {}", final_vec);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
|
use ::byte_sized::ByteSized;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Describes types that can convert Numbers into bytes,
|
/// Describes types that can convert Numbers into bytes,
|
||||||
@ -87,9 +89,9 @@ pub trait Converter
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
/// This will panic if the buffer does not have
|
/// This will panic if the buffer does not have
|
||||||
/// enough space to store the converted value.
|
/// enough space to store the converted value.
|
||||||
fn i16_to_bytes(buffer: &mut [u8], num: i16)
|
fn i16_to_bytes(num: i16) -> Vec<u8>
|
||||||
{
|
{
|
||||||
Self::u16_to_bytes(buffer, num as u16)
|
Self::u16_to_bytes(num as u16)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a signed 32-bit integer to bytes
|
/// Converts a signed 32-bit integer to bytes
|
||||||
@ -98,9 +100,9 @@ pub trait Converter
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
/// This will panic if the buffer does not have
|
/// This will panic if the buffer does not have
|
||||||
/// enough space to store the converted value.
|
/// enough space to store the converted value.
|
||||||
fn i32_to_bytes(buffer: &mut [u8], num: i32)
|
fn i32_to_bytes(num: i32) -> Vec<u8>
|
||||||
{
|
{
|
||||||
Self::u32_to_bytes(buffer, num as u32)
|
Self::u32_to_bytes(num as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a signed 64-bit integer to bytes
|
/// Converts a signed 64-bit integer to bytes
|
||||||
@ -109,9 +111,9 @@ pub trait Converter
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
/// This will panic if the buffer does not have
|
/// This will panic if the buffer does not have
|
||||||
/// enough space to store the converted value.
|
/// enough space to store the converted value.
|
||||||
fn i64_to_bytes(buffer: &mut [u8], num: i64)
|
fn i64_to_bytes(num: i64) -> Vec<u8>
|
||||||
{
|
{
|
||||||
Self::u64_to_bytes(buffer, num as u64)
|
Self::u64_to_bytes(num as u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a signed integer to bytes
|
/// Converts a signed integer to bytes
|
||||||
@ -124,14 +126,12 @@ pub trait Converter
|
|||||||
/// This will panic if the number of bytes
|
/// This will panic if the number of bytes
|
||||||
/// passed in is less than the byte size of the given number
|
/// passed in is less than the byte size of the given number
|
||||||
/// or more than eight.
|
/// or more than eight.
|
||||||
fn isize_to_bytes(buffer: &mut [u8], num: isize)
|
fn isize_to_bytes(num: isize) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let temp_num: usize;
|
let temp_num: usize;
|
||||||
|
|
||||||
assert!(buffer.len() >= 1 && buffer.len() <= 8);
|
temp_num = remove_sign(num as i64, isize::BYTES as u8) as usize;
|
||||||
|
Self::usize_to_bytes(temp_num)
|
||||||
temp_num = remove_sign(num as i64, buffer.len() as u8) as usize;
|
|
||||||
Self::usize_to_bytes(buffer, temp_num)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a 32-bit floating point number to bytes
|
/// Converts a 32-bit floating point number to bytes
|
||||||
@ -140,11 +140,11 @@ pub trait Converter
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
/// This will panic if the buffer does not have
|
/// This will panic if the buffer does not have
|
||||||
/// enough space to store the converted value.
|
/// enough space to store the converted value.
|
||||||
fn f32_to_bytes(buffer: &mut [u8], num: f32)
|
fn f32_to_bytes(num: f32) -> Vec<u8>
|
||||||
{
|
{
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
Self::u32_to_bytes(buffer, mem::transmute::<f32, u32>(num));
|
Self::u32_to_bytes(mem::transmute::<f32, u32>(num))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,11 +154,11 @@ pub trait Converter
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
/// This will panic if the buffer does not have
|
/// This will panic if the buffer does not have
|
||||||
/// enough space to store the converted value.
|
/// enough space to store the converted value.
|
||||||
fn f64_to_bytes(buffer: &mut [u8], num: f64)
|
fn f64_to_bytes(num: f64) -> Vec<u8>
|
||||||
{
|
{
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
Self::u64_to_bytes(buffer, mem::transmute::<f64, u64>(num));
|
Self::u64_to_bytes(mem::transmute::<f64, u64>(num))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,7 +202,7 @@ pub trait Converter
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
/// This will panic if the buffer does not have
|
/// This will panic if the buffer does not have
|
||||||
/// enough space to store the converted value.
|
/// enough space to store the converted value.
|
||||||
fn u16_to_bytes(buffer: &mut [u8], num: u16);
|
fn u16_to_bytes(num: u16) -> Vec<u8>;
|
||||||
|
|
||||||
/// Converts an unsigned 32-bit integer to bytes
|
/// Converts an unsigned 32-bit integer to bytes
|
||||||
/// and places them into the given buffer.
|
/// and places them into the given buffer.
|
||||||
@ -210,7 +210,7 @@ pub trait Converter
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
/// This will panic if the buffer does not have
|
/// This will panic if the buffer does not have
|
||||||
/// enough space to store the converted value.
|
/// enough space to store the converted value.
|
||||||
fn u32_to_bytes(buffer: &mut [u8], num: u32);
|
fn u32_to_bytes(num: u32) -> Vec<u8>;
|
||||||
|
|
||||||
/// Converts an unsigned 64-bit integer to bytes
|
/// Converts an unsigned 64-bit integer to bytes
|
||||||
/// and places them into the given buffer.
|
/// and places them into the given buffer.
|
||||||
@ -218,7 +218,7 @@ pub trait Converter
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
/// This will panic if the buffer does not have
|
/// This will panic if the buffer does not have
|
||||||
/// enough space to store the converted value.
|
/// enough space to store the converted value.
|
||||||
fn u64_to_bytes(buffer: &mut [u8], num: u64);
|
fn u64_to_bytes(num: u64) -> Vec<u8>;
|
||||||
|
|
||||||
/// Converts an unsigned integer to bytes
|
/// Converts an unsigned integer to bytes
|
||||||
/// and places them into the given buffer.
|
/// and places them into the given buffer.
|
||||||
@ -230,7 +230,7 @@ pub trait Converter
|
|||||||
/// This will panic if the number of bytes
|
/// This will panic if the number of bytes
|
||||||
/// passed in is less than the byte size of the given number
|
/// passed in is less than the byte size of the given number
|
||||||
/// or more than eight.
|
/// or more than eight.
|
||||||
fn usize_to_bytes(buffer: &mut [u8], num: usize);
|
fn usize_to_bytes(num: usize) -> Vec<u8>;
|
||||||
|
|
||||||
|
|
||||||
/// Converts a String to bytes
|
/// Converts a String to bytes
|
||||||
@ -250,7 +250,7 @@ pub trait Converter
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
/// This will panic if the buffer does not have
|
/// This will panic if the buffer does not have
|
||||||
/// enough information to convert.
|
/// enough information to convert.
|
||||||
fn string_to_bytes(buffer: &mut [u8], string: String);
|
fn string_to_bytes(string: String) -> Vec<u8>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
149
src/endian.rs
149
src/endian.rs
@ -77,20 +77,13 @@ macro_rules! write_bytes
|
|||||||
($buffer: expr, $valueType: ident, $numBytes: expr,
|
($buffer: expr, $valueType: ident, $numBytes: expr,
|
||||||
$num: expr, $convertFunc: ident) =>
|
$num: expr, $convertFunc: ident) =>
|
||||||
({
|
({
|
||||||
assert!($buffer.len() >= $valueType::BYTES,
|
|
||||||
"Not enough room in the buffer to write to.");
|
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
let size: usize;
|
|
||||||
let bytes: [u8; $numBytes];
|
let bytes: [u8; $numBytes];
|
||||||
|
|
||||||
size = $valueType::BYTES;
|
|
||||||
|
|
||||||
bytes =
|
bytes =
|
||||||
mem::transmute::<_, [u8; $numBytes]>($num.$convertFunc());
|
mem::transmute::<_, [u8; $numBytes]>($num.$convertFunc());
|
||||||
copy_nonoverlapping::<u8>((&bytes).as_ptr(),
|
$buffer.extend_from_slice(&bytes);
|
||||||
$buffer.as_mut_ptr(),
|
|
||||||
size);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -134,38 +127,75 @@ impl Converter for BigEndian
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn u16_to_bytes(buffer: &mut [u8], num: u16)
|
fn u16_to_bytes(num: u16) -> Vec<u8>
|
||||||
{
|
{
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(u16::BYTES);
|
||||||
|
|
||||||
|
// Write the bytes to the Vector.
|
||||||
write_bytes!(buffer, u16, U16_BYTES, num, to_be);
|
write_bytes!(buffer, u16, U16_BYTES, num, to_be);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn u32_to_bytes(buffer: &mut [u8], num: u32)
|
fn u32_to_bytes(num: u32) -> Vec<u8>
|
||||||
{
|
{
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(u32::BYTES);
|
||||||
|
|
||||||
|
// Write the bytes to the Vector.
|
||||||
write_bytes!(buffer, u32, U32_BYTES, num, to_be);
|
write_bytes!(buffer, u32, U32_BYTES, num, to_be);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn u64_to_bytes(buffer: &mut [u8], num: u64)
|
fn u64_to_bytes(num: u64) -> Vec<u8>
|
||||||
{
|
{
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(u64::BYTES);
|
||||||
|
|
||||||
|
// Write the bytes to the Vector.
|
||||||
write_bytes!(buffer, u64, U64_BYTES, num, to_be);
|
write_bytes!(buffer, u64, U64_BYTES, num, to_be);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usize_to_bytes(buffer: &mut [u8], num: usize)
|
fn usize_to_bytes(num: usize) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let bytes: [u8; 8];
|
let bytes: [u8; 8];
|
||||||
let num_bytes: u8;
|
let num_bytes: u8;
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
num_bytes = buffer.len() as u8;
|
num_bytes = usize::BYTES as u8;
|
||||||
|
|
||||||
|
// Create a buffer with enough space for this type.
|
||||||
|
buffer = Vec::with_capacity(usize::BYTES);
|
||||||
|
|
||||||
assert!(determine_size(num as u64) <= num_bytes && num_bytes <= 8);
|
assert!(determine_size(num as u64) <= num_bytes && num_bytes <= 8);
|
||||||
assert!(buffer.len() >= 1 && buffer.len() <= 8);
|
|
||||||
|
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
bytes = mem::transmute::<usize, [u8; 8]>(num.to_be());
|
bytes = mem::transmute::<usize, [u8; 8]>(num.to_be());
|
||||||
copy_nonoverlapping::<u8>(
|
buffer.extend_from_slice(&bytes);
|
||||||
bytes.as_ptr().offset((8 - num_bytes) as isize),
|
//copy_nonoverlapping::<u8>(
|
||||||
buffer.as_mut_ptr(), num_bytes as usize);
|
// bytes.as_ptr().offset((8 - num_bytes) as isize),
|
||||||
|
// buffer.as_mut_ptr(), num_bytes as usize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bytes_to_string(buffer: &[u8]) -> String
|
fn bytes_to_string(buffer: &[u8]) -> String
|
||||||
@ -204,10 +234,11 @@ impl Converter for BigEndian
|
|||||||
new_string
|
new_string
|
||||||
}
|
}
|
||||||
|
|
||||||
fn string_to_bytes(buffer: &mut [u8], string: String)
|
fn string_to_bytes(string: String) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let bytes: &[u8];
|
let bytes: &[u8];
|
||||||
let byte_count: u64;
|
let byte_count: u64;
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Turn the string into a byte array.
|
// Turn the string into a byte array.
|
||||||
bytes = string.as_bytes();
|
bytes = string.as_bytes();
|
||||||
@ -217,16 +248,16 @@ impl Converter for BigEndian
|
|||||||
byte_count = bytes.len() as u64;
|
byte_count = bytes.len() as u64;
|
||||||
|
|
||||||
// Make sure the buffer has enough space for this string.
|
// Make sure the buffer has enough space for this string.
|
||||||
assert!(buffer.len() as u64 >= byte_count + U64_BYTES as u64);
|
buffer = Vec::with_capacity(bytes.len() + U64_BYTES);
|
||||||
|
|
||||||
// Add the count to the buffer.
|
// Add the count to the buffer.
|
||||||
BigEndian::u64_to_bytes(&mut buffer[0..U64_BYTES], byte_count);
|
buffer.append(&mut BigEndian::u64_to_bytes(byte_count));
|
||||||
|
|
||||||
// Add each byte of the string to the buffer.
|
// Add each byte of the string to the buffer.
|
||||||
for (i, byte) in bytes.iter().enumerate()
|
buffer.extend_from_slice(bytes);
|
||||||
{
|
|
||||||
buffer[U64_BYTES + i] = byte.clone();
|
// Return the byte buffer.
|
||||||
}
|
buffer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,37 +295,74 @@ impl Converter for LittleEndian
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn u16_to_bytes(buffer: &mut [u8], num: u16)
|
fn u16_to_bytes(num: u16) -> Vec<u8>
|
||||||
{
|
{
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(u16::BYTES);
|
||||||
|
|
||||||
|
// Write the bytes to the Vector.
|
||||||
write_bytes!(buffer, u16, U16_BYTES, num, to_le);
|
write_bytes!(buffer, u16, U16_BYTES, num, to_le);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn u32_to_bytes(buffer: &mut [u8], num: u32)
|
fn u32_to_bytes(num: u32) -> Vec<u8>
|
||||||
{
|
{
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(u32::BYTES);
|
||||||
|
|
||||||
|
// Write the bytes to the Vector.
|
||||||
write_bytes!(buffer, u32, U32_BYTES, num, to_le);
|
write_bytes!(buffer, u32, U32_BYTES, num, to_le);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn u64_to_bytes(buffer: &mut [u8], num: u64)
|
fn u64_to_bytes(num: u64) -> Vec<u8>
|
||||||
{
|
{
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(u64::BYTES);
|
||||||
|
|
||||||
|
// Write the bytes to the Vector.
|
||||||
write_bytes!(buffer, u64, U64_BYTES, num, to_le);
|
write_bytes!(buffer, u64, U64_BYTES, num, to_le);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usize_to_bytes(buffer: &mut [u8], num: usize)
|
fn usize_to_bytes(num: usize) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let bytes: [u8; 8];
|
let bytes: [u8; 8];
|
||||||
let num_bytes: u8;
|
let num_bytes: u8;
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
num_bytes = buffer.len() as u8;
|
num_bytes = usize::BYTES as u8;
|
||||||
|
|
||||||
|
// Create a buffer with enough space for this type.
|
||||||
|
buffer = Vec::with_capacity(usize::BYTES);
|
||||||
|
|
||||||
assert!(determine_size(num as u64) <= num_bytes && num_bytes <= 8);
|
assert!(determine_size(num as u64) <= num_bytes && num_bytes <= 8);
|
||||||
assert!(buffer.len() >= 1 && buffer.len() <= 8);
|
|
||||||
|
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
bytes = mem::transmute::<usize, [u8; 8]>(num.to_le());
|
bytes = mem::transmute::<usize, [u8; 8]>(num.to_le());
|
||||||
copy_nonoverlapping::<u8>(bytes.as_ptr(), buffer.as_mut_ptr(),
|
buffer.extend_from_slice(&bytes);
|
||||||
num_bytes as usize);
|
//copy_nonoverlapping::<u8>(bytes.as_ptr(), buffer.as_mut_ptr(),
|
||||||
|
// num_bytes as usize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bytes_to_string(buffer: &[u8]) -> String
|
fn bytes_to_string(buffer: &[u8]) -> String
|
||||||
@ -333,29 +401,30 @@ impl Converter for LittleEndian
|
|||||||
new_string
|
new_string
|
||||||
}
|
}
|
||||||
|
|
||||||
fn string_to_bytes(buffer: &mut [u8], string: String)
|
fn string_to_bytes(string: String) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let bytes: &[u8];
|
let bytes: &[u8];
|
||||||
let byte_count: u64;
|
let byte_count: u64;
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Turn the string into a byte array.
|
// Turn the string into a byte array.
|
||||||
bytes = string.as_bytes();
|
bytes = string.as_bytes();
|
||||||
|
|
||||||
// Determine how many bytes will be written
|
// Determine how many bytes will be written
|
||||||
// for this string.
|
// for this string.
|
||||||
byte_count = bytes.len() as u64 + U64_BYTES as u64;
|
byte_count = bytes.len() as u64;
|
||||||
|
|
||||||
// Make sure the buffer has enough space for this string.
|
// Make sure the buffer has enough space for this string.
|
||||||
assert!(buffer.len() as u64 >= byte_count);
|
buffer = Vec::with_capacity(bytes.len() + U64_BYTES);
|
||||||
|
|
||||||
// Add the count to the buffer.
|
// Add the count to the buffer.
|
||||||
LittleEndian::u64_to_bytes(&mut buffer[0..U64_BYTES], byte_count);
|
buffer.append(&mut LittleEndian::u64_to_bytes(byte_count));
|
||||||
|
|
||||||
// Add each byte of the string to the buffer.
|
// Add each byte of the string to the buffer.
|
||||||
for (i, byte) in bytes.iter().enumerate()
|
buffer.extend_from_slice(bytes);
|
||||||
{
|
|
||||||
buffer[U64_BYTES + i] = byte.clone();
|
// Return the byte buffer.
|
||||||
}
|
buffer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ use sigils::{Zero, Number, Real};
|
|||||||
use sigils::vector::{Vector, Vector2, Vector3, Vector4};
|
use sigils::vector::{Vector, Vector2, Vector3, Vector4};
|
||||||
use sigils::quaternion::Quaternion;
|
use sigils::quaternion::Quaternion;
|
||||||
|
|
||||||
use ::byte_sized::ByteSized;
|
use ::byte_sized::{ByteSized, get_byte_size_of_string};
|
||||||
use ::converter::Converter;
|
use ::converter::Converter;
|
||||||
use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess};
|
use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess};
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ use ::endian::{BigEndian, LittleEndian, PlatformEndian, Endianess};
|
|||||||
pub trait Transmutable
|
pub trait Transmutable
|
||||||
{
|
{
|
||||||
/// Transmute this type to an array of bytes.
|
/// Transmute this type to an array of bytes.
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess);
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>;
|
||||||
|
|
||||||
/// Transmute an array of bytes to this type.
|
/// Transmute an array of bytes to this type.
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Self;
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Self;
|
||||||
@ -30,24 +30,21 @@ macro_rules! handle_endianess_to_bytes
|
|||||||
($buffer: ident, $val: ident, $endianess: ident, $func: ident) =>
|
($buffer: ident, $val: ident, $endianess: ident, $func: ident) =>
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
let mut mut_buffer;
|
|
||||||
|
|
||||||
mut_buffer = $buffer;
|
|
||||||
match $endianess
|
match $endianess
|
||||||
{
|
{
|
||||||
Endianess::BIG =>
|
Endianess::BIG =>
|
||||||
{
|
{
|
||||||
BigEndian::$func(&mut mut_buffer, *$val);
|
$buffer.append(&mut BigEndian::$func(*$val));
|
||||||
}
|
}
|
||||||
|
|
||||||
Endianess::LITTLE =>
|
Endianess::LITTLE =>
|
||||||
{
|
{
|
||||||
LittleEndian::$func(&mut mut_buffer, *$val);
|
$buffer.append(&mut LittleEndian::$func(*$val));
|
||||||
}
|
}
|
||||||
|
|
||||||
Endianess::PLATFORM =>
|
Endianess::PLATFORM =>
|
||||||
{
|
{
|
||||||
PlatformEndian::$func(&mut mut_buffer, *$val);
|
$buffer.append(&mut PlatformEndian::$func(*$val));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,15 +83,19 @@ macro_rules! handle_endianess_from_bytes
|
|||||||
impl Transmutable for u8
|
impl Transmutable for u8
|
||||||
{
|
{
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= u8::BYTES);
|
// Create the Vector to hold the byte.
|
||||||
|
buffer = Vec::with_capacity(u8::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
// Endianess doesn't matter here.
|
// Endianess doesn't matter here.
|
||||||
buffer[0] = *self;
|
buffer.push(*self);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
@ -112,14 +113,19 @@ impl Transmutable for u8
|
|||||||
|
|
||||||
impl Transmutable for u16
|
impl Transmutable for u16
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= u16::BYTES);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(u16::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, u16_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, u16_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u16
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u16
|
||||||
@ -135,14 +141,19 @@ impl Transmutable for u16
|
|||||||
|
|
||||||
impl Transmutable for u32
|
impl Transmutable for u32
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= u32::BYTES);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(u32::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, u32_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, u32_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u32
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u32
|
||||||
@ -158,14 +169,19 @@ impl Transmutable for u32
|
|||||||
|
|
||||||
impl Transmutable for u64
|
impl Transmutable for u64
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= u64::BYTES);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(u64::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, u64_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, u64_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u64
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> u64
|
||||||
@ -181,14 +197,19 @@ impl Transmutable for u64
|
|||||||
|
|
||||||
impl Transmutable for usize
|
impl Transmutable for usize
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= usize::BYTES);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(usize::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, usize_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, usize_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> usize
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> usize
|
||||||
@ -205,14 +226,19 @@ impl Transmutable for usize
|
|||||||
impl Transmutable for i8
|
impl Transmutable for i8
|
||||||
{
|
{
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= i8::BYTES);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(i8::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
buffer[0] = *self as u8;
|
buffer.push(*self as u8);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
@ -230,14 +256,19 @@ impl Transmutable for i8
|
|||||||
|
|
||||||
impl Transmutable for i16
|
impl Transmutable for i16
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= i16::BYTES);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(i16::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, i16_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, i16_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i16
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i16
|
||||||
@ -253,14 +284,19 @@ impl Transmutable for i16
|
|||||||
|
|
||||||
impl Transmutable for i32
|
impl Transmutable for i32
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= i32::BYTES);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(i32::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, i32_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, i32_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i32
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i32
|
||||||
@ -276,14 +312,19 @@ impl Transmutable for i32
|
|||||||
|
|
||||||
impl Transmutable for i64
|
impl Transmutable for i64
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= i64::BYTES);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(i64::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, i64_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, i64_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i64
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> i64
|
||||||
@ -299,14 +340,19 @@ impl Transmutable for i64
|
|||||||
|
|
||||||
impl Transmutable for isize
|
impl Transmutable for isize
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= isize::BYTES);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(isize::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, isize_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, isize_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> isize
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> isize
|
||||||
@ -322,14 +368,19 @@ impl Transmutable for isize
|
|||||||
|
|
||||||
impl Transmutable for f32
|
impl Transmutable for f32
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= 4);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(f32::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, f32_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, f32_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> f32
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> f32
|
||||||
@ -345,14 +396,19 @@ impl Transmutable for f32
|
|||||||
|
|
||||||
impl Transmutable for f64
|
impl Transmutable for f64
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
// Make sure that there is enough space to store
|
let mut buffer: Vec<u8>;
|
||||||
// the bytes from this type.
|
|
||||||
assert!(buffer.len() >= 8);
|
// Create the Vector to hold the bytes
|
||||||
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(f64::BYTES);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
handle_endianess_to_bytes!(buffer, self, endianess, f64_to_bytes);
|
handle_endianess_to_bytes!(buffer, self, endianess, f64_to_bytes);
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> f64
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> f64
|
||||||
@ -368,13 +424,14 @@ impl Transmutable for f64
|
|||||||
|
|
||||||
impl Transmutable for String
|
impl Transmutable for String
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let temp: String;
|
let temp: String;
|
||||||
let mut mut_buffer;
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Create a mutable handle to the buffer.
|
// Create the Vector to hold the bytes
|
||||||
mut_buffer = buffer;
|
// from this type.
|
||||||
|
buffer = Vec::with_capacity(get_byte_size_of_string(self));
|
||||||
|
|
||||||
// Clone the string so that we can use its data.
|
// Clone the string so that we can use its data.
|
||||||
// We have to do this because Strings don't implement Copy.
|
// We have to do this because Strings don't implement Copy.
|
||||||
@ -386,19 +443,22 @@ impl Transmutable for String
|
|||||||
{
|
{
|
||||||
Endianess::BIG =>
|
Endianess::BIG =>
|
||||||
{
|
{
|
||||||
BigEndian::string_to_bytes(&mut mut_buffer, temp);
|
buffer.append(&mut BigEndian::string_to_bytes(temp));
|
||||||
}
|
}
|
||||||
|
|
||||||
Endianess::LITTLE =>
|
Endianess::LITTLE =>
|
||||||
{
|
{
|
||||||
LittleEndian::string_to_bytes(&mut mut_buffer, temp);
|
buffer.append(&mut LittleEndian::string_to_bytes(temp));
|
||||||
}
|
}
|
||||||
|
|
||||||
Endianess::PLATFORM =>
|
Endianess::PLATFORM =>
|
||||||
{
|
{
|
||||||
PlatformEndian::string_to_bytes(&mut mut_buffer, temp);
|
buffer.append(&mut PlatformEndian::string_to_bytes(temp));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> String
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> String
|
||||||
@ -410,10 +470,11 @@ impl Transmutable for String
|
|||||||
|
|
||||||
impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Determine the number of bytes requires to
|
// Determine the number of bytes requires to
|
||||||
// represent a Vector2.
|
// represent a Vector2.
|
||||||
@ -422,11 +483,14 @@ impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
|||||||
|
|
||||||
// Make sure that there is enough space to store
|
// Make sure that there is enough space to store
|
||||||
// the bytes from this type.
|
// the bytes from this type.
|
||||||
assert!(buffer.len() >= num_bytes);
|
buffer = Vec::with_capacity(num_bytes);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
self.x.to_bytes(&mut buffer[0..byte_size], endianess);
|
buffer.append(&mut self.x.to_bytes(endianess));
|
||||||
self.y.to_bytes(&mut buffer[byte_size..num_bytes], endianess);
|
buffer.append(&mut self.y.to_bytes(endianess));
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector2<T>
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector2<T>
|
||||||
@ -454,10 +518,11 @@ impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
|
|||||||
|
|
||||||
impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Determine the number of bytes requires to
|
// Determine the number of bytes requires to
|
||||||
// represent a Vector3.
|
// represent a Vector3.
|
||||||
@ -466,12 +531,15 @@ impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
|||||||
|
|
||||||
// Make sure that there is enough space to store
|
// Make sure that there is enough space to store
|
||||||
// the bytes from this type.
|
// the bytes from this type.
|
||||||
assert!(buffer.len() >= num_bytes);
|
buffer = Vec::with_capacity(num_bytes);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
self.x.to_bytes(&mut buffer[0..byte_size], endianess);
|
buffer.append(&mut self.x.to_bytes(endianess));
|
||||||
self.y.to_bytes(&mut buffer[byte_size..(byte_size*2)], endianess);
|
buffer.append(&mut self.y.to_bytes(endianess));
|
||||||
self.z.to_bytes(&mut buffer[(byte_size*2)..num_bytes], endianess);
|
buffer.append(&mut self.z.to_bytes(endianess));
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector3<T>
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector3<T>
|
||||||
@ -500,10 +568,11 @@ impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
|
|||||||
|
|
||||||
impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Determine the number of bytes requires to
|
// Determine the number of bytes requires to
|
||||||
// represent a Vector4.
|
// represent a Vector4.
|
||||||
@ -512,13 +581,16 @@ impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
|||||||
|
|
||||||
// Make sure that there is enough space to store
|
// Make sure that there is enough space to store
|
||||||
// the bytes from this type.
|
// the bytes from this type.
|
||||||
assert!(buffer.len() >= num_bytes);
|
buffer = Vec::with_capacity(num_bytes);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
self.x.to_bytes(&mut buffer[0..byte_size], endianess);
|
buffer.append(&mut self.x.to_bytes(endianess));
|
||||||
self.y.to_bytes(&mut buffer[byte_size..(byte_size*2)], endianess);
|
buffer.append(&mut self.y.to_bytes(endianess));
|
||||||
self.z.to_bytes(&mut buffer[(byte_size*2)..(byte_size*3)], endianess);
|
buffer.append(&mut self.z.to_bytes(endianess));
|
||||||
self.w.to_bytes(&mut buffer[(byte_size*3)..num_bytes], endianess);
|
buffer.append(&mut self.w.to_bytes(endianess));
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector4<T>
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Vector4<T>
|
||||||
@ -549,10 +621,11 @@ impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
|
|||||||
impl<T> Transmutable for Quaternion<T>
|
impl<T> Transmutable for Quaternion<T>
|
||||||
where T: Real + ByteSized + Transmutable
|
where T: Real + ByteSized + Transmutable
|
||||||
{
|
{
|
||||||
fn to_bytes(&self, buffer: &mut [u8], endianess: Endianess)
|
fn to_bytes(&self, endianess: Endianess) -> Vec<u8>
|
||||||
{
|
{
|
||||||
let byte_size: usize;
|
let byte_size: usize;
|
||||||
let num_bytes: usize;
|
let num_bytes: usize;
|
||||||
|
let mut buffer: Vec<u8>;
|
||||||
|
|
||||||
// Determine the number of bytes requires to
|
// Determine the number of bytes requires to
|
||||||
// represent a Quaternion.
|
// represent a Quaternion.
|
||||||
@ -561,11 +634,14 @@ impl<T> Transmutable for Quaternion<T>
|
|||||||
|
|
||||||
// Make sure that there is enough space to store
|
// Make sure that there is enough space to store
|
||||||
// the bytes from this type.
|
// the bytes from this type.
|
||||||
assert!(buffer.len() >= num_bytes);
|
buffer = Vec::with_capacity(num_bytes);
|
||||||
|
|
||||||
// Convert this to bytes and add it to the buffer.
|
// Convert this to bytes and add it to the buffer.
|
||||||
self.scalar.to_bytes(&mut buffer[0..byte_size], endianess);
|
buffer.append(&mut self.scalar.to_bytes(endianess));
|
||||||
self.vector.to_bytes(&mut buffer[byte_size..num_bytes], endianess);
|
buffer.append(&mut self.vector.to_bytes(endianess));
|
||||||
|
|
||||||
|
// Return the byte buffer.
|
||||||
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Quaternion<T>
|
fn from_bytes(buffer: &[u8], endianess: Endianess) -> Quaternion<T>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user