Skip to content

Commit e02a915

Browse files
committed
Accept qualified types in impl_from_* macros
1 parent 6469324 commit e02a915

File tree

2 files changed

+46
-57
lines changed

2 files changed

+46
-57
lines changed

src/lib.rs

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
use bitcoin::address::{NetworkChecked, NetworkUnchecked};
22
use bitcoin::consensus::{deserialize, serialize};
3-
use bitcoin::Address as BitcoinAddress;
4-
use bitcoin::Amount as BitcoinAmount;
5-
use bitcoin::FeeRate as BitcoinFeeRate;
6-
use bitcoin::OutPoint as BitcoinOutPoint;
7-
use bitcoin::ScriptBuf as BitcoinScriptBuf;
8-
use bitcoin::Sequence;
9-
use bitcoin::Transaction as BitcoinTransaction;
10-
pub use bitcoin::Txid as BitcoinTxid;
11-
use bitcoin::TxIn as BitcoinTxIn;
12-
use bitcoin::TxOut as BitcoinTxOut;
133

144
pub use bitcoin::BlockHash;
155

@@ -28,20 +18,20 @@ mod macros;
2818
pub mod error;
2919

3020
#[derive(Debug, PartialEq, Eq, uniffi::Object)]
31-
pub struct Address(BitcoinAddress<NetworkChecked>);
21+
pub struct Address(bitcoin::Address<NetworkChecked>);
3222

