Skip to content

Commit 144adfc

Browse files
committed
consensus_encoding: remove Transitioning state
Simplify the Decoder2State by dropping the unnecessary Transitioning variant. We can just use the Errored variant and not change the state of the Decoder2 if the first decoder fails while transitioning to the second.
1 parent 56d844d commit 144adfc

File tree

1 file changed

+9
-33
lines changed

1 file changed

+9
-33
lines changed

consensus_encoding/src/decode/decoders.rs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -295,26 +295,6 @@ enum Decoder2State<A: Decoder, B: Decoder> {
295295
Second(A::Output, B),
296296
/// Decoder has failed and cannot be used again.
297297
Errored,
298-
/// Temporary state during transitions from First to Second, should never be observed.
299-
Transitioning,
300-
}
301-
302-
impl<A: Decoder, B: Decoder> Decoder2State<A, B> {
303-
/// Transitions from the first state to second by extracting both decoders.
304-
///
305-
/// We use `mem::replace` to atomically swap the entire state, giving us
306-
/// ownership of the decoders so we can consume the first decoder while
307-
/// holding a mutable reference to the state.
308-
///
309-
/// If this method is called when not in the `First` state, we panic
310-
/// with `#[track_caller]` to show where the bug occurred.
311-
#[track_caller]
312-
fn transition(&mut self) -> (A, B) {
313-
match mem::replace(self, Decoder2State::Transitioning) {
314-
Decoder2State::First(first, second) => (first, second),
315-
_ => panic!("transition called on invalid state"),
316-
}
317-
}
318298
}
319299

320300
impl<A, B, Err> Decoder2<A, B, Err>
@@ -347,12 +327,15 @@ where
347327
}
348328

349329
// First decoder is complete, transition to second.
350-
let (first, second) = self.state.transition();
351-
let first_result = first.end().map_err(|error| {
352-
self.state = Decoder2State::Errored;
353-
Err::from(error)
354-
})?;
355-
self.state = Decoder2State::Second(first_result, second);
330+
// If the first decoder fails, the composite decoder
331+
// remains in an Errored state.
332+
match mem::replace(&mut self.state, Decoder2State::Errored) {
333+
Decoder2State::First(first, second) => {
334+
let first_result = first.end()?;
335+
self.state = Decoder2State::Second(first_result, second);
336+
}
337+
_ => unreachable!("we know we're in First state"),
338+
}
356339
}
357340
Decoder2State::Second(_, second_decoder) => {
358341
return second_decoder.push_bytes(bytes).map_err(|error| {
@@ -363,9 +346,6 @@ where
363346
Decoder2State::Errored => {
364347
panic!("use of failed decoder");
365348
}
366-
Decoder2State::Transitioning => {
367-
panic!("use of decoder in transitioning state");
368-
}
369349
}
370350
}
371351
}
@@ -388,9 +368,6 @@ where
388368
Decoder2State::Errored => {
389369
panic!("use of failed decoder");
390370
}
391-
Decoder2State::Transitioning => {
392-
panic!("use of decoder in transitioning state");
393-
}
394371
}
395372
}
396373

@@ -401,7 +378,6 @@ where
401378
first_decoder.read_limit() + second_decoder.read_limit(),
402379
Decoder2State::Second(_, second_decoder) => second_decoder.read_limit(),
403380
Decoder2State::Errored => 0,
404-
Decoder2State::Transitioning => 0,
405381
}
406382
}
407383
}

0 commit comments

Comments
 (0)