Skip to content

Commit f21b69f

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#1014: Use fragment-specifier literal
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
2 parents bf3d812 + 6227e87 commit f21b69f

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
@@ -657,7 +657,7 @@ impl Script {
657657
pub fn bytes_to_asm_fmt(script: &[u8], f: &mut dyn fmt::Write) -> fmt::Result {
658658
// This has to be a macro because it needs to break the loop
659659
macro_rules! read_push_data_len {
660-
($iter:expr, $len:expr, $formatter:expr) => {
660+
($iter:expr, $len:literal, $formatter:expr) => {
661661
match read_uint_iter($iter, $len) {
662662
Ok(n) => {
663663
n

src/consensus/encode.rs

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

548548
// Arrays
549549
macro_rules! impl_array {
550-
( $size:expr ) => {
550+
( $size:literal ) => {
551551
impl Encodable for [u8; $size] {
552552
#[inline]
553553
fn consensus_encode<W: WriteExt + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {

src/internal_macros.rs

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

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

140140
macro_rules! serde_string_impl {
141-
($name:ident, $expecting:expr) => {
141+
($name:ident, $expecting:literal) => {
142142
#[cfg(feature = "serde")]
143143
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
144144
impl<'de> $crate::serde::Deserialize<'de> for $name {
@@ -185,7 +185,7 @@ macro_rules! serde_string_impl {
185185
/// A combination macro where the human-readable serialization is done like
186186
/// serde_string_impl and the non-human-readable impl is done as a struct.
187187
macro_rules! serde_struct_human_string_impl {
188-
($name:ident, $expecting:expr, $($fe:ident),*) => (
188+
($name:ident, $expecting:literal, $($fe:ident),*) => (
189189
#[cfg(feature = "serde")]
190190
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
191191
impl<'de> $crate::serde::Deserialize<'de> for $name {
@@ -363,7 +363,7 @@ macro_rules! serde_struct_human_string_impl {
363363
/// - core::str::FromStr
364364
/// - hashes::hex::FromHex
365365
macro_rules! impl_bytes_newtype {
366-
($t:ident, $len:expr) => (
366+
($t:ident, $len:literal) => (
367367

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

src/util/address.rs

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

10201020
use super::*;
10211021

1022-
macro_rules! hex (($hex:expr) => (Vec::from_hex($hex).unwrap()));
1023-
macro_rules! hex_key (($hex:expr) => (PublicKey::from_slice(&hex!($hex)).unwrap()));
1024-
macro_rules! hex_script (($hex:expr) => (Script::from(hex!($hex))));
1025-
macro_rules! hex_pubkeyhash (($hex:expr) => (PubkeyHash::from_hex(&$hex).unwrap()));
1026-
macro_rules! hex_scripthash (($hex:expr) => (ScriptHash::from_hex($hex).unwrap()));
1022+
macro_rules! hex (($hex:literal) => (Vec::from_hex($hex).unwrap()));
1023+
macro_rules! hex_key (($hex:literal) => (PublicKey::from_slice(&hex!($hex)).unwrap()));
1024+
macro_rules! hex_script (($hex:literal) => (Script::from(hex!($hex))));
1025+
macro_rules! hex_pubkeyhash (($hex:literal) => (PubkeyHash::from_hex(&$hex).unwrap()));
1026+
macro_rules! hex_scripthash (($hex:literal) => (ScriptHash::from_hex($hex).unwrap()));
10271027

10281028
fn roundtrips(addr: &Address) {
10291029
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)