Merge branch 'master' of gitlab.com:CyberMages/Core/alchemy

Conflicts:
	Cargo.toml
	src/converter.rs
	src/transmutable.rs

Merged the code duplication removal from transmutable into the changes
made to handle the conversion in a safer fashion.
This commit is contained in:
Myrddin Dundragon 2018-01-05 19:35:40 -05:00
commit 8f006edf09
2 changed files with 18 additions and 345 deletions

View File

@ -12,11 +12,12 @@
extern crate scribe;
extern crate weave;
extern crate spellbook;
#[cfg(feature="sigils")]
extern crate sigils;
extern crate spellbook;
#[macro_use]

View File

@ -24,7 +24,10 @@ pub trait Transmutable: Sized
{
/// Transmute an array of bytes in the
/// platform's endian to this type.
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>;
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
/// Transmute an array of bytes in the
/// given endian to this type.
@ -34,16 +37,25 @@ pub trait Transmutable: Sized
/// Transmute this type to an array of bytes in
/// the Platform's endian.
fn to_bytes(self) -> Array<u8>;
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
/// Transmute this type to an array of bytes in
/// the desired endian.
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>;
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
/// Transmute this type to an array of bytes in
/// the Platform's endian.
fn as_bytes(&self) -> Array<u8>;
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
/// Transmute this type to an array of bytes in
/// the desired endian.
@ -126,11 +138,6 @@ macro_rules! handle_endianess_from_bytes
impl Transmutable for u8
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
#[allow(unused_variables)]
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
@ -141,21 +148,6 @@ impl Transmutable for u8
Ok(buffer[0])
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
#[allow(unused_variables)]
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
@ -171,11 +163,6 @@ impl Transmutable for u8
impl Transmutable for u16
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -183,21 +170,6 @@ impl Transmutable for u16
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u16)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Return the Endianess conversion.
@ -212,11 +184,6 @@ impl Transmutable for u16
impl Transmutable for u32
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -224,21 +191,6 @@ impl Transmutable for u32
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u32)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -253,11 +205,6 @@ impl Transmutable for u32
impl Transmutable for u64
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -265,21 +212,6 @@ impl Transmutable for u64
handle_endianess_from_bytes!(buffer, endianess, bytes_to_u64)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -294,11 +226,6 @@ impl Transmutable for u64
impl Transmutable for usize
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -306,21 +233,6 @@ impl Transmutable for usize
handle_endianess_from_bytes!(buffer, endianess, bytes_to_usize)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -335,11 +247,6 @@ impl Transmutable for usize
impl Transmutable for i8
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
#[allow(unused_variables)]
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
@ -349,21 +256,6 @@ impl Transmutable for i8
Ok(buffer[0] as i8)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
#[allow(unused_variables)]
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
@ -378,11 +270,6 @@ impl Transmutable for i8
impl Transmutable for i16
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -390,21 +277,6 @@ impl Transmutable for i16
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i16)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -419,11 +291,6 @@ impl Transmutable for i16
impl Transmutable for i32
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -431,21 +298,6 @@ impl Transmutable for i32
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i32)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -460,11 +312,6 @@ impl Transmutable for i32
impl Transmutable for i64
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -472,21 +319,6 @@ impl Transmutable for i64
handle_endianess_from_bytes!(buffer, endianess, bytes_to_i64)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -501,11 +333,6 @@ impl Transmutable for i64
impl Transmutable for isize
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -513,21 +340,6 @@ impl Transmutable for isize
handle_endianess_from_bytes!(buffer, endianess, bytes_to_isize)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -542,11 +354,6 @@ impl Transmutable for isize
impl Transmutable for f32
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -554,21 +361,6 @@ impl Transmutable for f32
handle_endianess_from_bytes!(buffer, endianess, bytes_to_f32)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -583,11 +375,6 @@ impl Transmutable for f32
impl Transmutable for f64
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -595,21 +382,6 @@ impl Transmutable for f64
handle_endianess_from_bytes!(buffer, endianess, bytes_to_f64)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -624,11 +396,6 @@ impl Transmutable for f64
impl Transmutable for String
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -636,21 +403,6 @@ impl Transmutable for String
handle_endianess_from_bytes!(buffer, endianess, bytes_to_string)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
// Convert this to bytes and add it to the buffer.
@ -688,11 +440,6 @@ impl Transmutable for String
#[cfg(feature="convert_sigils")]
impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -716,21 +463,6 @@ impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
Ok(vec)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
let num_bytes: usize;
@ -761,11 +493,6 @@ impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
#[cfg(feature="convert_sigils")]
impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -789,21 +516,6 @@ impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
Ok(vec)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
let num_bytes: usize;
@ -835,11 +547,6 @@ impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
#[cfg(feature="convert_sigils")]
impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -865,21 +572,6 @@ impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
Ok(vec)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
let num_bytes: usize;
@ -913,11 +605,6 @@ impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
impl<T> Transmutable for Quaternion<T>
where T: Real + Trig + ByteSized + Transmutable
{
fn from_bytes(buffer: &[u8]) -> Result<Self, ConversionError>
{
Self::from_endian_bytes(buffer, Endianess::Platform)
}
fn from_endian_bytes(buffer: &[u8], endianess: Endianess)
-> Result<Self, ConversionError>
{
@ -942,21 +629,6 @@ impl<T> Transmutable for Quaternion<T>
Ok(quat)
}
fn to_bytes(self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn to_endian_bytes(self, endianess: Endianess) -> Array<u8>
{
self.as_endian_bytes(endianess)
}
fn as_bytes(&self) -> Array<u8>
{
self.as_endian_bytes(Endianess::Platform)
}
fn as_endian_bytes(&self, endianess: Endianess) -> Array<u8>
{
let num_bytes: usize;