From ac88bc03fda57f55e39e59a300a20dc364173b39 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 5 Feb 2024 18:15:59 +1100 Subject: [PATCH] Make constructors const Audit the codebase for any function that starts with `/// Creates` and see if we can make it const. Inline them at the same time. --- bitcoin/src/blockdata/block.rs | 3 ++- bitcoin/src/blockdata/script/builder.rs | 3 ++- bitcoin/src/blockdata/script/owned.rs | 1 + bitcoin/src/blockdata/script/push_bytes.rs | 3 ++- bitcoin/src/blockdata/transaction.rs | 2 +- bitcoin/src/blockdata/witness.rs | 15 +++++++++++++-- hashes/src/siphash24.rs | 6 ++++-- 7 files changed, 25 insertions(+), 8 deletions(-) diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 53155904..49ea80f5 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -169,7 +169,8 @@ impl Version { /// Creates a [`Version`] from a signed 32 bit integer value. /// /// This is the data type used in consensus code in Bitcoin Core. - pub fn from_consensus(v: i32) -> Self { Version(v) } + #[inline] + pub const fn from_consensus(v: i32) -> Self { Version(v) } /// Returns the inner `i32` value. /// diff --git a/bitcoin/src/blockdata/script/builder.rs b/bitcoin/src/blockdata/script/builder.rs index e5b997fb..8d038152 100644 --- a/bitcoin/src/blockdata/script/builder.rs +++ b/bitcoin/src/blockdata/script/builder.rs @@ -18,7 +18,8 @@ pub struct Builder(ScriptBuf, Option); impl Builder { /// Creates a new empty script. - pub fn new() -> Self { Builder(ScriptBuf::new(), None) } + #[inline] + pub const fn new() -> Self { Builder(ScriptBuf::new(), None) } /// Returns the length in bytes of the script. pub fn len(&self) -> usize { self.0.len() } diff --git a/bitcoin/src/blockdata/script/owned.rs b/bitcoin/src/blockdata/script/owned.rs index 22ecf716..66aab19c 100644 --- a/bitcoin/src/blockdata/script/owned.rs +++ b/bitcoin/src/blockdata/script/owned.rs @@ -33,6 +33,7 @@ pub struct ScriptBuf(pub(in crate::blockdata::script) Vec); impl ScriptBuf { /// Creates a new empty script. + #[inline] pub const fn new() -> Self { ScriptBuf(Vec::new()) } /// Creates a new empty script with pre-allocated capacity. diff --git a/bitcoin/src/blockdata/script/push_bytes.rs b/bitcoin/src/blockdata/script/push_bytes.rs index 51d3f37e..5b066711 100644 --- a/bitcoin/src/blockdata/script/push_bytes.rs +++ b/bitcoin/src/blockdata/script/push_bytes.rs @@ -194,7 +194,8 @@ mod primitive { impl PushBytesBuf { /// Creates a new empty `PushBytesBuf`. - pub fn new() -> Self { PushBytesBuf(Vec::new()) } + #[inline] + pub const fn new() -> Self { PushBytesBuf(Vec::new()) } /// Creates a new empty `PushBytesBuf` with reserved capacity. pub fn with_capacity(capacity: usize) -> Self { PushBytesBuf(Vec::with_capacity(capacity)) } diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 985d2454..7674824c 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -78,7 +78,7 @@ impl OutPoint { /// Creates a new [`OutPoint`]. #[inline] - pub fn new(txid: Txid, vout: u32) -> OutPoint { OutPoint { txid, vout } } + pub const fn new(txid: Txid, vout: u32) -> OutPoint { OutPoint { txid, vout } } /// Creates a "null" `OutPoint`. /// diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs index 91d0abdd..2974de43 100644 --- a/bitcoin/src/blockdata/witness.rs +++ b/bitcoin/src/blockdata/witness.rs @@ -27,7 +27,7 @@ use crate::{Script, VarInt}; /// saving some allocations. /// /// [segwit upgrade]: -#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Witness { /// Contains the witness `Vec>` serialization without the initial varint indicating the /// number of elements (which is stored in `witness_elements`). @@ -233,7 +233,14 @@ impl Encodable for Witness { impl Witness { /// Creates a new empty [`Witness`]. - pub fn new() -> Self { Witness::default() } + #[inline] + pub const fn new() -> Self { + Witness { + content: Vec::new(), + witness_elements: 0, + indices_start: 0, + } + } /// Creates a witness required to spend a P2WPKH output. /// @@ -543,6 +550,10 @@ impl From> for Witness { fn from(vec: Vec<&[u8]>) -> Self { Witness::from_slice(&vec) } } +impl Default for Witness { + fn default() -> Self { Self::new() } +} + #[cfg(test)] mod test { use hex::{test_hex_unwrap as hex}; diff --git a/hashes/src/siphash24.rs b/hashes/src/siphash24.rs index c81335de..a20c1d33 100644 --- a/hashes/src/siphash24.rs +++ b/hashes/src/siphash24.rs @@ -90,7 +90,8 @@ pub struct HashEngine { impl HashEngine { /// Creates a new SipHash24 engine with keys. - pub fn with_keys(k0: u64, k1: u64) -> HashEngine { + #[inline] + pub const fn with_keys(k0: u64, k1: u64) -> HashEngine { HashEngine { k0, k1, @@ -107,7 +108,8 @@ impl HashEngine { } /// Creates a new SipHash24 engine. - pub fn new() -> HashEngine { HashEngine::with_keys(0, 0) } + #[inline] + pub const fn new() -> HashEngine { HashEngine::with_keys(0, 0) } /// Retrieves the keys of this engine. pub fn keys(&self) -> (u64, u64) { (self.k0, self.k1) }