Merge rust-bitcoin/rust-bitcoin#4338: primitives: Document script to/from bytes

35ca48a85c primitives: Document script to/from bytes (Tobin C. Harding)

Pull request description:

  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.

ACKs for top commit:
  apoelstra:
    ACK 35ca48a85cd90fc9c1b13da439177a4772c1c330; successfully ran local tests; yeah, I like this

Tree-SHA512: 6efeddfd8fe9e5bb32a6d40ca1a0fa3aa686047ad9f4cb865e3c70f8339cd39c18daa01441a1a5898300b1e73150b39c53d507af528830bbd68904148b65ef35
This commit is contained in:
merge-script 2025-04-16 15:08:34 +00:00
commit 1134abce97
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
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.
#[inline]
///
/// 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 }