Implement `Arbitrary` for `&'a Script`

The `Arbitrary` trait allows zero-copy implementations for borrowed
types, so this adds the implementation for borrowed `Script`.
This commit is contained in:
Martin Habovstiak 2025-02-20 16:02:22 +01:00
parent bca2864084
commit b7e2af1b6b
1 changed files with 11 additions and 0 deletions

View File

@ -4,6 +4,9 @@ use core::ops::{
Bound, Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, Bound, Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
}; };
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
use super::ScriptBuf; use super::ScriptBuf;
use crate::prelude::{Box, ToOwned, Vec}; 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<Self> {
let v = <&'a [u8]>::arbitrary(u)?;
Ok(Script::from_bytes(v))
}
}
macro_rules! delegate_index { macro_rules! delegate_index {
($($type:ty),* $(,)?) => { ($($type:ty),* $(,)?) => {
$( $(