Add From<Opcode> for u8 and roundtrip test
What changed, and why it matters
This commit adds a harmless convenience conversion that lets an Opcode be turned into a u8 number, matching the existing conversion from u8 to Opcode. It also adds a test to confirm the conversions work both ways. There is no security issue here.
No action needed; this is a benign API and test addition.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change implements From
Changed components
primitives/src/opcodes.rsInspect captured patch +21 / −0
diff --git a/primitives/src/opcodes.rs b/primitives/src/opcodes.rs
index 19b94633..4f676804 100644
--- a/primitives/src/opcodes.rs
+++ b/primitives/src/opcodes.rs
@@ -42,6 +42,11 @@ impl From<u8> for Opcode {
fn from(b: u8) -> Self { Self::from_u8(b) }
}
+impl From<Opcode> for u8 {
+ #[inline]
+ fn from(op: Opcode) -> Self { op.to_u8() }
+}
+
macro_rules! all_opcodes {
($($op:ident => $val:expr, $doc:expr);* $(;)?) => {
/// Enables wildcard imports to bring into scope all opcodes and nothing else.
@@ -257,3 +262,19 @@ pub(crate) fn fmt_opcode(op: u8, f: &mut fmt::Formatter) -> fmt::Result {
0xff => f.write_str("OP_INVALIDOPCODE"),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn opcode_to_u8_from_u8_roundtrip() {
+ for b in 0..=u8::MAX {
+ let op = Opcode::from_u8(b);
+ assert_eq!(op.to_u8(), b);
+
+ let op = Opcode::from(b);
+ assert_eq!(u8::from(op), b);
+ }
+ }
+}
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.