primitives: Document script to/from bytes

The byte slice/vector in the to/from methods for the script types does
not include the length prefix that is used when consensus
encoding.

Add docs to make this more clear.
This commit is contained in:
Tobin C. Harding 2025-04-14 12:18:05 +10:00
parent e2149ec4e9
commit 35ca48a85c
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 13 additions and 2 deletions

View File

@ -90,14 +90,20 @@ impl Script {
pub const fn new() -> &'static Self { Self::from_bytes(&[]) }
/// Returns the script data as a byte slice.
///
/// This is just the script bytes **not** consensus encoding (which includes a length prefix).
#[inline]
pub const fn as_bytes(&self) -> &[u8] { &self.0 }
/// Returns the script data as a mutable byte slice.
///
/// This is just the script bytes **not** consensus encoding (which includes a length prefix).
#[inline]
pub fn as_mut_bytes(&mut self) -> &mut [u8] { &mut self.0 }
/// Returns a copy of the script data.
///
/// This is just the script bytes **not** consensus encoding (which includes a length prefix).
#[inline]
pub fn to_vec(&self) -> Vec<u8> { self.as_bytes().to_owned() }

View File

@ -27,7 +27,8 @@ impl ScriptBuf {
/// Converts byte vector into script.
///
/// This method doesn't (re)allocate.
/// This method doesn't (re)allocate. `bytes` is just the script bytes **not** consensus
/// encoding (i.e no length prefix).
#[inline]
pub const fn from_bytes(bytes: Vec<u8>) -> Self { Self(bytes) }
@ -42,6 +43,10 @@ impl ScriptBuf {
/// Converts the script into a byte vector.
///
/// This method doesn't (re)allocate.
///
/// # Returns
///
/// Just the script bytes **not** consensus encoding (which includes a length prefix).
#[inline]
pub fn into_bytes(self) -> Vec<u8> { self.0 }