From 5e3e7c1d7c20a8edd19960b9947644a99d5f57be Mon Sep 17 00:00:00 2001 From: "Tony Arcieri (iqlusion)" Date: Tue, 10 May 2022 17:15:25 -0700 Subject: [PATCH] bip32: use const panic for `Prefix::from_parts_unchecked` (#995) Now that we're MSRV 1.57, const panic can be used because it was stabilized in that release. --- bip32/src/prefix.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/bip32/src/prefix.rs b/bip32/src/prefix.rs index e8299a33..39df6400 100644 --- a/bip32/src/prefix.rs +++ b/bip32/src/prefix.rs @@ -6,15 +6,6 @@ use core::{ str, }; -/// Constant panicking assertion. -// TODO(tarcieri): use const panic when stable. -// See: https://github.com/rust-lang/rust/issues/51999 -macro_rules! const_assert { - ($bool:expr, $msg:expr) => { - [$msg][!$bool as usize] - }; -} - /// BIP32 extended key prefixes a.k.a. "versions" (e.g. `xpub`, `xprv`) /// /// The BIP32 spec describes these as "versions" and gives examples for @@ -62,15 +53,18 @@ impl Prefix { /// The main intended use case for this function is [`Prefix`] constants /// such as [`Prefix::XPRV`]. /// + /// # Warning + /// /// Use this function with care: No consistency check is performed! It is /// up to the caller to ensure that the version number matches the prefix. /// + /// # Panics + /// /// Panics if `s` is not 4 chars long, or any of the chars lie outside of /// the supported range: lower case (`a..=z`) or upper case (`A..=Z`) /// letters. pub const fn from_parts_unchecked(s: &str, version: Version) -> Self { - // TODO(tarcieri): return `Result` when const panic is stable - const_assert!(Self::validate_str(s).is_ok(), "invalid prefix"); + assert!(Self::validate_str(s).is_ok(), "invalid prefix"); let bytes = s.as_bytes(); let chars = [bytes[0], bytes[1], bytes[2], bytes[3]]; Self { chars, version }