From b7e2af1b6b2ccb46b5ef14755e8150c64199e39a Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Thu, 20 Feb 2025 16:02:22 +0100 Subject: [PATCH] Implement `Arbitrary` for `&'a Script` The `Arbitrary` trait allows zero-copy implementations for borrowed types, so this adds the implementation for borrowed `Script`. --- primitives/src/script/borrowed.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/primitives/src/script/borrowed.rs b/primitives/src/script/borrowed.rs index 295f1eeda..a2ba0f84b 100644 --- a/primitives/src/script/borrowed.rs +++ b/primitives/src/script/borrowed.rs @@ -4,6 +4,9 @@ use core::ops::{ Bound, Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, }; +#[cfg(feature = "arbitrary")] +use arbitrary::{Arbitrary, Unstructured}; + use super::ScriptBuf; use crate::prelude::{Box, ToOwned, Vec}; @@ -132,6 +135,14 @@ impl Script { } } +#[cfg(feature = "arbitrary")] +impl<'a> Arbitrary<'a> for &'a Script { + fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result { + let v = <&'a [u8]>::arbitrary(u)?; + Ok(Script::from_bytes(v)) + } +} + macro_rules! delegate_index { ($($type:ty),* $(,)?) => { $(