Skip to content

Commit 3051b3e

Browse files
committed
primitives: Refactor WitnessEncoder to use CompactSizeEncoder
The current `WitnessEncoder` contains custom compact size encoding logic. We simplify the implementation by using the new `CompactSizeEncoder` + `Encoder2` composition
1 parent 982a805 commit 3051b3e

File tree

1 file changed

+8
-33
lines changed

1 file changed

+8
-33
lines changed

primitives/src/witness.rs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ use core::ops::Index;
99

1010
#[cfg(feature = "arbitrary")]
1111
use arbitrary::{Arbitrary, Unstructured};
12-
use encoding::{Encodable, Encoder};
12+
use encoding::{BytesEncoder, CompactSizeEncoder, Encodable, Encoder, Encoder2};
1313
#[cfg(feature = "hex")]
1414
use hex::{error::HexToBytesError, FromHex};
15-
use internals::array_vec::ArrayVec;
1615
use internals::compact_size;
1716
use internals::slice::SliceExt;
1817
use internals::wrap_debug::WrapDebug;
@@ -262,19 +261,8 @@ fn decode_cursor(bytes: &[u8], start_of_indices: usize, index: usize) -> Option<
262261
bytes.get_array::<4>(start).map(|index_bytes| u32::from_ne_bytes(*index_bytes) as usize)
263262
}
264263

265-
/// The maximum length of a compact size encoding.
266-
const SIZE: usize = compact_size::MAX_ENCODING_SIZE;
267-
268264
/// The encoder for the [`Witness`] type.
269-
// This is basically an exact copy of the `encoding::BytesEncoder` except we prefix
270-
// with the number of witness elements not the byte slice length.
271-
pub struct WitnessEncoder<'a> {
272-
/// A slice of all the elements without the initial length prefix
273-
/// but with the length prefix on each element.
274-
witness_elements: Option<&'a [u8]>,
275-
/// Encoding of the number of witness elements.
276-
num_elements: Option<ArrayVec<u8, SIZE>>,
277-
}
265+
pub struct WitnessEncoder<'a>(Encoder2<CompactSizeEncoder, BytesEncoder<'a>>);
278266

279267
impl Encodable for Witness {
280268
type Encoder<'a>
@@ -283,33 +271,20 @@ impl Encodable for Witness {
283271
Self: 'a;
284272

285273
fn encoder(&self) -> Self::Encoder<'_> {
286-
let num_elements = Some(compact_size::encode(self.len()));
287-
let witness_elements = Some(&self.content[..self.indices_start]);
274+
let num_elements = CompactSizeEncoder::new(self.len());
275+
let witness_elements =
276+
BytesEncoder::without_length_prefix(&self.content[..self.indices_start]);
288277

289-
WitnessEncoder { witness_elements, num_elements }
278+
WitnessEncoder(Encoder2::new(num_elements, witness_elements))
290279
}
291280
}
292281

293282
impl<'a> Encoder for WitnessEncoder<'a> {
294283
#[inline]
295-
fn current_chunk(&self) -> Option<&[u8]> {
296-
if let Some(num_elements) = self.num_elements.as_ref() {
297-
Some(num_elements)
298-
} else {
299-
self.witness_elements
300-
}
301-
}
284+
fn current_chunk(&self) -> Option<&[u8]> { self.0.current_chunk() }
302285

303286
#[inline]
304-
fn advance(&mut self) -> bool {
305-
if self.num_elements.is_some() {
306-
self.num_elements = None;
307-
true
308-
} else {
309-
self.witness_elements = None;
310-
false
311-
}
312-
}
287+
fn advance(&mut self) -> bool { self.0.advance() }
313288
}
314289

315290
// Note: we use `Borrow` in the following `PartialEq` impls specifically because of its additional

0 commit comments

Comments
 (0)