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.
This commit is contained in:
Tobin C. Harding 2024-02-05 18:15:59 +11:00
parent 975ada3570
commit ac88bc03fd
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
7 changed files with 25 additions and 8 deletions

View File

@ -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.
///

View File

@ -18,7 +18,8 @@ pub struct Builder(ScriptBuf, Option<Opcode>);
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() }

View File

@ -33,6 +33,7 @@ pub struct ScriptBuf(pub(in crate::blockdata::script) Vec<u8>);
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.

View File

@ -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)) }

View File

@ -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`.
///

View File

@ -27,7 +27,7 @@ use crate::{Script, VarInt};
/// saving some allocations.
///
/// [segwit upgrade]: <https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki>
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Witness {
/// Contains the witness `Vec<Vec<u8>>` 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<Vec<&[u8]>> 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};

View File

@ -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) }