Test consensus encoding error API traits
What changed, and why it matters
This commit only adds new automated tests that check error types implement certain standard Rust traits (Display and From<Infallible>). It does not change any production code, fix a bug, or alter behavior. There is no security relevance.
No action required; this is a benign test-only commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds two compile-time test functions in consensus_encoding/tests/api.rs: api_all_error_types_implement_display() and api_all_error_types_implement_from_infallible(). These use generic helper functions to assert that various error types in the bitcoin-consensus-encoding crate implement fmt::Display and From
Changed components
consensus_encoding/tests/api.rsInspect captured patch +35 / −1
diff --git a/consensus_encoding/tests/api.rs b/consensus_encoding/tests/api.rs
index 3fd580ff..a47c1671 100644
--- a/consensus_encoding/tests/api.rs
+++ b/consensus_encoding/tests/api.rs
@@ -9,7 +9,7 @@
#![allow(dead_code)]
#![allow(unused_imports)]
-use core::fmt;
+use core::{convert::Infallible, fmt};
use bitcoin_consensus_encoding::{
self as encoding, encoder_newtype, ArrayDecoder, ArrayEncoder, ArrayRefEncoder, BytesEncoder,
@@ -229,6 +229,40 @@ fn all_types_implement_send_sync() {
assert_sync::<Errors>();
}
+#[test]
+fn api_all_error_types_implement_display() {
+ fn assert_display<T: fmt::Display>() {}
+
+ #[cfg(feature = "std")]
+ assert_display::<ReadError<UnexpectedEofError>>();
+ assert_display::<DecodeError<UnexpectedEofError>>();
+ assert_display::<UnconsumedError>();
+ assert_display::<CompactSizeDecoderError>();
+ #[cfg(feature = "alloc")]
+ assert_display::<LengthPrefixExceedsMaxError>();
+ #[cfg(feature = "alloc")]
+ assert_display::<ByteVecDecoderError>();
+ #[cfg(feature = "alloc")]
+ assert_display::<VecDecoderError<UnexpectedEofError>>();
+ assert_display::<UnexpectedEofError>();
+}
+
+#[test]
+fn api_all_error_types_implement_from_infallible() {
+ fn assert_from_infallible<T: From<Infallible>>() {}
+
+ assert_from_infallible::<DecodeError<UnexpectedEofError>>();
+ assert_from_infallible::<UnconsumedError>();
+ assert_from_infallible::<CompactSizeDecoderError>();
+ #[cfg(feature = "alloc")]
+ assert_from_infallible::<LengthPrefixExceedsMaxError>();
+ #[cfg(feature = "alloc")]
+ assert_from_infallible::<ByteVecDecoderError>();
+ #[cfg(feature = "alloc")]
+ assert_from_infallible::<VecDecoderError<UnexpectedEofError>>();
+ assert_from_infallible::<UnexpectedEofError>();
+}
+
#[test]
fn dyn_compatible() {
// If this builds then traits are dyn compatible.
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.