From 7c8601a69614df342826a52c0937e2beee1068d3 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sun, 11 Aug 2024 21:57:20 +0200 Subject: [PATCH] implement IndexMut for PushBytes --- bitcoin/src/blockdata/script/push_bytes.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/bitcoin/src/blockdata/script/push_bytes.rs b/bitcoin/src/blockdata/script/push_bytes.rs index 47c1e4d97..89cbdc268 100644 --- a/bitcoin/src/blockdata/script/push_bytes.rs +++ b/bitcoin/src/blockdata/script/push_bytes.rs @@ -15,7 +15,8 @@ pub use self::primitive::*; /// break invariants. Therefore auditing this module should be sufficient. mod primitive { use core::ops::{ - Bound, Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, + Bound, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, + RangeToInclusive, }; use super::PushBytesError; @@ -95,6 +96,17 @@ mod primitive { } } } + + impl IndexMut<$type> for PushBytes { + #[inline] + #[track_caller] + fn index_mut(&mut self, index: $type) -> &mut Self::Output { + // SAFETY: Slicing can not make slices longer. + unsafe { + Self::from_mut_slice_unchecked(&mut self.0[index]) + } + } + } )* } } @@ -117,6 +129,12 @@ mod primitive { fn index(&self, index: usize) -> &Self::Output { &self.0[index] } } + impl IndexMut for PushBytes { + #[inline] + #[track_caller] + fn index_mut(&mut self, index: usize) -> &mut Self::Output { &mut self.0[index] } + } + impl<'a> TryFrom<&'a [u8]> for &'a PushBytes { type Error = PushBytesError;