Fixed the library to use the new Rust 2018 module system.

This commit is contained in:
Myrddin Dundragon 2019-01-20 01:08:14 -05:00
parent 0f9b3b56c5
commit 3e4094a50c
5 changed files with 53 additions and 49 deletions

View File

@ -7,6 +7,7 @@ license = ""
repository = "https://gitlab.com/CyberMages/Core/alchemy.git" repository = "https://gitlab.com/CyberMages/Core/alchemy.git"
documentation = "" documentation = ""
keywords = ["converter", "binary"] keywords = ["converter", "binary"]
edition = "2018"
[features] [features]

View File

@ -1,9 +1,11 @@
use std::mem; use std::mem;
use weave::attempt;
use spellbook::components::Array; use spellbook::components::Array;
use ::byte_sized::ByteSized; use crate::byte_sized::ByteSized;
use ::conversion_error::ConversionError; use crate::conversion_error::ConversionError;
/// Describes types that can convert Numbers into bytes, /// Describes types that can convert Numbers into bytes,
@ -17,7 +19,7 @@ pub trait Converter
/// enough information to convert. /// enough information to convert.
fn bytes_to_i16(buffer: &[u8]) -> Result<i16, ConversionError> fn bytes_to_i16(buffer: &[u8]) -> Result<i16, ConversionError>
{ {
Ok(try!(Self::bytes_to_u16(buffer)) as i16) Ok(attempt!(Self::bytes_to_u16(buffer)) as i16)
} }
/// Converts an array of bytes to a signed 32-bit integer. /// Converts an array of bytes to a signed 32-bit integer.
@ -27,7 +29,7 @@ pub trait Converter
/// enough information to convert. /// enough information to convert.
fn bytes_to_i32(buffer: &[u8]) -> Result<i32, ConversionError> fn bytes_to_i32(buffer: &[u8]) -> Result<i32, ConversionError>
{ {
Ok(try!(Self::bytes_to_u32(buffer)) as i32) Ok(attempt!(Self::bytes_to_u32(buffer)) as i32)
} }
/// Converts an array of bytes to a signed 64-bit integer. /// Converts an array of bytes to a signed 64-bit integer.
@ -37,7 +39,7 @@ pub trait Converter
/// enough information to convert. /// enough information to convert.
fn bytes_to_i64(buffer: &[u8]) -> Result<i64, ConversionError> fn bytes_to_i64(buffer: &[u8]) -> Result<i64, ConversionError>
{ {
Ok(try!(Self::bytes_to_u64(buffer)) as i64) Ok(attempt!(Self::bytes_to_u64(buffer)) as i64)
} }
/// Converts an array of bytes to a signed integer. /// Converts an array of bytes to a signed integer.
@ -54,7 +56,7 @@ pub trait Converter
check_length!(buffer, isize::BYTES, u64::BYTES); check_length!(buffer, isize::BYTES, u64::BYTES);
temp_num = try!(Self::bytes_to_usize(buffer)) as u64; temp_num = attempt!(Self::bytes_to_usize(buffer)) as u64;
Ok(add_sign(temp_num, buffer.len() as u8) as isize) Ok(add_sign(temp_num, buffer.len() as u8) as isize)
} }
@ -67,7 +69,7 @@ pub trait Converter
{ {
unsafe unsafe
{ {
Ok(mem::transmute::<u32, f32>(try!(Self::bytes_to_u32(buffer)))) Ok(mem::transmute::<u32, f32>(attempt!(Self::bytes_to_u32(buffer))))
} }
} }
@ -80,7 +82,7 @@ pub trait Converter
{ {
unsafe unsafe
{ {
Ok(mem::transmute::<u64, f64>(try!(Self::bytes_to_u64(buffer)))) Ok(mem::transmute::<u64, f64>(attempt!(Self::bytes_to_u64(buffer))))
} }
} }

View File

