What changed, and why it matters
This is a routine internal code cleanup in the Rust Bitcoin library. It adds a new public constant function `Opcode::from_u8` so that Bitcoin script opcodes can be created from raw byte values inside constant contexts (for example, at compile time). The existing `From<u8>` conversion is rewritten to use this new function. There is no security-relevant change: the behavior is identical, no new data is accepted, and no existing checks are removed.
No security action required. Review as normal code-quality/refactoring change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors opcode construction in bitcoin/src/blockdata/opcodes.rs. It introduces pub const fn from_u8(b: u8) -> Self on Opcode and updates the macro-generated opcode constants and the From<u8> implementation to call it. This enables const construction of Opcode values outside the opcodes module, which is a prerequisite for moving the type into the primitives crate. The function is a direct, unchecked constructor identical to the previous Opcode { code: b } expression.
Changed components
bitcoin/src/blockdata/opcodes.rsInspect captured patch +6 / −2
diff --git a/bitcoin/src/blockdata/opcodes.rs b/bitcoin/src/blockdata/opcodes.rs
index bee85901..65ba964f 100644
--- a/bitcoin/src/blockdata/opcodes.rs
+++ b/bitcoin/src/blockdata/opcodes.rs
@@ -42,7 +42,7 @@ macro_rules! all_opcodes {
use super::Opcode;
$(
#[doc = $doc]
- pub const $op: Opcode = Opcode { code: $val};
+ pub const $op: Opcode = Opcode::from_u8($val);
)*
/// Helper function for as_str in OpcodeExt.
@@ -404,11 +404,15 @@ impl Opcode {
/// Encodes [`Opcode`] as a byte.
#[inline]
pub const fn to_u8(self) -> u8 { self.code }
+
+ /// Constructs an [`Opcode`] from a byte.
+ #[inline]
+ pub const fn from_u8(b: u8) -> Self { Self { code: b } }
}
impl From<u8> for Opcode {
#[inline]
- fn from(b: u8) -> Self { Self { code: b } }
+ fn from(b: u8) -> Self { Self::from_u8(b) }
}
impl fmt::Debug for Opcode {
Why this scored 15/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.