The conversion of an Array of bytes to a value can now fail gracefully.

This was added so that parsers can detect problems converting
a value and fail in an expected way.
This commit is contained in:
2018-01-05 19:20:02 -05:00
parent 3a1b847c51
commit ff3558ea37
12 changed files with 505 additions and 396 deletions

View File

@ -1,7 +1,9 @@
extern crate weave;
extern crate alchemy;
use weave::Error;
use alchemy::{Converter, Endianess, PlatformEndian, Transmutable};
@ -9,7 +11,6 @@ use alchemy::{Converter, Endianess, PlatformEndian, Transmutable};
fn use_converter()
{
let num: f32;
let final_num: f32;
let mut buffer: Vec<u8>;
// Initialize the variables.
@ -25,14 +26,23 @@ fn use_converter()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a floating point number.
final_num = PlatformEndian::bytes_to_f32(buffer.as_slice());
println!("The buffer converts back to: {}", final_num);
match PlatformEndian::bytes_to_f32(buffer.as_slice())
{
Ok(final_num) =>
{
println!("The buffer converts back to: {}", final_num);
}
Err(error) =>
{
println!("{}\n{}", error, error.get_description());
}
}
}
pub fn use_transmutable()
{
let num: f32;
let final_num: f32;
let endianess: Endianess;
let mut buffer: Vec<u8>;
@ -50,8 +60,18 @@ pub fn use_transmutable()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a floating point number.
final_num = f32::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_num);
match f32::from_endian_bytes(buffer.as_slice(), endianess)
{
Ok(final_num) =>
{
println!("The buffer converts back to: {}", final_num);
}
Err(error) =>
{
println!("{}\n{}", error, error.get_description());
}
}
}
/// This just help pretty up the printing of an array of bytes.

View File

@ -1,10 +1,16 @@
//! Run this example with --feature convert_sigils
extern crate weave;
extern crate alchemy;
extern crate sigils;
use weave::Error;
use alchemy::F64_BYTES;
use alchemy::{Endianess, Transmutable};
use sigils::quaternion::Quaternion;
@ -58,7 +64,6 @@ fn stringify_array(buffer: &[u8]) -> String
pub fn main()
{
let quat: Quaternion<f64>;
let final_quat: Quaternion<f64>;
let endianess: Endianess;
let mut buffer: Vec<u8>;
@ -79,6 +84,16 @@ pub fn main()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a Vector2.
final_quat = Quaternion::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_quat);
match Quaternion::<f64>::from_endian_bytes(buffer.as_slice(), endianess)
{
Ok(final_quat) =>
{
println!("The buffer converts back to: {}", final_quat);
}
Err(error) =>
{
println!("{}\n{}", error, error.get_description());
}
}
}

View File

@ -1,6 +1,11 @@
extern crate weave;
extern crate alchemy;
use weave::Error;
use alchemy::{Converter, Endianess, PlatformEndian, Transmutable};
use alchemy::platform_to_network_order;
@ -9,7 +14,6 @@ use alchemy::platform_to_network_order;
fn use_converter()
{
let num: u16;
let final_num: u16;
let mut buffer: Vec<u8>;
// Initialize the variables.
@ -25,14 +29,23 @@ fn use_converter()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a short number.
final_num = PlatformEndian::bytes_to_u16(buffer.as_slice());
println!("The buffer converts back to: {}", final_num);
match PlatformEndian::bytes_to_u16(buffer.as_slice())
{
Ok(final_num) =>
{
println!("The buffer converts back to: {}", final_num);
}
Err(error) =>
{
println!("{}\n{}", error, error.get_description());
}
}
}
pub fn use_transmutable()
{
let num: u16;
let final_num: u16;
let endianess: Endianess;
let mut buffer: Vec<u8>;
@ -50,8 +63,19 @@ pub fn use_transmutable()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a short number.
final_num = u16::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_num);
match u16::from_endian_bytes(buffer.as_slice(), endianess)
{
Ok(final_num) =>
{
println!("The buffer converts back to: {}", final_num);
}
Err(error) =>
{
println!("{}\n{}", error, error.get_description());
}
}
}
/// This just help pretty up the printing of an array of bytes.
@ -105,6 +129,9 @@ pub fn main()
println!("");
println!("This turns into a network value of: {}",
platform_to_network_order(32832u16));
match platform_to_network_order(32832u16)
{
Ok(val) => { println!("This turns into a network value of: {}", val); }
Err(error) => { println!("{}\n{}", error, error.get_description()); }
}
}

View File

@ -1,10 +1,14 @@
extern crate weave;
extern crate alchemy;
extern crate sigils;
use weave::Error;
use alchemy::U64_BYTES;
use alchemy::{Endianess, Transmutable};
use sigils::vector::Vector2;
@ -58,7 +62,6 @@ fn stringify_array(buffer: &[u8]) -> String
pub fn main()
{
let vec: Vector2<u64>;
let final_vec: Vector2<u64>;
let endianess: Endianess;
let mut buffer: Vec<u8>;
@ -79,6 +82,16 @@ pub fn main()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a Vector2.
final_vec = Vector2::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_vec);
match Vector2::<u64>::from_endian_bytes(buffer.as_slice(), endianess)
{
Ok(final_vec) =>
{
println!("The buffer converts back to: {}", final_vec);
}
Err(error) =>
{
println!("{}\n{}", error, error.get_description());
}
}
}