units: Move pow module errors to error submodule
What changed, and why it matters
This commit is a routine internal code reorganization. It moves an error type (CompactTargetDecoderError) from one place in the source file to a new 'error' submodule and re-exports it so existing code keeps working. There is no functional change to how the library behaves, and no security issue is present.
No action required. This is a non-security refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors the units::pow module to follow the project’s convention of placing error types in an error submodule. It creates units::pow::error, moves CompactTargetDecoderError there, changes its inner field visibility to pub(super), and adds a re-export. It also adds a re-export in bitcoin::pow. The logic, trait implementations, and public API remain unchanged.
Changed components
units/src/pow.rsbitcoin/src/pow.rsInspect captured patch +35 / −21
diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs
index fcb73465..6903825e 100644
--- a/bitcoin/src/pow.rs
+++ b/bitcoin/src/pow.rs
@@ -20,6 +20,8 @@ use crate::network::Params;
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
pub use primitives::CompactTarget;
+#[doc(inline)]
+pub use units::pow::error;
/// Implement traits and methods shared by `Target` and `Work`.
macro_rules! do_impl {
diff --git a/units/src/pow.rs b/units/src/pow.rs
index 32815a4e..53403819 100644
--- a/units/src/pow.rs
+++ b/units/src/pow.rs
@@ -2,19 +2,20 @@
//! Proof-of-work related integer types.
-#[cfg(feature = "encoding")]
-use core::convert::Infallible;
use core::fmt;
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
-#[cfg(feature = "encoding")]
-use internals::write_err;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::parse_int::{self, PrefixedHexError, UnprefixedHexError};
+#[rustfmt::skip] // Keep public re-exports separate.
+#[cfg(feature = "encoding")]
+#[doc(no_inline)]
+pub use self::error::CompactTargetDecoderError;
+
/// Encoding of 256-bit target as 32-bit float.
///
/// This is used to encode a target into the block header. Satoshi made this part of consensus code
@@ -143,27 +144,38 @@ impl encoding::Decodable for CompactTarget {
fn decoder() -> Self::Decoder { CompactTargetDecoder(encoding::ArrayDecoder::<4>::new()) }
}
-/// An error consensus decoding an `CompactTarget`.
-#[derive(Debug, Clone, PartialEq, Eq)]
-#[cfg(feature = "encoding")]
-pub struct CompactTargetDecoderError(encoding::UnexpectedEofError);
+/// Error types for proof-of-work related integer types.
+pub mod error {
+ #[cfg(feature = "encoding")]
+ use core::convert::Infallible;
+ #[cfg(feature = "encoding")]
+ use core::fmt;
-#[cfg(feature = "encoding")]
-impl From<Infallible> for CompactTargetDecoderError {
- fn from(never: Infallible) -> Self { match never {} }
-}
+ #[cfg(feature = "encoding")]
+ use internals::write_err;
-#[cfg(feature = "encoding")]
-impl fmt::Display for CompactTargetDecoderError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write_err!(f, "compact target decoder error"; self.0)
+ /// An error consensus decoding an `CompactTarget`.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[cfg(feature = "encoding")]
+ pub struct CompactTargetDecoderError(pub(super) encoding::UnexpectedEofError);
+
+ #[cfg(feature = "encoding")]
+ impl From<Infallible> for CompactTargetDecoderError {
+ fn from(never: Infallible) -> Self { match never {} }
}
-}
-#[cfg(feature = "std")]
-#[cfg(feature = "encoding")]
-impl std::error::Error for CompactTargetDecoderError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ #[cfg(feature = "encoding")]
+ impl fmt::Display for CompactTargetDecoderError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write_err!(f, "compact target decoder error"; self.0)
+ }
+ }
+
+ #[cfg(feature = "std")]
+ #[cfg(feature = "encoding")]
+ impl std::error::Error for CompactTargetDecoderError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ }
}
#[cfg(feature = "arbitrary")]
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.