Skip to content

Commit 87d274b

Browse files
committed
Merge rust-bitcoin#5034: Remove current index from SliceEncoder
4c197ea Remove current index from SliceEncoder (Tobin C. Harding) Pull request description: At one stage during development, IIRC, I was trying to mutate the encoder from within a loop and was unable to do so so I added a current index instead. With Andrew's later improved version of `advance` this was no longer the case but I left the `cur_idx` variable in there. Remove it. This was noticed during review but kindly left for a follow up since its an internal change. This is a follow up to rust-bitcoin#4982 ACKs for top commit: apoelstra: ACK 4c197ea; successfully ran local tests Tree-SHA512: 1f47b3a0fff8bec58fbf859d4f66720198959b477852541b80138c336cf11646424a2f16cd7c3aacab6aaab3f8619ce2ae4d97abe41be6cd219ce7c7f436045d
2 parents c6bdbe6 + 4c197ea commit 87d274b

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

consensus_encoding/src/encode/encoders.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,10 @@ impl<const N: usize> Encoder for ArrayEncoder<N> {
7979
/// An encoder for a list of encodable types.
8080
pub struct SliceEncoder<'e, T: Encodable> {
8181
/// The list of references to the objects we are encoding.
82-
///
83-
/// This is **never** mutated. All accesses are done by array accesses because
84-
/// of lifetimes and the borrow checker.
8582
sl: &'e [T],
8683
/// The length prefix.
8784
compact_size: Option<ArrayVec<u8, SIZE>>,
88-
/// Index into `sl` of the element we are currently encoding.
89-
cur_idx: usize,
90-
/// Current encoder (for `sl[self.cur_idx]`).
85+
/// Encoder for the current object being encoded.
9186
cur_enc: Option<T::Encoder<'e>>,
9287
}
9388

@@ -100,7 +95,7 @@ impl<'e, T: Encodable> SliceEncoder<'e, T> {
10095
// In this `map` call we cannot remove the closure. Seems to be a bug in the compiler.
10196
// Perhaps https://github.com/rust-lang/rust/issues/102540 which is 3 years old with
10297
// no replies or even an acknowledgement. We will not bother filing our own issue.
103-
Self { sl, compact_size, cur_idx: 0, cur_enc: sl.first().map(|x| T::encoder(x)) }
98+
Self { sl, compact_size, cur_enc: sl.first().map(|x| T::encoder(x)) }
10499
}
105100
}
106101

@@ -123,19 +118,20 @@ impl<'e, T: Encodable> Encoder for SliceEncoder<'e, T> {
123118
loop {
124119
if self.compact_size.is_some() {
125120
// On the first call to advance(), just mark the compact_size as already
126-
// yielded and leave self.cur_idx at 0.
121+
// yielded and leave self.sl alone.
127122
self.compact_size = None;
128123
} else {
129124
// On subsequent calls, attempt to advance the current encoder and return
130125
// success if this succeeds.
131126
if cur.advance() {
132127
return true;
133128
}
134-
self.cur_idx += 1;
129+
// self.sl guaranteed to be non-empty if cur is non-None.
130+
self.sl = &self.sl[1..];
135131
}
136132

137133
// If advancing the current encoder failed, attempt to move to the next encoder.
138-
if let Some(x) = self.sl.get(self.cur_idx) {
134+
if let Some(x) = self.sl.first() {
139135
*cur = x.encoder();
140136
if cur.current_chunk().is_some() {
141137
return true;

0 commit comments

Comments
 (0)