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:
149
src/endian.rs
149
src/endian.rs
@ -77,20 +77,13 @@ macro_rules! write_bytes
|
||||
($buffer: expr, $valueType: ident, $numBytes: expr,
|
||||
$num: expr, $convertFunc: ident) =>
|
||||
({
|
||||
assert!($buffer.len() >= $valueType::BYTES,
|
||||
"Not enough room in the buffer to write to.");
|
||||
unsafe
|
||||
{
|
||||
let size: usize;
|
||||
let bytes: [u8; $numBytes];
|
||||
|
||||
size = $valueType::BYTES;
|
||||
|
||||
bytes =
|
||||
mem::transmute::<_, [u8; $numBytes]>($num.$convertFunc());
|
||||
copy_nonoverlapping::<u8>((&bytes).as_ptr(),
|
||||
$buffer.as_mut_ptr(),
|
||||
size);
|
||||
$buffer.extend_from_slice(&bytes);
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -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);
|
||||
|
||||
// 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);
|
||||
|
||||
// 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);
|
||||
|
||||
// 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 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!(buffer.len() >= 1 && buffer.len() <= 8);
|
||||
|
||||
unsafe
|
||||
{
|
||||
bytes = mem::transmute::<usize, [u8; 8]>(num.to_be());
|
||||
copy_nonoverlapping::<u8>(
|
||||
bytes.as_ptr().offset((8 - num_bytes) as isize),
|
||||
buffer.as_mut_ptr(), num_bytes as usize);
|
||||
buffer.extend_from_slice(&bytes);
|
||||
//copy_nonoverlapping::<u8>(
|
||||
// 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
|
||||
@ -204,10 +234,11 @@ impl Converter for BigEndian
|
||||
new_string
|
||||
}
|
||||
|
||||
fn string_to_bytes(buffer: &mut [u8], string: String)
|
||||
fn string_to_bytes(string: String) -> Vec<u8>
|
||||
{
|
||||
let bytes: &[u8];
|
||||
let byte_count: u64;
|
||||
let mut buffer: Vec<u8>;
|
||||
|
||||
// Turn the string into a byte array.
|
||||
bytes = string.as_bytes();
|
||||
@ -217,16 +248,16 @@ impl Converter for BigEndian
|
||||
byte_count = bytes.len() as u64;
|
||||
|
||||
// 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.
|
||||
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.
|
||||
for (i, byte) in bytes.iter().enumerate()
|
||||
{
|
||||
buffer[U64_BYTES + i] = byte.clone();
|
||||
}
|
||||
buffer.extend_from_slice(bytes);
|
||||
|
||||
// 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);
|
||||
|
||||
// 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);
|
||||
|
||||
// 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);
|
||||
|
||||
// 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 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!(buffer.len() >= 1 && buffer.len() <= 8);
|
||||
|
||||
unsafe
|
||||
{
|
||||
bytes = mem::transmute::<usize, [u8; 8]>(num.to_le());
|
||||
copy_nonoverlapping::<u8>(bytes.as_ptr(), buffer.as_mut_ptr(),
|
||||
num_bytes as usize);
|
||||
buffer.extend_from_slice(&bytes);
|
||||
//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
|
||||
@ -333,29 +401,30 @@ impl Converter for LittleEndian
|
||||
new_string
|
||||
}
|
||||
|
||||
fn string_to_bytes(buffer: &mut [u8], string: String)
|
||||
fn string_to_bytes(string: String) -> Vec<u8>
|
||||
{
|
||||
let bytes: &[u8];
|
||||
let byte_count: u64;
|
||||
let mut buffer: Vec<u8>;
|
||||
|
||||
// Turn the string into a byte array.
|
||||
bytes = string.as_bytes();
|
||||
|
||||
// Determine how many bytes will be written
|
||||
// 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.
|
||||
assert!(buffer.len() as u64 >= byte_count);
|
||||
buffer = Vec::with_capacity(bytes.len() + U64_BYTES);
|
||||
|
||||
// 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.
|
||||
for (i, byte) in bytes.iter().enumerate()
|
||||
{
|
||||
buffer[U64_BYTES + i] = byte.clone();
|
||||
}
|
||||
buffer.extend_from_slice(bytes);
|
||||
|
||||
// Return the byte buffer.
|
||||
buffer
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user