Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve leaf errors #2530

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions bitcoin/src/blockdata/script/witness_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,14 @@ impl From<TryFromError> for TryFromInstructionError {

/// Error attempting to create a [`WitnessVersion`] from an integer.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TryFromError {
/// The invalid non-witness version integer.
pub invalid: u8,
invalid: u8,
}

impl TryFromError {
/// Returns the invalid non-witness version integer.
pub fn invalid_version(&self) -> u8 { self.invalid }
}

impl fmt::Display for TryFromError {
Expand All @@ -258,6 +262,4 @@ impl fmt::Display for TryFromError {
}

#[cfg(feature = "std")]
impl std::error::Error for TryFromError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}
impl std::error::Error for TryFromError {}
16 changes: 0 additions & 16 deletions bitcoin/src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,22 +723,6 @@ impl<T: Into<u128>> From<T> for U256 {
fn from(x: T) -> Self { U256(0, x.into()) }
}

/// Error from `TryFrom<signed type>` implementations, occurs when input is negative.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct TryFromError(i128);

impl fmt::Display for TryFromError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "attempt to create unsigned integer type from negative number: {}", self.0)
}
}

#[cfg(feature = "std")]
impl std::error::Error for TryFromError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}

Kixunil marked this conversation as resolved.
Show resolved Hide resolved
impl Add for U256 {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Expand Down
13 changes: 9 additions & 4 deletions hashes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,22 +234,27 @@ pub trait Hash:

/// Attempted to create a hash from an invalid length slice.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FromSliceError {
expected: usize,
got: usize,
}

impl FromSliceError {
/// Returns the expected slice length.
pub fn expected_length(&self) -> usize { self.expected }

/// Returns the invalid slice length.
pub fn invalid_length(&self) -> usize { self.got }
}

impl fmt::Display for FromSliceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid slice length {} (expected {})", self.got, self.expected)
}
}

#[cfg(feature = "std")]
impl std::error::Error for FromSliceError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}
impl std::error::Error for FromSliceError {}

#[cfg(test)]
mod tests {
Expand Down