diff --git a/core/src/lib.rs b/core/src/lib.rs index dd50489e..ba7cc2a9 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -17,7 +17,6 @@ #![warn(missing_docs)] #![warn(unused_extern_crates)] -#[cfg(not(tarpaulin_include))] #[macro_use] pub mod macros; diff --git a/core/src/macros.rs b/core/src/macros.rs index 956c2d02..94ab3112 100644 --- a/core/src/macros.rs +++ b/core/src/macros.rs @@ -1,192 +1,5 @@ -//! Useful macros for implementing new chains +//! Macro for defining a new hash type. -#[macro_export] -/// Implement `serde::Serialize` and `serde::Deserialize` by passing through to the hex -macro_rules! impl_hex_serde { - ($item:ty) => { - impl serde::Serialize for $item { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - let s = $crate::ser::ByteFormat::serialize_hex(self); - serializer.serialize_str(&s) - } - } - - impl<'de> serde::Deserialize<'de> for $item { - fn deserialize(deserializer: D) -> Result<$item, D::Error> - where - D: serde::Deserializer<'de>, - { - let s: &str = serde::Deserialize::deserialize(deserializer)?; - <$item as $crate::ser::ByteFormat>::deserialize_hex(s) - .map_err(|e| serde::de::Error::custom(e.to_string())) - } - } - }; -} - -#[macro_export] -/// Wrap a prefixed vector of bytes (`u8`) in a newtype, and implement convenience functions for -/// it. -macro_rules! wrap_prefixed_byte_vector { - ( - $(#[$outer:meta])* - $wrapper_name:ident - ) => { - $(#[$outer])* - #[derive(Clone, Debug, Eq, PartialEq, Default, Hash, PartialOrd, Ord)] - pub struct $wrapper_name(Vec); - - impl $crate::ser::ByteFormat for $wrapper_name { - type Error = $crate::ser::SerError; - - fn serialized_length(&self) -> usize { - let mut length = self.len(); - length += self.len_prefix() as usize; - length - } - - fn read_from(reader: &mut R) -> Result - where - R: std::io::Read - { - Ok(coins_core::ser::read_prefix_vec(reader)?.into()) - } - - fn write_to(&self, writer: &mut W) -> Result - where - W: std::io::Write - { - coins_core::ser::write_prefix_vec(writer, &self.0) - } - } - - impl_hex_serde!($wrapper_name); - - impl std::convert::AsRef<[u8]> for $wrapper_name { - fn as_ref(&self) -> &[u8] { - &self.0[..] - } - } - - impl $wrapper_name { - /// Instantate a new wrapped vector - pub fn new(v: Vec) -> Self { - Self(v) - } - - /// Construct an empty wrapped vector instance. - pub fn null() -> Self { - Self(vec![]) - } - - /// Return a reference to the underlying bytes - pub fn items(&self) -> &[u8] { - &self.0 - } - - /// Set the underlying items vector. - pub fn set_items(&mut self, v: Vec) { - self.0 = v - } - - /// Push an item to the item vector. - pub fn push(&mut self, i: u8) { - self.0.push(i) - } - - /// Return the length of the item vector. - pub fn len(&self) -> usize { - self.0.len() - } - - /// Return true if the length of the item vector is 0. - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Determine the byte-length of the vector length prefix - pub fn len_prefix(&self) -> u8 { - $crate::ser::prefix_byte_len(self.len() as u64) - } - - /// Insert an item at the specified index. - pub fn insert(&mut self, index: usize, i: u8) { - self.0.insert(index, i) - } - } - - impl From<&[u8]> for $wrapper_name { - fn from(v: &[u8]) -> Self { - Self(v.to_vec()) - } - } - - impl From> for $wrapper_name { - fn from(v: Vec) -> Self { - Self(v) - } - } - - impl std::ops::Index for $wrapper_name { - type Output = u8; - - fn index(&self, index: usize) -> &Self::Output { - &self.0[index] - } - } - - impl std::ops::Index> for $wrapper_name { - type Output = [u8]; - - fn index(&self, range: std::ops::Range) -> &[u8] { - &self.0[range] - } - } - - impl std::ops::IndexMut for $wrapper_name { - fn index_mut(&mut self, index: usize) -> &mut Self::Output { - &mut self.0[index] - } - } - - impl std::iter::Extend for $wrapper_name { - fn extend>(&mut self, iter: I) { - self.0.extend(iter) - } - } - - impl std::iter::IntoIterator for $wrapper_name { - type Item = u8; - type IntoIter = std::vec::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() - } - } - } -} - -#[macro_export] -/// Implement conversion between script types by passing via `as_ref().into()` -macro_rules! impl_script_conversion { - ($t1:ty, $t2:ty) => { - impl From<&$t2> for $t1 { - fn from(t: &$t2) -> $t1 { - t.as_ref().into() - } - } - impl From<&$t1> for $t2 { - fn from(t: &$t1) -> $t2 { - t.as_ref().into() - } - } - }; -} - -#[macro_export] /// Instantiate a new marked digest. Wraps the output of some type that implemented `digest::Digest` macro_rules! marked_digest { (