3323
#[uniffi::export]
3424
impl Address {
3525
#[uniffi::constructor]
3626
pub fn new(address: String, network: Network) -> Result<Self, AddressParseError> {
37-
let parsed_address = BitcoinAddress::from_str(&address).map_err(AddressParseError::from)?;
27+
let parsed_address = bitcoin::Address::from_str(&address).map_err(AddressParseError::from)?;
3828
let network_checked_address = parsed_address.require_network(network.into())?;
3929
Ok(Address(network_checked_address))
4030
}
4131

4232
#[uniffi::constructor]
4333
pub fn from_script(script: Arc<Script>, network: Network) -> Result<Self, FromScriptError> {
44-
let address = BitcoinAddress::from_script(&script.0.clone(), Into::<bitcoin::Network>::into(network))?;
34+
let address = bitcoin::Address::from_script(&script.0.clone(), Into::<bitcoin::Network>::into(network))?;
4535
Ok(Address(address))
4636
}
4737

@@ -55,7 +45,7 @@ impl Address {
5545

5646
pub fn is_valid_for_network(&self, network: Network) -> bool {
5747
let address_str = self.0.to_string();
58-
if let Ok(unchecked_address) = address_str.parse::<BitcoinAddress<NetworkUnchecked>>() {
48+
if let Ok(unchecked_address) = address_str.parse::<bitcoin::Address<NetworkUnchecked>>() {
5949
unchecked_address.is_valid_for_network(network.into())
6050
} else {
6151
false
@@ -69,18 +59,17 @@ impl Display for Address {
6959
}
7060
}
7161

72-
type CheckedBitcoinAddress = BitcoinAddress<NetworkChecked>;
73-
impl_from_core_type!(Address, CheckedBitcoinAddress);
74-
impl_from_ffi_type!(Address, CheckedBitcoinAddress);
62+
impl_from_core_type!(Address, bitcoin::Address<NetworkChecked>);
63+
impl_from_ffi_type!(Address, bitcoin::Address<NetworkChecked>);
7564

7665
#[derive(Clone, Debug, uniffi::Object)]
77-
pub struct FeeRate(pub BitcoinFeeRate);
66+
pub struct FeeRate(pub bitcoin::FeeRate);
7867

7968
#[uniffi::export]
8069
impl FeeRate {
8170
#[uniffi::constructor]
8271
pub fn from_sat_per_vb(sat_per_vb: u64) -> Result<Self, FeeRateError> {
83-
let fee_rate: Option<BitcoinFeeRate> = BitcoinFeeRate::from_sat_per_vb(sat_per_vb);
72+
let fee_rate: Option<bitcoin::FeeRate> = bitcoin::FeeRate::from_sat_per_vb(sat_per_vb);
8473
match fee_rate {
8574
Some(fee_rate) => Ok(FeeRate(fee_rate)),
8675
None => Err(FeeRateError::ArithmeticOverflow),
@@ -89,7 +78,7 @@ impl FeeRate {
8978

9079
#[uniffi::constructor]
9180
pub fn from_sat_per_kwu(sat_per_kwu: u64) -> Self {
92-
FeeRate(BitcoinFeeRate::from_sat_per_kwu(sat_per_kwu))
81+
FeeRate(bitcoin::FeeRate::from_sat_per_kwu(sat_per_kwu))
9382
}
9483

9584
pub fn to_sat_per_vb_ceil(&self) -> u64 {
@@ -105,17 +94,17 @@ impl FeeRate {
10594
}
10695
}
10796

108-
impl_from_core_type!(FeeRate, BitcoinFeeRate);
109-
impl_from_ffi_type!(FeeRate, BitcoinFeeRate);
97+
impl_from_core_type!(FeeRate, bitcoin::FeeRate);
98+
impl_from_ffi_type!(FeeRate, bitcoin::FeeRate);
11099

111100
#[derive(Clone, Debug, PartialEq, Eq, uniffi::Object)]
112-
pub struct OutPoint(pub BitcoinOutPoint);
101+
pub struct OutPoint(pub bitcoin::OutPoint);
113102

114103
#[uniffi::export]
115104
impl OutPoint {
116105
#[uniffi::constructor]
117106
pub fn new(txid: Arc<Txid>, vout: u32) -> Self {
118-
OutPoint(BitcoinOutPoint::new(txid.0, vout))
107+
OutPoint(bitcoin::OutPoint::new(txid.0, vout))
119108
}
120109

121110
pub fn txid(&self) -> Arc<Txid> {
@@ -131,23 +120,23 @@ impl OutPoint {
131120
}
132121
}
133122

134-
impl_from_core_type!(OutPoint, BitcoinOutPoint);
135-
impl_from_ffi_type!(OutPoint, BitcoinOutPoint);
123+
impl_from_core_type!(OutPoint, bitcoin::OutPoint);
124+
impl_from_ffi_type!(OutPoint, bitcoin::OutPoint);
136125

137126
#[derive(Clone, Debug, PartialEq, Eq, uniffi::Object)]
138-
pub struct Txid(pub BitcoinTxid);
127+
pub struct Txid(pub bitcoin::Txid);
139128

140-
impl_from_core_type!(Txid, BitcoinTxid);
141-
impl_from_ffi_type!(Txid, BitcoinTxid);
129+
impl_from_core_type!(Txid, bitcoin::Txid);
130+
impl_from_ffi_type!(Txid, bitcoin::Txid);
142131

143132
#[derive(Clone, Debug, PartialEq, Eq, uniffi::Object)]
144-
pub struct Script(pub BitcoinScriptBuf);
133+
pub struct Script(pub bitcoin::ScriptBuf);
145134

146135
#[uniffi::export]
147136
impl Script {
148137
#[uniffi::constructor]
149138
pub fn new(raw_output_script: Vec<u8>) -> Self {
150-
let script: BitcoinScriptBuf = raw_output_script.into();
139+
let script: bitcoin::ScriptBuf = raw_output_script.into();
151140
Script(script)
152141
}
153142

@@ -156,46 +145,46 @@ impl Script {
156145
}
157146
}
158147

159-
impl_from_core_type!(Script, BitcoinScriptBuf);
160-
impl_from_ffi_type!(Script, BitcoinScriptBuf);
148+
impl_from_core_type!(Script, bitcoin::ScriptBuf);
149+
impl_from_ffi_type!(Script, bitcoin::ScriptBuf);
161150

162151
#[derive(Debug, Clone, uniffi::Record)]
163152
pub struct TxOut {
164153
pub value: Arc<Amount>,
165154
pub script_pubkey: Arc<Script>,
166155
}
167156

168-
impl From<BitcoinTxOut> for TxOut {
169-
fn from(tx_out: BitcoinTxOut) -> Self {
157+
impl From<bitcoin::TxOut> for TxOut {
158+
fn from(tx_out: bitcoin::TxOut) -> Self {
170159
TxOut {
171160
value: Arc::new(Amount(tx_out.value)),
172161
script_pubkey: Arc::new(Script(tx_out.script_pubkey)),
173162
}
174163
}
175164
}
176165

177-
impl From<TxOut> for BitcoinTxOut {
166+
impl From<TxOut> for bitcoin::TxOut {
178167
fn from(tx_out: TxOut) -> Self {
179-
BitcoinTxOut {
168+
bitcoin::TxOut {
180169
value: tx_out.value.0,
181170
script_pubkey: tx_out.script_pubkey.0.clone(),
182171
}
183172
}
184173
}
185174

186175
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Object)]
187-
pub struct Amount(pub BitcoinAmount);
176+
pub struct Amount(pub bitcoin::Amount);
188177

189178
#[uniffi::export]
190179
impl Amount {
191180
#[uniffi::constructor]
192181
pub fn from_sat(sat: u64) -> Self {
193-
Amount(BitcoinAmount::from_sat(sat))
182+
Amount(bitcoin::Amount::from_sat(sat))
194183
}
195184

196185
#[uniffi::constructor]
197186
pub fn from_btc(btc: f64) -> Result<Self, ParseAmountError> {
198-
let bitcoin_amount = BitcoinAmount::from_btc(btc).map_err(ParseAmountError::from)?;
187+
let bitcoin_amount = bitcoin::Amount::from_btc(btc).map_err(ParseAmountError::from)?;
199188
Ok(Amount(bitcoin_amount))
200189
}
201190

@@ -208,8 +197,8 @@ impl Amount {
208197
}
209198
}
210199

211-
impl_from_core_type!(Amount, BitcoinAmount);
212-
impl_from_ffi_type!(Amount, BitcoinAmount);
200+
impl_from_core_type!(Amount, bitcoin::Amount);
201+
impl_from_ffi_type!(Amount, bitcoin::Amount);
213202

214203
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
215204
pub struct TxIn {
@@ -219,8 +208,8 @@ pub struct TxIn {
219208
pub witness: Vec<Vec<u8>>,
220209
}
221210

222-
impl From<BitcoinTxIn> for TxIn {
223-
fn from(tx_in: BitcoinTxIn) -> Self {
211+
impl From<bitcoin::TxIn> for TxIn {
212+
fn from(tx_in: bitcoin::TxIn) -> Self {
224213
TxIn {
225214
previous_output: Arc::new(tx_in.previous_output.into()),
226215
script_sig: Arc::new(tx_in.script_sig.into()),
@@ -230,8 +219,8 @@ impl From<BitcoinTxIn> for TxIn {
230219
}
231220
}
232221

233-
impl From<&BitcoinTxIn> for TxIn {
234-
fn from(tx_in: &BitcoinTxIn) -> Self {
222+
impl From<&bitcoin::TxIn> for TxIn {
223+
fn from(tx_in: &bitcoin::TxIn) -> Self {
235224
TxIn {
236225
previous_output: Arc::new(tx_in.previous_output.into()),
237226
script_sig: Arc::new(tx_in.script_sig.clone().into()),
@@ -241,25 +230,25 @@ impl From<&BitcoinTxIn> for TxIn {
241230
}
242231
}
243232

244-
impl From<TxIn> for BitcoinTxIn {
233+
impl From<TxIn> for bitcoin::TxIn {
245234
fn from(tx_in: TxIn) -> Self {
246-
BitcoinTxIn {
235+
bitcoin::TxIn {
247236
previous_output: tx_in.previous_output.0,
248237
script_sig: tx_in.script_sig.0.clone(),
249-
sequence: Sequence(tx_in.sequence),
238+
sequence: bitcoin::Sequence(tx_in.sequence),
250239
witness: tx_in.witness.into(),
251240
}
252241
}
253242
}
254243

255244
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Object)]
256-
pub struct Transaction(pub BitcoinTransaction);
245+
pub struct Transaction(pub bitcoin::Transaction);
257246

258247
#[uniffi::export]
259248
impl Transaction {
260249
#[uniffi::constructor]
261250
pub fn deserialize(transaction_bytes: &[u8]) -> Result<Self, EncodeError> {
262-
let transaction: BitcoinTransaction = deserialize(transaction_bytes)?;
251+
let transaction: bitcoin::Transaction = deserialize(transaction_bytes)?;
263252
Ok(Transaction(transaction))
264253
}
265254

@@ -322,8 +311,8 @@ impl Transaction {
322311
}
323312
}
324313

325-
impl_from_core_type!(Transaction, BitcoinTransaction);
326-
impl_from_ffi_type!(Transaction, BitcoinTransaction);
314+
impl_from_core_type!(Transaction, bitcoin::Transaction);
315+
impl_from_ffi_type!(Transaction, bitcoin::Transaction);
327316

328317
#[derive(Clone, Default, uniffi::Enum)]
329318
#[non_exhaustive]

src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
macro_rules! impl_from_core_type {
2-
($ffi_type:ident, $core_type:ident) => {
2+
($ffi_type:ty, $core_type:ty) => {
33
impl From<$core_type> for $ffi_type {
44
fn from(core_type: $core_type) -> Self {
5-
$ffi_type(core_type)
5+
Self(core_type)
66
}
77
}
88
};
99
}
1010

1111
macro_rules! impl_from_ffi_type {
12-
($ffi_type:ident, $core_type:ident) => {
12+
($ffi_type:ty, $core_type:ty) => {
1313
impl From<$ffi_type> for $core_type {
1414
fn from(ffi_type: $ffi_type) -> Self {
15-
ffi_type.0
15+
ffi_type.into()
1616
}
1717
}
1818
};

0 commit comments

Comments
 (0)