@ -1,10 +1,14 @@
use scribe::*;
use weave::attempt;
use spellbook::components::Array; use spellbook::components::Array;
use ::byte_sized::ByteSized; use crate::byte_sized::ByteSized;
use ::byte_sized::U64_BYTES; use crate::byte_sized::U64_BYTES;
use ::conversion_error::ConversionError; use crate::conversion_error::ConversionError;
use ::converter::Converter; use crate::converter::Converter;
use ::transmutable::Transmutable; use crate::transmutable::Transmutable;
@ -166,7 +170,7 @@ impl Converter for BigEndian
// Strings start with the size of bytes to read as // Strings start with the size of bytes to read as
// a u64. So read that in and then we know how many // a u64. So read that in and then we know how many
// bytes make up the string. // bytes make up the string.
byte_count = try!(BigEndian::bytes_to_u64(&buffer[0..U64_BYTES])); byte_count = attempt!(BigEndian::bytes_to_u64(&buffer[0..U64_BYTES]));
if byte_count > 0 if byte_count > 0
{ {
@ -275,7 +279,7 @@ impl Converter for LittleEndian
// Strings start with the size of bytes to read as // Strings start with the size of bytes to read as
// a u64. So read that in and then we know how many // a u64. So read that in and then we know how many
// bytes make up the string. // bytes make up the string.
byte_count = try!(LittleEndian::bytes_to_u64(&buffer[0..U64_BYTES])); byte_count = attempt!(LittleEndian::bytes_to_u64(&buffer[0..U64_BYTES]));
if byte_count > 0 if byte_count > 0
{ {
@ -383,7 +387,7 @@ pub fn network_to_platform_order<T>(val: T) -> Result<T, ConversionError>
} }
else else
{ {
converted_val = try!(T::from_endian_bytes(val.as_bytes().as_slice(), converted_val = attempt!(T::from_endian_bytes(val.as_bytes().as_slice(),
Endianess::Network)); Endianess::Network));
} }
@ -405,7 +409,7 @@ pub fn platform_to_network_order<T>(val: T) -> Result<T, ConversionError>
} }
else else
{ {
converted_val = try!(T::from_endian_bytes(val.as_bytes().as_slice(), converted_val = attempt!(T::from_endian_bytes(val.as_bytes().as_slice(),
Endianess::Network)); Endianess::Network));
} }

View File

@ -8,16 +8,13 @@
#[macro_use]
extern crate scribe; extern crate scribe;
extern crate weave; extern crate weave;
extern crate spellbook;
#[cfg(feature="sigils")] #[cfg(feature="sigils")]
extern crate sigils; extern crate sigils;
extern crate spellbook;
#[macro_use] #[macro_use]
@ -31,15 +28,15 @@ mod transmutable;
pub use ::byte_sized::ByteSized; pub use crate::byte_sized::ByteSized;
pub use ::byte_sized::{U8_BYTES, U16_BYTES, U32_BYTES, U64_BYTES, USIZE_BYTES}; pub use crate::byte_sized::{U8_BYTES, U16_BYTES, U32_BYTES, U64_BYTES, USIZE_BYTES};
pub use ::byte_sized::{I8_BYTES, I16_BYTES, I32_BYTES, I64_BYTES, ISIZE_BYTES}; pub use crate::byte_sized::{I8_BYTES, I16_BYTES, I32_BYTES, I64_BYTES, ISIZE_BYTES};
pub use ::byte_sized::{F32_BYTES, F64_BYTES}; pub use crate::byte_sized::{F32_BYTES, F64_BYTES};
pub use ::byte_sized::get_byte_size_of_string; pub use crate::byte_sized::get_byte_size_of_string;
pub use ::conversion_error::ConversionError; pub use crate::conversion_error::ConversionError;
pub use ::converter::Converter; pub use crate::converter::Converter;
pub use ::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian}; pub use crate::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian};
pub use ::endian::Endianess; pub use crate::endian::Endianess;
pub use ::endian::{network_to_platform_order, platform_to_network_order}; pub use crate::endian::{network_to_platform_order, platform_to_network_order};
pub use ::endian::{get_network_endianess, get_platform_endianess}; pub use crate::endian::{get_network_endianess, get_platform_endianess};
pub use ::transmutable::Transmutable; pub use crate::transmutable::Transmutable;

View File

