p2p: refactor: use `take()` instead of `replace()`
What changed, and why it matters
This is a small code cleanup in the Bitcoin peer-to-peer message decoder. It replaces a more verbose pattern that swapped out an internal state object with a simpler one that just takes the current value. The behavior is unchanged because the replaced value was immediately overwritten by the next state transition anyway. There is no security issue here.
No action required. This is a non-security refactoring change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors V1NetworkMessageDecoder in p2p/src/message.rs. Previously, when the header decoder was ready, the code used core::mem::replace to swap self.state with a fresh ReadingHeader state, then destructured the old state to extract header_decoder. Since the next state transition immediately sets self.state to ReadPayload, the replacement value was never used. The refactor uses core::mem::take(header_decoder) to move the decoder out directly, removing 11 lines of dead-state-transition boilerplate. This is a pure refactor with no functional change.
Changed components
p2p/src/message.rsV1NetworkMessageDecoderInspect captured patch +2 / −13
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index c5b05411..ead07887 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -1357,19 +1357,8 @@ impl encoding::Decoder for V1NetworkMessageDecoder {
})?;
if status.is_ready() {
- // Header complete, extract values and transition to payload state.
- let old_state = core::mem::replace(
- &mut self.state,
- DecoderState::ReadingHeader {
- header_decoder: <V1MessageHeader as encoding::Decode>::decoder(),
- },
- );
-
- let DecoderState::ReadingHeader { header_decoder } = old_state else {
- unreachable!("we are in ReadingHeader state")
- };
-
- let header = header_decoder.end().map_err(|e| {
+ let decoder = core::mem::take(header_decoder);
+ let header = decoder.end().map_err(|e| {
V1NetworkMessageDecoderError(V1NetworkMessageDecoderErrorInner::Header(e))
})?;
let payload_len = usize::try_from(header.length)
Why this scored 13/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.