-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #92: Implement
TryFrom<T> for u5
for all standard integer types
25462c9 Implement TryFrom various integer types (Tobin C. Harding) 7ab12fc Use Error variants locally (Tobin C. Harding) Pull request description: Patch 1 is preparatory clean up. From the commit log of patch 2: 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 #60 and the stdlib macros of the same name. Please note, there is expected error work to come soon on this crate, can we keep perfect error code out of scope for this PR please (obviously if it has gaping problems then yell at me)? Note that the same derives are used here as for `Error` even though I think they are wrong. Done as part of stabilising (#60). ACKs for top commit: apoelstra: ACK 25462c9 Tree-SHA512: 830c6d0e1a8fa8badc983497b9eb6f0e38f0d81dc4f33dd5cd882d3e34836cfca277b4b1af9d98ea26e8cc0e53df9e708d8bc7d84757104cd05054fcbec169e0
- Loading branch information
Showing
2 changed files
with
158 additions
and
25 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