Skip to content

Commit 6227e87

Browse files
committed
Use fragment-specifier literal
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.
1 parent 8525a4e commit 6227e87

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

src/blockdata/script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl Script {
672672
pub fn bytes_to_asm_fmt(script: &[u8], f: &mut dyn fmt::Write) -> fmt::Result {
673673
// This has to be a macro because it needs to break the loop
674674
macro_rules! read_push_data_len {
675-
($iter:expr, $len:expr, $formatter:expr) => {
675+
($iter:expr, $len:literal, $formatter:expr) => {
676676
match read_uint_iter($iter, $len) {
677677
Ok(n) => {
678678
n

src/consensus/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl Decodable for Cow<'static, str> {
536536

537537
// Arrays
538538
macro_rules! impl_array {
539-
( $size:expr ) => {
539+
( $size:literal ) => {
540540
impl Encodable for [u8; $size] {
541541
#[inline]
542542
fn consensus_encode<S: WriteExt>(&self, mut s: S) -> Result<usize, io::Error> {

src/internal_macros.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ macro_rules! impl_consensus_encoding {
5757

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

139139
macro_rules! serde_string_impl {
140-
($name:ident, $expecting:expr) => {
140+
($name:ident, $expecting:literal) => {
141141
#[cfg(feature = "serde")]
142142
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
143143
impl<'de> $crate::serde::Deserialize<'de> for $name {
@@ -184,7 +184,7 @@ macro_rules! serde_string_impl {
184184
/// A combination macro where the human-readable serialization is done like
185185
/// serde_string_impl and the non-human-readable impl is done as a struct.
186186
macro_rules! serde_struct_human_string_impl {
187-
($name:ident, $expecting:expr, $($fe:ident),*) => (
187+
($name:ident, $expecting:literal, $($fe:ident),*) => (
188188
#[cfg(feature = "serde")]
189189
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
190190
impl<'de> $crate::serde::Deserialize<'de> for $name {
@@ -362,7 +362,7 @@ macro_rules! serde_struct_human_string_impl {
362362
/// - core::str::FromStr
363363
/// - hashes::hex::FromHex
364364
macro_rules! impl_bytes_newtype {
365-
($t:ident, $len:expr) => (
365+
($t:ident, $len:literal) => (
366366

367367
impl ::core::fmt::LowerHex for $t {
368368
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
@@ -493,7 +493,7 @@ macro_rules! user_enum {
493493
$(#[$attr:meta])*
494494
pub enum $name:ident {
495495
$(#[$doc:meta]
496-
$elem:ident <-> $txt:expr),*
496+
$elem:ident <-> $txt:literal),*
497497
}
498498
) => (
499499
$(#[$attr])*

src/util/address.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -946,11 +946,11 @@ mod tests {
946946

947947
use super::*;
948948

949-
macro_rules! hex (($hex:expr) => (Vec::from_hex($hex).unwrap()));
950-
macro_rules! hex_key (($hex:expr) => (PublicKey::from_slice(&hex!($hex)).unwrap()));
951-
macro_rules! hex_script (($hex:expr) => (Script::from(hex!($hex))));
952-
macro_rules! hex_pubkeyhash (($hex:expr) => (PubkeyHash::from_hex(&$hex).unwrap()));
953-
macro_rules! hex_scripthash (($hex:expr) => (ScriptHash::from_hex($hex).unwrap()));
949+
macro_rules! hex (($hex:literal) => (Vec::from_hex($hex).unwrap()));
950+
macro_rules! hex_key (($hex:literal) => (PublicKey::from_slice(&hex!($hex)).unwrap()));
951+
macro_rules! hex_script (($hex:literal) => (Script::from(hex!($hex))));
952+
macro_rules! hex_pubkeyhash (($hex:literal) => (PubkeyHash::from_hex(&$hex).unwrap()));
953+
macro_rules! hex_scripthash (($hex:literal) => (ScriptHash::from_hex($hex).unwrap()));
954954

955955
fn roundtrips(addr: &Address) {
956956
assert_eq!(

src/util/amount.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ mod tests {
16781678

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

16921692
macro_rules! check_format_non_negative_show_denom {
1693-
($denom:ident, $denom_suffix:expr; $($test_name:ident, $val:expr, $format_string:expr, $expected:expr);* $(;)?) => {
1693+
($denom:ident, $denom_suffix:literal; $($test_name:ident, $val:literal, $format_string:literal, $expected:literal);* $(;)?) => {
16941694
$(
16951695
#[test]
16961696
fn $test_name() {

src/util/uint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//!
2020
2121
macro_rules! construct_uint {
22-
($name:ident, $n_words:expr) => {
22+
($name:ident, $n_words:literal) => {
2323
/// Little-endian large integer type
2424
#[derive(Copy, Clone, PartialEq, Eq, Hash, Default)]
2525
pub struct $name(pub [u64; $n_words]);

0 commit comments

Comments
 (0)