Skip to content

Commit

Permalink
Merge #1014: Use fragment-specifier literal
Browse files Browse the repository at this point in the history
4d22919 Use fragment-specifier literal (Tobin C. Harding)

Pull request description:

  Currently we are using the fragment-specifier `expr` in a bunch of
  macros for captures that are only used for literals. If we use `literal`
  instead it allows the compiler to give slightly more specific error
  messages.

  The benefits of this change are minor. Of note, this patch found one
  unusual macro call site (removed unnecessary `&`).

  The macros changed are all internal macros, this is not a breaking change.

ACKs for top commit:
  Kixunil:
    ACK 4d22919
  apoelstra:
    ACK 4d22919

Tree-SHA512: 51c109fe3a884191bf623508555c1d5ad337a3f3b48538d18aec13e581f2c5fbbd055be49600ced19f38541412c34090bd8bac61fd05d5aa9702c96ff521364f
  • Loading branch information
apoelstra committed Jun 30, 2022
2 parents 05f7545 + 4d22919 commit 30baeea
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ impl Script {
pub fn bytes_to_asm_fmt(script: &[u8], f: &mut dyn fmt::Write) -> fmt::Result {
// This has to be a macro because it needs to break the loop
macro_rules! read_push_data_len {
($iter:expr, $len:expr, $formatter:expr) => {
($iter:expr, $len:literal, $formatter:expr) => {
match read_uint_iter($iter, $len) {
Ok(n) => {
n
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ impl Decodable for Cow<'static, str> {

// Arrays
macro_rules! impl_array {
( $size:expr ) => {
( $size:literal ) => {
impl Encodable for [u8; $size] {
#[inline]
fn consensus_encode<W: WriteExt + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
Expand Down
10 changes: 5 additions & 5 deletions src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ macro_rules! impl_consensus_encoding {

/// Implements standard array methods for a given wrapper type
macro_rules! impl_array_newtype {
($thing:ident, $ty:ty, $len:expr) => {
($thing:ident, $ty:ty, $len:literal) => {
impl $thing {
/// Converts the object to a raw pointer
#[inline]
Expand Down Expand Up @@ -138,7 +138,7 @@ macro_rules! hex_hash (($h:ident, $s:expr) => ($h::from_slice(&<$crate::prelude:
macro_rules! hex_decode (($h:ident, $s:expr) => (deserialize::<$h>(&<$crate::prelude::Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap()));

macro_rules! serde_string_impl {
($name:ident, $expecting:expr) => {
($name:ident, $expecting:literal) => {
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> $crate::serde::Deserialize<'de> for $name {
Expand Down Expand Up @@ -185,7 +185,7 @@ macro_rules! serde_string_impl {
/// A combination macro where the human-readable serialization is done like
/// serde_string_impl and the non-human-readable impl is done as a struct.
macro_rules! serde_struct_human_string_impl {
($name:ident, $expecting:expr, $($fe:ident),*) => (
($name:ident, $expecting:literal, $($fe:ident),*) => (
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> $crate::serde::Deserialize<'de> for $name {
Expand Down Expand Up @@ -363,7 +363,7 @@ macro_rules! serde_struct_human_string_impl {
/// - core::str::FromStr
/// - hashes::hex::FromHex
macro_rules! impl_bytes_newtype {
($t:ident, $len:expr) => (
($t:ident, $len:literal) => (

impl ::core::fmt::LowerHex for $t {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
Expand Down Expand Up @@ -494,7 +494,7 @@ macro_rules! user_enum {
$(#[$attr:meta])*
pub enum $name:ident {
$(#[$doc:meta]
$elem:ident <-> $txt:expr),*
$elem:ident <-> $txt:literal),*
}
) => (
$(#[$attr])*
Expand Down
10 changes: 5 additions & 5 deletions src/util/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,11 +1019,11 @@ mod tests {

use super::*;

macro_rules! hex (($hex:expr) => (Vec::from_hex($hex).unwrap()));
macro_rules! hex_key (($hex:expr) => (PublicKey::from_slice(&hex!($hex)).unwrap()));
macro_rules! hex_script (($hex:expr) => (Script::from(hex!($hex))));
macro_rules! hex_pubkeyhash (($hex:expr) => (PubkeyHash::from_hex(&$hex).unwrap()));
macro_rules! hex_scripthash (($hex:expr) => (ScriptHash::from_hex($hex).unwrap()));
macro_rules! hex (($hex:literal) => (Vec::from_hex($hex).unwrap()));
macro_rules! hex_key (($hex:literal) => (PublicKey::from_slice(&hex!($hex)).unwrap()));
macro_rules! hex_script (($hex:literal) => (Script::from(hex!($hex))));
macro_rules! hex_pubkeyhash (($hex:literal) => (PubkeyHash::from_hex(&$hex).unwrap()));
macro_rules! hex_scripthash (($hex:literal) => (ScriptHash::from_hex($hex).unwrap()));

fn roundtrips(addr: &Address) {
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions src/util/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ mod tests {

// Creates individual test functions to make it easier to find which check failed.
macro_rules! check_format_non_negative {
($denom:ident; $($test_name:ident, $val:expr, $format_string:expr, $expected:expr);* $(;)?) => {
($denom:ident; $($test_name:ident, $val:literal, $format_string:literal, $expected:literal);* $(;)?) => {
$(
#[test]
fn $test_name() {
Expand All @@ -1690,7 +1690,7 @@ mod tests {
}

macro_rules! check_format_non_negative_show_denom {
($denom:ident, $denom_suffix:expr; $($test_name:ident, $val:expr, $format_string:expr, $expected:expr);* $(;)?) => {
($denom:ident, $denom_suffix:literal; $($test_name:ident, $val:literal, $format_string:literal, $expected:literal);* $(;)?) => {
$(
#[test]
fn $test_name() {
Expand Down
2 changes: 1 addition & 1 deletion src/util/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//!

macro_rules! construct_uint {
($name:ident, $n_words:expr) => {
($name:ident, $n_words:literal) => {
/// Little-endian large integer type
#[derive(Copy, Clone, PartialEq, Eq, Hash, Default)]
pub struct $name(pub [u64; $n_words]);
Expand Down

0 comments on commit 30baeea

Please sign in to comment.