// SPDX-License-Identifier: CC0-1.0 use core::ops::{Deref, DerefMut}; #[cfg(feature = "arbitrary")] use arbitrary::{Arbitrary, Unstructured}; use super::Script; use crate::prelude::{Box, Vec}; /// An owned, growable script. /// /// `ScriptBuf` is the most common script type that has the ownership over the contents of the /// script. It has a close relationship with its borrowed counterpart, [`Script`]. /// /// Just as other similar types, this implements [`Deref`], so [deref coercions] apply. Also note /// that all the safety/validity restrictions that apply to [`Script`] apply to `ScriptBuf` as well. /// /// [deref coercions]: https://doc.rust-lang.org/std/ops/trait.Deref.html#more-on-deref-coercion #[derive(Default, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)] pub struct ScriptBuf(Vec); impl ScriptBuf { /// Constructs a new empty script. #[inline] pub const fn new() -> Self { Self::from_bytes(Vec::new()) } /// Converts byte vector into script. /// /// 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) -> Self { Self(bytes) } /// Returns a reference to unsized script. #[inline] pub fn as_script(&self) -> &Script { Script::from_bytes(&self.0) } /// Returns a mutable reference to unsized script. #[inline] pub fn as_mut_script(&mut self) -> &mut Script { Script::from_bytes_mut(&mut self.0) } /// 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 { 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] #[inline] pub fn into_boxed_script(self) -> Box