From b368384317ba32f8714b92262d9091e4b0d25604 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 13 Aug 2024 06:23:28 +1000 Subject: [PATCH 1/8] Separate ScriptBuf POD methods In preparation for moving the `ScriptBuf` as a plain old datatype to `primitives`; separate the POD methods into their own impl block. Refactor only, no logic changes. --- bitcoin/src/blockdata/script/owned.rs | 50 ++++++++++++++------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/bitcoin/src/blockdata/script/owned.rs b/bitcoin/src/blockdata/script/owned.rs index a6d283729..43f645e40 100644 --- a/bitcoin/src/blockdata/script/owned.rs +++ b/bitcoin/src/blockdata/script/owned.rs @@ -64,6 +64,32 @@ impl ScriptBuf { /// Returns a mutable reference to unsized script. pub fn as_mut_script(&mut self) -> &mut Script { Script::from_bytes_mut(&mut self.0) } + /// Converts byte vector into script. + /// + /// This method doesn't (re)allocate. + pub fn from_bytes(bytes: Vec) -> Self { ScriptBuf(bytes) } + + /// Converts the script into a byte vector. + /// + /// This method doesn't (re)allocate. + pub fn into_bytes(self) -> Vec { self.0 } + + /// Converts this `ScriptBuf` into a [boxed](Box) [`Script`]. + /// + /// This method reallocates if the capacity is greater than length of the script but should not + /// when they are equal. If you know beforehand that you need to create a script of exact size + /// use [`reserve_exact`](Self::reserve_exact) before adding data to the script so that the + /// reallocation can be avoided. + #[must_use = "`self` will be dropped if the result is not used"] + #[inline] + pub fn into_boxed_script(self) -> Box