From b2e044f9dbd8043700afc0b8b11d16e833469406 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Tue, 15 Jan 2019 11:14:54 -0500 Subject: [PATCH] Internalize unnecessarily exported macros --- src/internal_macros.rs | 108 ++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 +- src/macros.rs | 123 ----------------------------------------- src/test_macros.rs | 1 - 4 files changed, 109 insertions(+), 126 deletions(-) delete mode 100644 src/macros.rs diff --git a/src/internal_macros.rs b/src/internal_macros.rs index afb66580..6af38a46 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -12,6 +12,10 @@ // If not, see . // +//! Internal Macros +//! +//! Macros meant to be used inside the Rust Bitcoin library + macro_rules! impl_consensus_encoding { ($thing:ident, $($field:ident),+) => ( impl ::consensus::encode::Encodable for $thing { @@ -416,3 +420,107 @@ macro_rules! serde_struct_impl { } ) } + +macro_rules! user_enum { + ( + $(#[$attr:meta])* + pub enum $name:ident { + $(#[$doc:meta] + $elem:ident <-> $txt:expr),* + } + ) => ( + $(#[$attr])* + pub enum $name { + $(#[$doc] $elem),* + } + + impl ::std::fmt::Debug for $name { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + f.pad(match *self { + $($name::$elem => $txt),* + }) + } + } + + impl ::std::fmt::Display for $name { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + f.pad(match *self { + $($name::$elem => $txt),* + }) + } + } + + impl ::std::str::FromStr for $name { + type Err = ::std::io::Error; + #[inline] + fn from_str(s: &str) -> Result { + match s { + $($txt => Ok($name::$elem)),*, + _ => Err(::std::io::Error::new( + ::std::io::ErrorKind::InvalidInput, + format!("Unknown network (type {})", s), + )), + } + } + } + + #[cfg(feature = "serde")] + impl<'de> $crate::serde::Deserialize<'de> for $name { + #[inline] + fn deserialize(deserializer: D) -> Result + where + D: $crate::serde::Deserializer<'de>, + { + use $crate::std::fmt::{self, Formatter}; + + struct Visitor; + impl<'de> $crate::serde::de::Visitor<'de> for Visitor { + type Value = $name; + + fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { + formatter.write_str("an enum value") + } + + fn visit_str(self, v: &str) -> Result + where + E: $crate::serde::de::Error, + { + static FIELDS: &'static [&'static str] = &[$(stringify!($txt)),*]; + + $( if v == $txt { Ok($name::$elem) } )else* + else { + Err(E::unknown_variant(v, FIELDS)) + } + } + + fn visit_borrowed_str(self, v: &'de str) -> Result + where + E: $crate::serde::de::Error, + { + self.visit_str(v) + } + + fn visit_string(self, v: String) -> Result + where + E: $crate::serde::de::Error, + { + self.visit_str(&v) + } + + } + + deserializer.deserialize_str(Visitor) + } + } + + #[cfg(feature = "serde")] + impl ::serde::Serialize for $name { + fn serialize(&self, serializer: S) -> Result + where + S: ::serde::Serializer, + { + serializer.serialize_str(&self.to_string()) + } + } + ); +} diff --git a/src/lib.rs b/src/lib.rs index 5311f617..362f0591 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,6 @@ mod test_macros; #[macro_use] mod internal_macros; #[macro_use] -pub mod macros; pub mod network; pub mod blockdata; pub mod util; @@ -82,4 +81,4 @@ pub use util::decimal::Decimal; pub use util::decimal::UDecimal; #[cfg(feature = "fuzztarget")] -pub mod fuzz_util; \ No newline at end of file +pub mod fuzz_util; diff --git a/src/macros.rs b/src/macros.rs deleted file mode 100644 index 718d4ae5..00000000 --- a/src/macros.rs +++ /dev/null @@ -1,123 +0,0 @@ -// Rust Bitcoin Library -// Written in 2014 by -// Andrew Poelstra -// -// To the extent possible under law, the author(s) have dedicated all -// copyright and related and neighboring rights to this software to -// the public domain worldwide. This software is distributed without -// any warranty. -// -// You should have received a copy of the CC0 Public Domain Dedication -// along with this software. -// If not, see . -// - -//! Macros -//! -//! Macros available to users of the Bitcoin library - -#[macro_export] -macro_rules! user_enum { - ( - $(#[$attr:meta])* - pub enum $name:ident { - $(#[$doc:meta] - $elem:ident <-> $txt:expr),* - } - ) => ( - $(#[$attr])* - pub enum $name { - $(#[$doc] $elem),* - } - - impl ::std::fmt::Debug for $name { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { - f.pad(match *self { - $($name::$elem => $txt),* - }) - } - } - - impl ::std::fmt::Display for $name { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { - f.pad(match *self { - $($name::$elem => $txt),* - }) - } - } - - impl ::std::str::FromStr for $name { - type Err = ::std::io::Error; - #[inline] - fn from_str(s: &str) -> Result { - match s { - $($txt => Ok($name::$elem)),*, - _ => Err(::std::io::Error::new( - ::std::io::ErrorKind::InvalidInput, - format!("Unknown network (type {})", s), - )), - } - } - } - - #[cfg(feature = "serde")] - impl<'de> $crate::serde::Deserialize<'de> for $name { - #[inline] - fn deserialize(deserializer: D) -> Result - where - D: $crate::serde::Deserializer<'de>, - { - use $crate::std::fmt::{self, Formatter}; - - struct Visitor; - impl<'de> $crate::serde::de::Visitor<'de> for Visitor { - type Value = $name; - - fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { - formatter.write_str("an enum value") - } - - fn visit_str(self, v: &str) -> Result - where - E: $crate::serde::de::Error, - { - static FIELDS: &'static [&'static str] = &[$(stringify!($txt)),*]; - - $( if v == $txt { Ok($name::$elem) } )else* - else { - Err(E::unknown_variant(v, FIELDS)) - } - } - - fn visit_borrowed_str(self, v: &'de str) -> Result - where - E: $crate::serde::de::Error, - { - self.visit_str(v) - } - - fn visit_string(self, v: String) -> Result - where - E: $crate::serde::de::Error, - { - self.visit_str(&v) - } - - } - - deserializer.deserialize_str(Visitor) - } - } - - #[cfg(feature = "serde")] - impl ::serde::Serialize for $name { - fn serialize(&self, serializer: S) -> Result - where - S: ::serde::Serializer, - { - serializer.serialize_str(&self.to_string()) - } - } - ); -} - diff --git a/src/test_macros.rs b/src/test_macros.rs index b2e9cafc..6bf5e3dd 100644 --- a/src/test_macros.rs +++ b/src/test_macros.rs @@ -16,7 +16,6 @@ //! //! Internal macros used for unit tests -#[macro_export] #[cfg(all(feature = "serde", feature = "strason"))] macro_rules! serde_round_trip ( ($var:expr) => ({