forked from rust-bitcoin/rust-bech32
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement TryFrom various integer types
Currently we use `Error::InvalidData(u8)` to attempt to cache an invalid value encountered while converting to a `u8`. This is buggy because we cast to the `u8` to stash the value so we loose valuable bits. Implement `TryFrom` by doing: - Add an `Error::Overflow` variant. - Use `Error::Overflow` for conversion _outside_ of `TryFrom` impls - Add a new `TryFromError` and nest it in `Error::TryFrom` - Implement `TryFrom<T> for u5` impls for all standard integer types. Inspired by issue rust-bitcoin#60 and the stdlib macros of the same name.
- Loading branch information
Showing
2 changed files
with
149 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Written by the Rust Bitcoin developers. | ||
// SPDX-License-Identifier: CC0-1.0 | ||
|
||
// TODO: We can get this from `bitcoin-internals` crate once that is released. | ||
|
||
//! # Error | ||
//! | ||
//! Error handling macros and helpers. | ||
//! | ||
|
||
/// Formats error. | ||
/// | ||
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because | ||
/// `e.source()` is only available in std builds, without this macro the error source is lost for | ||
/// no-std builds. | ||
#[macro_export] | ||
macro_rules! write_err { | ||
($writer:expr, $string:literal $(, $args:expr)*; $source:expr) => { | ||
{ | ||
#[cfg(feature = "std")] | ||
{ | ||
let _ = &$source; // Prevents clippy warnings. | ||
write!($writer, $string $(, $args)*) | ||
} | ||
#[cfg(not(feature = "std"))] | ||
{ | ||
write!($writer, concat!($string, ": {}") $(, $args)*, $source) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters