diff --git a/bitcoin/src/blockdata/script/builder.rs b/bitcoin/src/blockdata/script/builder.rs index 855d04c61..ec842f7c9 100644 --- a/bitcoin/src/blockdata/script/builder.rs +++ b/bitcoin/src/blockdata/script/builder.rs @@ -21,6 +21,10 @@ impl Builder { #[inline] pub const fn new() -> Self { Builder(ScriptBuf::new(), None) } + /// Constructs a new empty script builder with at least the specified capacity. + #[inline] + pub fn with_capacity(capacity: usize) -> Self { Builder(ScriptBuf::with_capacity(capacity), None) } + /// Returns the length in bytes of the script. pub fn len(&self) -> usize { self.0.len() } diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs index ce458df01..b1bfae4e0 100644 --- a/bitcoin/src/blockdata/script/tests.rs +++ b/bitcoin/src/blockdata/script/tests.rs @@ -218,6 +218,13 @@ fn script_builder() { ); } +#[test] +fn script_builder_with_capacity() { + let script = Builder::with_capacity(42); + + assert!(script.into_script().capacity() >= 42); +} + #[test] fn script_generators() { let pubkey = "0234e6a79c5359c613762d537e0e19d86c77c1666d8c9ab050f23acd198e97f93e" diff --git a/primitives/src/script/owned.rs b/primitives/src/script/owned.rs index b76d841d4..70a3cbb87 100644 --- a/primitives/src/script/owned.rs +++ b/primitives/src/script/owned.rs @@ -71,7 +71,7 @@ impl ScriptBuf { Script::from_boxed_bytes(self.into_bytes().into_boxed_slice()) } - /// Constructs a new empty script with pre-allocated capacity. + /// Constructs a new empty script with at least the specified capacity. #[inline] pub fn with_capacity(capacity: usize) -> Self { ScriptBuf::from_bytes(Vec::with_capacity(capacity))