Loosen trait bounds on HexPrimitive trait impls
What changed, and why it matters
This commit is a routine code cleanup. It relaxes unnecessary requirements on an internal helper type so that formatting a value as hexadecimal no longer requires the value to also be decodable. There is no security issue here.
No security action needed. Review as normal code-quality change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change removes the T: Encodable + Decodable bound from HexPrimitive and splits the impl blocks so that parsing requires Decodable while formatting traits (Display, Debug, LowerHex, UpperHex) only require Encodable. This is a pure API-ergonomics refactor with no behavioral change to encoding or decoding logic.
Changed components
primitives/src/hex_codec.rsInspect captured patch +9 / −6
diff --git a/primitives/src/hex_codec.rs b/primitives/src/hex_codec.rs
index b2a75771..274ba62a 100644
--- a/primitives/src/hex_codec.rs
+++ b/primitives/src/hex_codec.rs
@@ -20,7 +20,7 @@ use internals::write_err;
///
/// Implements `Display`, `Debug`, `LowerHex`, and `UpperHex` as well as an inherent `from_str`
/// method that returns `T`.
-pub(crate) struct HexPrimitive<'a, T: Encodable + Decodable>(pub(crate) &'a T);
+pub(crate) struct HexPrimitive<'a, T>(pub(crate) &'a T);
impl<'a, T: Encodable + Decodable> IntoIterator for &HexPrimitive<'a, T> {
type Item = u8;
@@ -29,7 +29,7 @@ impl<'a, T: Encodable + Decodable> IntoIterator for &HexPrimitive<'a, T> {
fn into_iter(self) -> Self::IntoIter { EncodableByteIter::new(self.0) }
}
-impl<T: Encodable + Decodable> HexPrimitive<'_, T> {
+impl<T: Decodable> HexPrimitive<'_, T> {
/// Parses a given string into an instance of the type `T`.
///
/// Since `FromStr` would return an instance of `Self` and thus a &T, this function
@@ -67,6 +67,9 @@ impl<T: Encodable + Decodable> HexPrimitive<'_, T> {
decoder.end().map_err(ParsePrimitiveError::Decode)
}
+}
+
+impl<T: Encodable> HexPrimitive<'_, T> {
/// Writes an Encodable object to the given formatter in the requested case.
#[inline]
@@ -131,22 +134,22 @@ impl<T: Encodable + Decodable> HexPrimitive<'_, T> {
}
}
-impl<T: Encodable + Decodable> fmt::Display for HexPrimitive<'_, T> {
+impl<T: Encodable> fmt::Display for HexPrimitive<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self, f) }
}
-impl<T: Encodable + Decodable> fmt::Debug for HexPrimitive<'_, T> {
+impl<T: Encodable> fmt::Debug for HexPrimitive<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self, f) }
}
-impl<T: Encodable + Decodable> fmt::LowerHex for HexPrimitive<'_, T> {
+impl<T: Encodable> fmt::LowerHex for HexPrimitive<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.fmt_hex(f, Case::Lower) }
}
-impl<T: Encodable + Decodable> fmt::UpperHex for HexPrimitive<'_, T> {
+impl<T: Encodable> fmt::UpperHex for HexPrimitive<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.fmt_hex(f, Case::Upper) }
}
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.