2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-09-01 12:23:31 +02:00
|
|
|
//! General bytes-related utilities.
|
2015-12-26 15:48:41 +01:00
|
|
|
//!
|
2016-09-01 12:23:31 +02:00
|
|
|
//! Includes a pretty-printer for bytes, in the form of `ToPretty` and `PrettySlice`
|
|
|
|
//! as
|
2015-11-25 02:53:35 +01:00
|
|
|
|
|
|
|
use std::fmt;
|
2016-01-13 15:14:24 +01:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2016-01-27 12:14:57 +01:00
|
|
|
|
2016-08-03 16:31:13 +02:00
|
|
|
/// Slice pretty print helper
|
2015-12-01 18:25:18 +01:00
|
|
|
pub struct PrettySlice<'a> (&'a [u8]);
|
|
|
|
|
|
|
|
impl<'a> fmt::Debug for PrettySlice<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
for i in 0..self.0.len() {
|
2015-12-01 19:09:48 +01:00
|
|
|
match i > 0 {
|
|
|
|
true => { try!(write!(f, "·{:02x}", self.0[i])); },
|
|
|
|
false => { try!(write!(f, "{:02x}", self.0[i])); },
|
|
|
|
}
|
2015-12-01 18:25:18 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 12:43:50 +01:00
|
|
|
impl<'a> fmt::Display for PrettySlice<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
for i in 0..self.0.len() {
|
|
|
|
try!(write!(f, "{:02x}", self.0[i]));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Trait to allow a type to be pretty-printed in `format!`, where unoverridable
|
|
|
|
/// defaults cannot otherwise be avoided.
|
2015-12-01 18:25:18 +01:00
|
|
|
pub trait ToPretty {
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Convert a type into a derivative form in order to make `format!` print it prettily.
|
2015-12-01 18:25:18 +01:00
|
|
|
fn pretty(&self) -> PrettySlice;
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Express the object as a hex string.
|
2015-12-17 12:43:50 +01:00
|
|
|
fn to_hex(&self) -> String {
|
|
|
|
format!("{}", self.pretty())
|
|
|
|
}
|
2015-12-01 18:25:18 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 12:23:31 +02:00
|
|
|
impl<T: AsRef<[u8]>> ToPretty for T {
|
2015-12-01 18:25:18 +01:00
|
|
|
fn pretty(&self) -> PrettySlice {
|
2016-09-01 12:23:31 +02:00
|
|
|
PrettySlice(self.as_ref())
|
2015-12-01 18:25:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 16:43:48 +01:00
|
|
|
/// A byte collection reference that can either be a slice or a vector
|
2016-01-13 15:14:24 +01:00
|
|
|
pub enum BytesRef<'a> {
|
2016-02-03 16:43:48 +01:00
|
|
|
/// This is a reference to a vector
|
2016-01-13 15:14:24 +01:00
|
|
|
Flexible(&'a mut Bytes),
|
2016-02-03 16:43:48 +01:00
|
|
|
/// This is a reference to a slice
|
2016-01-13 15:14:24 +01:00
|
|
|
Fixed(&'a mut [u8])
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Deref for BytesRef<'a> {
|
|
|
|
type Target = [u8];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[u8] {
|
2016-01-19 12:14:29 +01:00
|
|
|
match *self {
|
|
|
|
BytesRef::Flexible(ref bytes) => bytes,
|
2016-02-19 11:31:40 +01:00
|
|
|
BytesRef::Fixed(ref bytes) => bytes,
|
2016-01-13 15:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a> DerefMut for BytesRef<'a> {
|
|
|
|
fn deref_mut(&mut self) -> &mut [u8] {
|
2016-01-19 12:14:29 +01:00
|
|
|
match *self {
|
|
|
|
BytesRef::Flexible(ref mut bytes) => bytes,
|
2016-02-19 11:31:40 +01:00
|
|
|
BytesRef::Fixed(ref mut bytes) => bytes,
|
2016-01-13 15:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 12:23:31 +02:00
|
|
|
/// Vector of bytes.
|
|
|
|
pub type Bytes = Vec<u8>;
|