From 3f2d4287061c7b42a9609b21369a7acdab679091 Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Mon, 5 Aug 2019 14:52:34 -0400 Subject: [PATCH] Remove needless references --- src/blockdata/opcodes.rs | 34 +++++++++++++++++----------------- src/blockdata/transaction.rs | 6 +++--- src/network/constants.rs | 13 ++++++------- src/util/amount.rs | 32 ++++++++++++++++---------------- src/util/bip158.rs | 4 ++-- src/util/bip32.rs | 16 ++++++++-------- src/util/psbt/mod.rs | 4 ++-- 7 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/blockdata/opcodes.rs b/src/blockdata/opcodes.rs index 724b16c2..697eee54 100644 --- a/src/blockdata/opcodes.rs +++ b/src/blockdata/opcodes.rs @@ -658,29 +658,29 @@ impl fmt::Debug for All { impl All { /// Classifies an Opcode into a broad class #[inline] - pub fn classify(&self) -> Class { + pub fn classify(self) -> Class { // 17 opcodes - if *self == all::OP_VERIF || *self == all::OP_VERNOTIF || - *self == all::OP_CAT || *self == all::OP_SUBSTR || - *self == all::OP_LEFT || *self == all::OP_RIGHT || - *self == all::OP_INVERT || *self == all::OP_AND || - *self == all::OP_OR || *self == all::OP_XOR || - *self == all::OP_2MUL || *self == all::OP_2DIV || - *self == all::OP_MUL || *self == all::OP_DIV || *self == all::OP_MOD || - *self == all::OP_LSHIFT || *self == all::OP_RSHIFT { + if self == all::OP_VERIF || self == all::OP_VERNOTIF || + self == all::OP_CAT || self == all::OP_SUBSTR || + self == all::OP_LEFT || self == all::OP_RIGHT || + self == all::OP_INVERT || self == all::OP_AND || + self == all::OP_OR || self == all::OP_XOR || + self == all::OP_2MUL || self == all::OP_2DIV || + self == all::OP_MUL || self == all::OP_DIV || self == all::OP_MOD || + self == all::OP_LSHIFT || self == all::OP_RSHIFT { Class::IllegalOp // 11 opcodes - } else if *self == all::OP_NOP || + } else if self == all::OP_NOP || (all::OP_NOP1.code <= self.code && self.code <= all::OP_NOP10.code) { Class::NoOp // 75 opcodes - } else if *self == all::OP_RESERVED || *self == all::OP_VER || *self == all::OP_RETURN || - *self == all::OP_RESERVED1 || *self == all::OP_RESERVED2 || + } else if self == all::OP_RESERVED || self == all::OP_VER || self == all::OP_RETURN || + self == all::OP_RESERVED1 || self == all::OP_RESERVED2 || self.code >= all::OP_RETURN_186.code { Class::ReturnOp // 1 opcode - } else if *self == all::OP_PUSHNUM_NEG1 { + } else if self == all::OP_PUSHNUM_NEG1 { Class::PushNum(-1) // 16 opcodes } else if all::OP_PUSHNUM_1.code <= self.code && @@ -691,13 +691,13 @@ impl All { Class::PushBytes(self.code as u32) // 60 opcodes } else { - Class::Ordinary(Ordinary::try_from_all(*self).unwrap()) + Class::Ordinary(Ordinary::try_from_all(self).unwrap()) } } /// Encode as a byte #[inline] - pub fn into_u8(&self) -> u8 { + pub fn into_u8(self) -> u8 { self.code } } @@ -809,8 +809,8 @@ ordinary_opcode! { impl Ordinary { /// Encode as a byte #[inline] - pub fn into_u8(&self) -> u8 { - *self as u8 + pub fn into_u8(self) -> u8 { + self as u8 } } diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 6e83f97b..99b87f22 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -598,8 +598,8 @@ pub enum SigHashType { impl SigHashType { /// Break the sighash flag into the "real" sighash flag and the ANYONECANPAY boolean - pub(crate) fn split_anyonecanpay_flag(&self) -> (SigHashType, bool) { - match *self { + pub(crate) fn split_anyonecanpay_flag(self) -> (SigHashType, bool) { + match self { SigHashType::All => (SigHashType::All, false), SigHashType::None => (SigHashType::None, false), SigHashType::Single => (SigHashType::Single, false), @@ -626,7 +626,7 @@ impl SigHashType { } /// Converts to a u32 - pub fn as_u32(&self) -> u32 { *self as u32 } + pub fn as_u32(self) -> u32 { self as u32 } } diff --git a/src/network/constants.rs b/src/network/constants.rs index 8724398a..0540981a 100644 --- a/src/network/constants.rs +++ b/src/network/constants.rs @@ -89,9 +89,9 @@ impl Network { /// let network = Network::Bitcoin; /// assert_eq!(network.magic(), 0xD9B4BEF9); /// ``` - pub fn magic(&self) -> u32 { + pub fn magic(self) -> u32 { // Note: any new entries here must be added to `from_magic` above - match *self { + match self { Network::Bitcoin => 0xD9B4BEF9, Network::Testnet => 0x0709110B, Network::Regtest => 0xDAB5BFFA, @@ -154,12 +154,12 @@ impl ServiceFlags { } /// Check whether [ServiceFlags] are included in this one. - pub fn has(&self, flags: ServiceFlags) -> bool { + pub fn has(self, flags: ServiceFlags) -> bool { (self.0 | flags.0) == self.0 } /// Get the integer representation of this [ServiceFlags]. - pub fn as_u64(&self) -> u64 { + pub fn as_u64(self) -> u64 { self.0 } } @@ -178,11 +178,10 @@ impl fmt::UpperHex for ServiceFlags { impl fmt::Display for ServiceFlags { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if *self == ServiceFlags::NONE { + let mut flags = *self; + if flags == ServiceFlags::NONE { return write!(f, "ServiceFlags(NONE)"); } - - let mut flags = self.clone(); let mut first = true; macro_rules! write_flag { ($f:ident) => { diff --git a/src/util/amount.rs b/src/util/amount.rs index 1bbb6ec6..31cb31f6 100644 --- a/src/util/amount.rs +++ b/src/util/amount.rs @@ -340,7 +340,7 @@ impl Amount { /// Express this [Amount] as a floating-point value in the given denomination. /// /// Please be aware of the risk of using floating-point numbers. - pub fn to_float_in(&self, denom: Denomination) -> f64 { + pub fn to_float_in(self, denom: Denomination) -> f64 { f64::from_str(&self.to_string_in(denom)).unwrap() } @@ -349,7 +349,7 @@ impl Amount { /// Equivalent to `to_float_in(Denomination::Bitcoin)`. /// /// Please be aware of the risk of using floating-point numbers. - pub fn as_btc(&self) -> f64 { + pub fn as_btc(self) -> f64 { self.to_float_in(Denomination::Bitcoin) } @@ -370,14 +370,14 @@ impl Amount { /// Format the value of this [Amount] in the given denomination. /// /// Does not include the denomination. - pub fn fmt_value_in(&self, f: &mut fmt::Write, denom: Denomination) -> fmt::Result { + pub fn fmt_value_in(self, f: &mut fmt::Write, denom: Denomination) -> fmt::Result { fmt_satoshi_in(self.as_sat(), false, f, denom) } /// Get a string number of this [Amount] in the given denomination. /// /// Does not include the denomination. - pub fn to_string_in(&self, denom: Denomination) -> String { + pub fn to_string_in(self, denom: Denomination) -> String { let mut buf = String::new(); self.fmt_value_in(&mut buf, denom).unwrap(); buf @@ -385,7 +385,7 @@ impl Amount { /// Get a formatted string of this [Amount] in the given denomination, /// suffixed with the abbreviation for the denomination. - pub fn to_string_with_denomination(&self, denom: Denomination) -> String { + pub fn to_string_with_denomination(self, denom: Denomination) -> String { let mut buf = String::new(); self.fmt_value_in(&mut buf, denom).unwrap(); write!(buf, " {}", denom).unwrap(); @@ -637,7 +637,7 @@ impl SignedAmount { /// Express this [SignedAmount] as a floating-point value in the given denomination. /// /// Please be aware of the risk of using floating-point numbers. - pub fn to_float_in(&self, denom: Denomination) -> f64 { + pub fn to_float_in(self, denom: Denomination) -> f64 { f64::from_str(&self.to_string_in(denom)).unwrap() } @@ -646,7 +646,7 @@ impl SignedAmount { /// Equivalent to `to_float_in(Denomination::Bitcoin)`. /// /// Please be aware of the risk of using floating-point numbers. - pub fn as_btc(&self) -> f64 { + pub fn as_btc(self) -> f64 { self.to_float_in(Denomination::Bitcoin) } @@ -667,7 +667,7 @@ impl SignedAmount { /// Format the value of this [SignedAmount] in the given denomination. /// /// Does not include the denomination. - pub fn fmt_value_in(&self, f: &mut fmt::Write, denom: Denomination) -> fmt::Result { + pub fn fmt_value_in(self, f: &mut fmt::Write, denom: Denomination) -> fmt::Result { let sats = self.as_sat().checked_abs().map(|a: i64| a as u64).unwrap_or_else(|| { // We could also hard code this into `9223372036854775808` u64::max_value() - self.as_sat() as u64 +1 @@ -678,7 +678,7 @@ impl SignedAmount { /// Get a string number of this [SignedAmount] in the given denomination. /// /// Does not include the denomination. - pub fn to_string_in(&self, denom: Denomination) -> String { + pub fn to_string_in(self, denom: Denomination) -> String { let mut buf = String::new(); self.fmt_value_in(&mut buf, denom).unwrap(); buf @@ -686,7 +686,7 @@ impl SignedAmount { /// Get a formatted string of this [SignedAmount] in the given denomination, /// suffixed with the abbreviation for the denomination. - pub fn to_string_with_denomination(&self, denom: Denomination) -> String { + pub fn to_string_with_denomination(self, denom: Denomination) -> String { let mut buf = String::new(); self.fmt_value_in(&mut buf, denom).unwrap(); write!(buf, " {}", denom).unwrap(); @@ -1313,12 +1313,12 @@ mod tests { use super::Denomination as D; let amt = Amount::from_sat(42); let denom = Amount::to_string_with_denomination; - assert_eq!(Amount::from_str(&denom(&amt, D::Bitcoin)), Ok(amt)); - assert_eq!(Amount::from_str(&denom(&amt, D::MilliBitcoin)), Ok(amt)); - assert_eq!(Amount::from_str(&denom(&amt, D::MicroBitcoin)), Ok(amt)); - assert_eq!(Amount::from_str(&denom(&amt, D::Bit)), Ok(amt)); - assert_eq!(Amount::from_str(&denom(&amt, D::Satoshi)), Ok(amt)); - assert_eq!(Amount::from_str(&denom(&amt, D::MilliSatoshi)), Ok(amt)); + assert_eq!(Amount::from_str(&denom(amt, D::Bitcoin)), Ok(amt)); + assert_eq!(Amount::from_str(&denom(amt, D::MilliBitcoin)), Ok(amt)); + assert_eq!(Amount::from_str(&denom(amt, D::MicroBitcoin)), Ok(amt)); + assert_eq!(Amount::from_str(&denom(amt, D::Bit)), Ok(amt)); + assert_eq!(Amount::from_str(&denom(amt, D::Satoshi)), Ok(amt)); + assert_eq!(Amount::from_str(&denom(amt, D::MilliSatoshi)), Ok(amt)); assert_eq!(Amount::from_str("42 satoshi BTC"), Err(ParseAmountError::InvalidFormat)); assert_eq!(SignedAmount::from_str("-42 satoshi BTC"), Err(ParseAmountError::InvalidFormat)); diff --git a/src/util/bip158.rs b/src/util/bip158.rs index b7154bfe..d196a11d 100644 --- a/src/util/bip158.rs +++ b/src/util/bip158.rs @@ -241,7 +241,7 @@ impl GCSFilterReader { pub fn match_any(&self, reader: &mut io::Read, query: &mut Iterator) -> Result { let mut decoder = reader; let n_elements: VarInt = Decodable::consensus_decode(&mut decoder).unwrap_or(VarInt(0)); - let ref mut reader = decoder; + let reader = &mut decoder; // map hashes to [0, n_elements << grp] let nm = n_elements.0 * self.m; let mut mapped = query.map(|e| map_to_range(self.filter.hash(e), nm)).collect::>(); @@ -281,7 +281,7 @@ impl GCSFilterReader { pub fn match_all(&self, reader: &mut io::Read, query: &mut Iterator) -> Result { let mut decoder = reader; let n_elements: VarInt = Decodable::consensus_decode(&mut decoder).unwrap_or(VarInt(0)); - let ref mut reader = decoder; + let reader = &mut decoder; // map hashes to [0, n_elements << grp] let nm = n_elements.0 * self.m; let mut mapped = query.map(|e| map_to_range(self.filter.hash(e), nm)).collect::>(); diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 470e59e1..d66167ec 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -124,15 +124,15 @@ impl ChildNumber { /// Returns `true` if the child number is a [`Normal`] value. /// /// [`Normal`]: #variant.Normal - pub fn is_normal(&self) -> bool { + pub fn is_normal(self) -> bool { !self.is_hardened() } /// Returns `true` if the child number is a [`Hardened`] value. /// /// [`Hardened`]: #variant.Hardened - pub fn is_hardened(&self) -> bool { - match *self { + pub fn is_hardened(self) -> bool { + match self { ChildNumber::Hardened {..} => true, ChildNumber::Normal {..} => false, } @@ -548,7 +548,7 @@ impl ExtendedPubKey { i: ChildNumber, ) -> Result { let (sk, chain_code) = self.ckd_pub_tweak(i)?; - let mut pk = self.public_key.clone(); + let mut pk = self.public_key; pk.key.add_exp_assign(secp, &sk[..]).map_err(Error::Ecdsa)?; Ok(ExtendedPubKey { @@ -604,9 +604,9 @@ impl FromStr for ExtendedPrivKey { let cn_int: u32 = endian::slice_to_u32_be(&data[9..13]); let child_number: ChildNumber = ChildNumber::from(cn_int); - let network = if &data[0..4] == [0x04u8, 0x88, 0xAD, 0xE4] { + let network = if data[0..4] == [0x04u8, 0x88, 0xAD, 0xE4] { Network::Bitcoin - } else if &data[0..4] == [0x04u8, 0x35, 0x83, 0x94] { + } else if data[0..4] == [0x04u8, 0x35, 0x83, 0x94] { Network::Testnet } else { return Err(base58::Error::InvalidVersion((&data[0..4]).to_vec())); @@ -661,9 +661,9 @@ impl FromStr for ExtendedPubKey { let child_number: ChildNumber = ChildNumber::from(cn_int); Ok(ExtendedPubKey { - network: if &data[0..4] == [0x04u8, 0x88, 0xB2, 0x1E] { + network: if data[0..4] == [0x04u8, 0x88, 0xB2, 0x1E] { Network::Bitcoin - } else if &data[0..4] == [0x04u8, 0x35, 0x87, 0xCF] { + } else if data[0..4] == [0x04u8, 0x35, 0x87, 0xCF] { Network::Testnet } else { return Err(base58::Error::InvalidVersion((&data[0..4]).to_vec())); diff --git a/src/util/psbt/mod.rs b/src/util/psbt/mod.rs index d281f1ce..985039f8 100644 --- a/src/util/psbt/mod.rs +++ b/src/util/psbt/mod.rs @@ -67,8 +67,8 @@ impl PartiallySignedTransaction { let mut tx: Transaction = self.global.unsigned_tx; for (vin, psbtin) in tx.input.iter_mut().zip(self.inputs.into_iter()) { - vin.script_sig = psbtin.final_script_sig.unwrap_or_else(|| Script::new()); - vin.witness = psbtin.final_script_witness.unwrap_or_else(|| Vec::new()); + vin.script_sig = psbtin.final_script_sig.unwrap_or_else(Script::new); + vin.witness = psbtin.final_script_witness.unwrap_or_else(Vec::new); } tx