Skip to content

Commit b368384

Browse files
committed
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.
1 parent 32a42cb commit b368384

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

bitcoin/src/blockdata/script/owned.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,32 @@ impl ScriptBuf {
6464
/// Returns a mutable reference to unsized script.
6565
pub fn as_mut_script(&mut self) -> &mut Script { Script::from_bytes_mut(&mut self.0) }
6666

67+
/// Converts byte vector into script.
68+
///
69+
/// This method doesn't (re)allocate.
70+
pub fn from_bytes(bytes: Vec<u8>) -> Self { ScriptBuf(bytes) }
71+
72+
/// Converts the script into a byte vector.
73+
///
74+
/// This method doesn't (re)allocate.
75+
pub fn into_bytes(self) -> Vec<u8> { self.0 }
76+
77+
/// Converts this `ScriptBuf` into a [boxed](Box) [`Script`].
78+
///
79+
/// This method reallocates if the capacity is greater than length of the script but should not
80+
/// when they are equal. If you know beforehand that you need to create a script of exact size
81+
/// use [`reserve_exact`](Self::reserve_exact) before adding data to the script so that the
82+
/// reallocation can be avoided.
83+
#[must_use = "`self` will be dropped if the result is not used"]
84+
#[inline]
85+
pub fn into_boxed_script(self) -> Box<Script> {
86+
// Copied from PathBuf::into_boxed_path
87+
let rw = Box::into_raw(self.0.into_boxed_slice()) as *mut Script;
88+
unsafe { Box::from_raw(rw) }
89+
}
90+
}
91+
92+
impl ScriptBuf {
6793
/// Creates a new script builder
6894
pub fn builder() -> Builder { Builder::new() }
6995

@@ -78,16 +104,6 @@ impl ScriptBuf {
78104
Ok(ScriptBuf::from_bytes(v))
79105
}
80106

81-
/// Converts byte vector into script.
82-
///
83-
/// This method doesn't (re)allocate.
84-
pub fn from_bytes(bytes: Vec<u8>) -> Self { ScriptBuf(bytes) }
85-
86-
/// Converts the script into a byte vector.
87-
///
88-
/// This method doesn't (re)allocate.
89-
pub fn into_bytes(self) -> Vec<u8> { self.0 }
90-
91107
/// Adds a single opcode to the script.
92108
pub fn push_opcode(&mut self, data: Opcode) { self.0.push(data.to_u8()); }
93109

@@ -188,20 +204,6 @@ impl ScriptBuf {
188204
None => self.push_opcode(OP_VERIFY),
189205
}
190206
}
191-
192-
/// Converts this `ScriptBuf` into a [boxed](Box) [`Script`].
193-
///
194-
/// This method reallocates if the capacity is greater than length of the script but should not
195-
/// when they are equal. If you know beforehand that you need to create a script of exact size
196-
/// use [`reserve_exact`](Self::reserve_exact) before adding data to the script so that the
197-
/// reallocation can be avoided.
198-
#[must_use = "`self` will be dropped if the result is not used"]
199-
#[inline]
200-
pub fn into_boxed_script(self) -> Box<Script> {
201-
// Copied from PathBuf::into_boxed_path
202-
let rw = Box::into_raw(self.0.into_boxed_slice()) as *mut Script;
203-
unsafe { Box::from_raw(rw) }
204-
}
205207
}
206208

207209
impl<'a> core::iter::FromIterator<Instruction<'a>> for ScriptBuf {

0 commit comments

Comments
 (0)