From e4cf8ebc208068ef2888e6e9e6f27f12cd07570f Mon Sep 17 00:00:00 2001 From: Max Fang Date: Mon, 18 Nov 2024 17:46:35 -0800 Subject: [PATCH 1/2] address: Add `Address::into_unchecked` Adds an ergonomic way to convert any `Address` (network can be checked or unchecked) into an `Address` without cloning, which I've found useful in several contexts. --- bitcoin/src/address/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index c7053f667..cc948ad1e 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -383,6 +383,9 @@ impl Address { pub fn as_unchecked(&self) -> &Address { unsafe { &*(self as *const Address as *const Address) } } + + /// Marks the network of this address as unchecked. + pub fn into_unchecked(self) -> Address { Address(self.0, PhantomData) } } /// Methods and functions that can be called only on `Address`. From 073ff81536e7a24883d6470ecf3054f4b7263186 Mon Sep 17 00:00:00 2001 From: Max Fang Date: Mon, 18 Nov 2024 17:52:11 -0800 Subject: [PATCH 2/2] address: Simplify `Address::assume_checked` impl Removes an unnecessary `match`. --- bitcoin/src/address/mod.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index cc948ad1e..1169e631a 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -795,16 +795,7 @@ impl Address { /// For details about this mechanism, see section [*Parsing addresses*](Address#parsing-addresses) /// on [`Address`]. #[inline] - pub fn assume_checked(self) -> Address { - use AddressInner::*; - - let inner = match self.0 { - P2pkh { hash, network } => P2pkh { hash, network }, - P2sh { hash, network } => P2sh { hash, network }, - Segwit { program, hrp } => Segwit { program, hrp }, - }; - Address(inner, PhantomData) - } + pub fn assume_checked(self) -> Address { Address(self.0, PhantomData) } /// Parse a bech32 Address string pub fn from_bech32_str(s: &str) -> Result, Bech32Error> {