Merge rust-bitcoin/rust-bitcoin#3640: address: Add `Address::into_unchecked`

073ff81536 address: Simplify `Address::assume_checked` impl (Max Fang)
e4cf8ebc20 address: Add `Address::into_unchecked` (Max Fang)

Pull request description:

  ## Commits

  ### address: Add `Address::into_unchecked`

  Adds an ergonomic way to convert any `Address` (network can be checked
  or unchecked) into an `Address<NetworkUnchecked>` without cloning, which
  I've found useful in several contexts.

  ### address: Simplify `Address::assume_checked` impl

  Removes an unnecessary `match` which I noticed while implementing `Address::into_unchecked`.

  ## Small note on use of `Self`

  The style guide in `CONTRIBUTING.md` notes to return `Self` when possible, but that doesn't apply here as the `Address` being returned is different from the `Address` type referred to by `Self`, due to the changed `NetworkValidation` type.

ACKs for top commit:
  tcharding:
    ACK 073ff81536
  apoelstra:
    ACK 073ff81536e7a24883d6470ecf3054f4b7263186; successfully ran local tests; nice!

Tree-SHA512: dd6749cffad75d88568106169032d8af023ed56df4b1b38fa613ae7b140d1eb22933e7b0ffc1b17681a5db4b11b28d9db4efb62868755ca3c0f7cac20de0be4b
This commit is contained in:
merge-script 2024-11-25 13:00:00 +00:00
commit 2a94004341
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 4 additions and 10 deletions

View File

@ -383,6 +383,9 @@ impl<V: NetworkValidation> Address<V> {
pub fn as_unchecked(&self) -> &Address<NetworkUnchecked> { pub fn as_unchecked(&self) -> &Address<NetworkUnchecked> {
unsafe { &*(self as *const Address<V> as *const Address<NetworkUnchecked>) } unsafe { &*(self as *const Address<V> as *const Address<NetworkUnchecked>) }
} }
/// Marks the network of this address as unchecked.
pub fn into_unchecked(self) -> Address<NetworkUnchecked> { Address(self.0, PhantomData) }
} }
/// Methods and functions that can be called only on `Address<NetworkChecked>`. /// Methods and functions that can be called only on `Address<NetworkChecked>`.
@ -792,16 +795,7 @@ impl Address<NetworkUnchecked> {
/// For details about this mechanism, see section [*Parsing addresses*](Address#parsing-addresses) /// For details about this mechanism, see section [*Parsing addresses*](Address#parsing-addresses)
/// on [`Address`]. /// on [`Address`].
#[inline] #[inline]
pub fn assume_checked(self) -> Address { pub fn assume_checked(self) -> Address { Address(self.0, PhantomData) }
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)
}
/// Parse a bech32 Address string /// Parse a bech32 Address string
pub fn from_bech32_str(s: &str) -> Result<Address<NetworkUnchecked>, Bech32Error> { pub fn from_bech32_str(s: &str) -> Result<Address<NetworkUnchecked>, Bech32Error> {