@ -9,11 +9,11 @@ use sigils::vector::{Vector, Vector2, Vector3, Vector4};
#[cfg(feature="convert_sigils")] #[cfg(feature="convert_sigils")]
use sigils::quaternion::Quaternion; use sigils::quaternion::Quaternion;
use ::byte_sized::{ByteSized, get_byte_size_of_string}; use crate::byte_sized::{ByteSized, get_byte_size_of_string};
use ::conversion_error::ConversionError; use crate::conversion_error::ConversionError;
use ::converter::Converter; use crate::converter::Converter;
use ::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian}; use crate::endian::{BigEndian, LittleEndian, PlatformEndian, NetworkEndian};
use ::endian::Endianess; use crate::endian::Endianess;
@ -465,9 +465,9 @@ impl<T> Transmutable for Vector2<T> where T: Number + ByteSized + Transmutable
check_length!(buffer >= num_bytes); check_length!(buffer >= num_bytes);
// Convert the given bytes to this type and return it. // Convert the given bytes to this type and return it.
vec.x = try!(T::from_endian_bytes(&buffer[0..T::BYTES], vec.x = attempt!(T::from_endian_bytes(&buffer[0..T::BYTES],
endianess)); endianess));
vec.y = try!(T::from_endian_bytes(&buffer[T::BYTES..num_bytes], vec.y = attempt!(T::from_endian_bytes(&buffer[T::BYTES..num_bytes],
endianess)); endianess));
Ok(vec) Ok(vec)
} }
@ -516,11 +516,11 @@ impl<T> Transmutable for Vector3<T> where T: Number + ByteSized + Transmutable
check_length!(buffer >= num_bytes); check_length!(buffer >= num_bytes);
// Convert the given bytes to this type and return it. // Convert the given bytes to this type and return it.
vec.x = try!(T::from_endian_bytes(&buffer[0..T::BYTES], vec.x = attempt!(T::from_endian_bytes(&buffer[0..T::BYTES],
endianess)); endianess));
vec.y = try!(T::from_endian_bytes(&buffer[T::BYTES..(T::BYTES * 2)], vec.y = attempt!(T::from_endian_bytes(&buffer[T::BYTES..(T::BYTES * 2)],
endianess)); endianess));
vec.z = try!(T::from_endian_bytes(&buffer[(T::BYTES * 2)..num_bytes], vec.z = attempt!(T::from_endian_bytes(&buffer[(T::BYTES * 2)..num_bytes],
endianess)); endianess));
Ok(vec) Ok(vec)
} }
@ -570,13 +570,13 @@ impl<T> Transmutable for Vector4<T> where T: Number + ByteSized + Transmutable
check_length!(buffer >= num_bytes); check_length!(buffer >= num_bytes);
// Convert the given bytes to this type and return it. // Convert the given bytes to this type and return it.
vec.x = try!(T::from_endian_bytes(&buffer[0..T::BYTES], vec.x = attempt!(T::from_endian_bytes(&buffer[0..T::BYTES],
endianess)); endianess));
vec.y = try!(T::from_endian_bytes(&buffer[T::BYTES..(T::BYTES * 2)], vec.y = attempt!(T::from_endian_bytes(&buffer[T::BYTES..(T::BYTES * 2)],
endianess)); endianess));
vec.z = try!(T::from_endian_bytes(&buffer[(T::BYTES*2)..(T::BYTES*3)], vec.z = attempt!(T::from_endian_bytes(&buffer[(T::BYTES*2)..(T::BYTES*3)],
endianess)); endianess));
vec.w = try!(T::from_endian_bytes(&buffer[(T::BYTES * 3)..num_bytes], vec.w = attempt!(T::from_endian_bytes(&buffer[(T::BYTES * 3)..num_bytes],
endianess)); endianess));
Ok(vec) Ok(vec)
} }
@ -630,10 +630,10 @@ impl<T> Transmutable for Quaternion<T>
check_length!(buffer >= num_bytes); check_length!(buffer >= num_bytes);
// Convert the given bytes to this type and return it. // Convert the given bytes to this type and return it.
quat.scalar = try!(T::from_endian_bytes(&buffer[0..T::BYTES], quat.scalar = attempt!(T::from_endian_bytes(&buffer[0..T::BYTES],
endianess)); endianess));
quat.vector = quat.vector =
try!(Vector3::<T>::from_endian_bytes(&buffer[T::BYTES..num_bytes], attempt!(Vector3::<T>::from_endian_bytes(&buffer[T::BYTES..num_bytes],
endianess)); endianess));
Ok(quat) Ok(quat)
} }