2015-11-25 02:53:35 +01:00
|
|
|
//! To/From Bytes conversation for basic types
|
|
|
|
//!
|
|
|
|
//! Types implementing `ToBytes` and `FromBytes` traits
|
|
|
|
//! can be easily converted to and from bytes
|
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use std::error::Error as StdError;
|
2015-11-26 15:00:17 +01:00
|
|
|
use uint::{U128, U256};
|
2015-11-25 02:53:35 +01:00
|
|
|
|
2015-11-27 17:54:33 +01:00
|
|
|
pub trait BytesConvertable {
|
|
|
|
fn bytes(&self) -> &[u8];
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BytesConvertable for &'a [u8] {
|
|
|
|
fn bytes(&self) -> &[u8] { self }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BytesConvertable for Vec<u8> {
|
|
|
|
fn bytes(&self) -> &[u8] { self }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bytes_convertable() {
|
|
|
|
assert_eq!(vec![0x12u8, 0x34].bytes(), &[0x12u8, 0x34]);
|
|
|
|
}
|
|
|
|
|
2015-11-25 02:53:35 +01:00
|
|
|
/// TODO: optimise some conversations
|
|
|
|
pub trait ToBytes {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn to_bytes(&self) -> Vec<u8>;
|
|
|
|
fn to_bytes_len(&self) -> usize { self.to_bytes().len() }
|
|
|
|
fn first_byte(&self) -> Option<u8> { self.to_bytes().first().map(|&x| { x })}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a> ToBytes for &'a str {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
From::from(*self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes_len(&self) -> usize { self.len() }
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ToBytes for String {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
let s: &str = self.as_ref();
|
|
|
|
From::from(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes_len(&self) -> usize { self.len() }
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ToBytes for u8 {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
match *self {
|
|
|
|
0 => vec![],
|
|
|
|
_ => vec![*self]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes_len(&self) -> usize {
|
|
|
|
match *self {
|
|
|
|
0 => 0,
|
|
|
|
_ => 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn first_byte(&self) -> Option<u8> {
|
|
|
|
match *self {
|
|
|
|
0 => None,
|
|
|
|
_ => Some(*self)
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ToBytes for u64 {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
let mut res= vec![];
|
|
|
|
let count = self.to_bytes_len();
|
|
|
|
res.reserve(count);
|
|
|
|
for i in 0..count {
|
|
|
|
let j = count - 1 - i;
|
|
|
|
res.push((*self >> (j * 8)) as u8);
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
|
2015-11-27 17:54:33 +01:00
|
|
|
fn to_bytes_len(&self) -> usize { 8 - self.leading_zeros() as usize / 8 }
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_map_to_bytes {
|
2015-11-27 17:54:33 +01:00
|
|
|
($from: ident, $to: ty) => {
|
|
|
|
impl ToBytes for $from {
|
|
|
|
fn to_bytes(&self) -> Vec<u8> { (*self as $to).to_bytes() }
|
|
|
|
fn to_bytes_len(&self) -> usize { (*self as $to).to_bytes_len() }
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_map_to_bytes!(usize, u64);
|
|
|
|
impl_map_to_bytes!(u16, u64);
|
|
|
|
impl_map_to_bytes!(u32, u64);
|
|
|
|
|
2015-11-26 15:00:17 +01:00
|
|
|
macro_rules! impl_uint_to_bytes {
|
2015-11-27 17:54:33 +01:00
|
|
|
($name: ident) => {
|
|
|
|
impl ToBytes for $name {
|
|
|
|
fn to_bytes(&self) -> Vec<u8> {
|
|
|
|
let mut res= vec![];
|
|
|
|
let count = self.to_bytes_len();
|
|
|
|
res.reserve(count);
|
|
|
|
for i in 0..count {
|
|
|
|
let j = count - 1 - i;
|
|
|
|
res.push(self.byte(j));
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
fn to_bytes_len(&self) -> usize { (self.bits() + 7) / 8 }
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 15:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_uint_to_bytes!(U256);
|
|
|
|
impl_uint_to_bytes!(U128);
|
|
|
|
|
2015-11-25 02:53:35 +01:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum FromBytesError {
|
2015-11-27 17:54:33 +01:00
|
|
|
UnexpectedEnd
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StdError for FromBytesError {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn description(&self) -> &str { "from_bytes error" }
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for FromBytesError {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(&self, f)
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type FromBytesResult<T> = Result<T, FromBytesError>;
|
|
|
|
|
|
|
|
/// implements "Sized", so the compiler can deducate the size
|
|
|
|
/// of the return type
|
2015-11-26 15:00:17 +01:00
|
|
|
/// TODO: check size of bytes before conversation and return appropriate error
|
2015-11-25 02:53:35 +01:00
|
|
|
pub trait FromBytes: Sized {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn from_bytes(bytes: &[u8]) -> FromBytesResult<Self>;
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromBytes for String {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn from_bytes(bytes: &[u8]) -> FromBytesResult<String> {
|
|
|
|
Ok(::std::str::from_utf8(bytes).unwrap().to_string())
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromBytes for u8 {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn from_bytes(bytes: &[u8]) -> FromBytesResult<u8> {
|
|
|
|
match bytes.len() {
|
|
|
|
0 => Ok(0),
|
|
|
|
_ => Ok(bytes[0])
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromBytes for u64 {
|
2015-11-27 17:54:33 +01:00
|
|
|
fn from_bytes(bytes: &[u8]) -> FromBytesResult<u64> {
|
|
|
|
match bytes.len() {
|
|
|
|
0 => Ok(0),
|
|
|
|
l => {
|
|
|
|
let mut res = 0u64;
|
|
|
|
for i in 0..l {
|
|
|
|
let shift = (l - 1 - i) * 8;
|
|
|
|
res = res + ((bytes[i] as u64) << shift);
|
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_map_from_bytes {
|
2015-11-27 17:54:33 +01:00
|
|
|
($from: ident, $to: ident) => {
|
|
|
|
impl FromBytes for $from {
|
|
|
|
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$from> {
|
|
|
|
$to::from_bytes(bytes).map(| x | { x as $from })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_map_from_bytes!(usize, u64);
|
|
|
|
impl_map_from_bytes!(u16, u64);
|
|
|
|
impl_map_from_bytes!(u32, u64);
|
2015-11-26 15:00:17 +01:00
|
|
|
|
|
|
|
macro_rules! impl_uint_from_bytes {
|
2015-11-27 17:54:33 +01:00
|
|
|
($name: ident) => {
|
|
|
|
impl FromBytes for $name {
|
|
|
|
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> {
|
|
|
|
Ok($name::from(bytes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 15:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_uint_from_bytes!(U256);
|
|
|
|
impl_uint_from_bytes!(U128);
|