Skip to content

Commit 6d3be0a

Browse files
committed
Merge rust-bitcoin#44: Add std feature
3a72c19 Remove dependency on bitcoin-internals (Tobin C. Harding) 27dbc0c types: Add std feature (Tobin C. Harding) c62cb3b Use BTreeMap instead of HashMap (Tobin C. Harding) Pull request description: `master` is currently broken because the linter is warning because of a `std` feature (which is coming from our usage of `internals::write_err`). We can fix this breakage after observing that calling macros from other crates is error prone. Lets copy the macro. On the way add a `std` feature and stop using `HashMap`. This also allows the removal of the `internals` dependency. Sexy PR right here. Close: rust-bitcoin#12 ACKs for top commit: apoelstra: ACK 3a72c19; successfully ran local tests Tree-SHA512: 0780be153283a723e0ebd4addf686304071aff8b1884e25270f5067e199840c8aff10a48a6b0bfb9f6a92b47374b9f7ac2231e6402ce58f94ce862eae03b4c18
2 parents 30bce38 + 3a72c19 commit 6d3be0a

File tree

19 files changed

+83
-22
lines changed

19 files changed

+83
-22
lines changed

Cargo-minimal.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ name = "corepc-types"
120120
version = "0.5.0"
121121
dependencies = [
122122
"bitcoin",
123-
"bitcoin-internals",
124123
"serde",
125124
"serde_json",
126125
]

Cargo-recent.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ name = "corepc-types"
120120
version = "0.5.0"
121121
dependencies = [
122122
"bitcoin",
123-
"bitcoin-internals",
124123
"serde",
125124
"serde_json",
126125
]

types/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ rust-version = "1.63.0"
1313
exclude = ["tests", "contrib"]
1414

1515
[features]
16-
default = []
16+
default = ["std"]
17+
std = ["bitcoin/std"]
1718

1819
[dependencies]
19-
bitcoin = { version = "0.32.0", default-features = false, features = ["std", "serde", "base64"] }
20-
internals = { package = "bitcoin-internals", version = "0.3.0", default-features = false, features = ["std"] }
20+
bitcoin = { version = "0.32.0", default-features = false, features = ["serde", "base64"] }
2121
serde = { version = "1.0.103", default-features = false, features = [ "derive", "alloc" ] }
2222
serde_json = { version = "1.0.117" }
2323

types/src/error.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
/// Formats error.
4+
///
5+
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because
6+
/// `e.source()` is only available in std builds, without this macro the error source is lost for
7+
/// no-std builds.
8+
macro_rules! write_err {
9+
($writer:expr, $string:literal $(, $args:expr)*; $source:expr) => {
10+
{
11+
#[cfg(feature = "std")]
12+
{
13+
let _ = &$source; // Prevents clippy warnings.
14+
write!($writer, $string $(, $args)*)
15+
}
16+
#[cfg(not(feature = "std"))]
17+
{
18+
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
19+
}
20+
}
21+
}
22+
}
23+
pub(crate) use write_err;

types/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
/// Re-export the `rust-bitcoin` crate.
1010
pub extern crate bitcoin;
1111

12+
extern crate alloc;
13+
1214
// TODO: Consider updating https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29 when this is complete.
1315

1416
// JSON types, for each specific version of `bitcoind`.
@@ -25,10 +27,11 @@ pub mod v26;
2527
pub mod v27;
2628
pub mod v28;
2729

30+
mod error;
2831
// JSON types that model _all_ `bitcoind` versions.
2932
pub mod model;
3033

31-
use std::fmt;
34+
use core::fmt;
3235

3336
use bitcoin::amount::ParseAmountError;
3437
use bitcoin::{Amount, FeeRate};
@@ -75,6 +78,7 @@ impl fmt::Display for NumericError {
7578
}
7679
}
7780

81+
#[cfg(feature = "std")]
7882
impl std::error::Error for NumericError {}
7983

8084
/// Converts `fee_rate` in BTC/kB to `FeeRate`.

types/src/model/blockchain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! These structs model the types returned by the JSON-RPC API but have concrete types
66
//! and are not specific to a specific version of Bitcoin Core.
77
8-
use std::collections::BTreeMap;
8+
use alloc::collections::BTreeMap;
99

1010
use bitcoin::address::NetworkUnchecked;
1111
use bitcoin::{

types/src/model/raw_transactions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! These structs model the types returned by the JSON-RPC API but have concrete types
66
//! and are not specific to a specific version of Bitcoin Core.
77
8-
use std::collections::BTreeMap;
8+
use alloc::collections::BTreeMap;
99

1010
use bitcoin::{Amount, FeeRate, Txid, Wtxid};
1111
use serde::{Deserialize, Serialize};

types/src/model/wallet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! These structs model the types returned by the JSON-RPC API but have concrete types
66
//! and are not specific to a specific version of Bitcoin Core.
77
8-
use std::collections::BTreeMap;
8+
use alloc::collections::BTreeMap;
99

1010
use bitcoin::address::NetworkUnchecked;
1111
use bitcoin::hashes::hash160;

types/src/v17/blockchain/error.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use bitcoin::amount::{self, ParseAmountError};
66
use bitcoin::consensus::encode;
77
use bitcoin::error::UnprefixedHexError;
88
use bitcoin::{address, hex, network};
9-
use internals::write_err;
109

10+
use crate::error::write_err;
1111
use crate::NumericError;
1212

1313
/// Error when converting a `GetBlockVerbosityOne` type into the model type.
@@ -47,6 +47,7 @@ impl fmt::Display for GetBlockVerbosityOneError {
4747
}
4848
}
4949

50+
#[cfg(feature = "std")]
5051
impl std::error::Error for GetBlockVerbosityOneError {
5152
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
5253
use GetBlockVerbosityOneError::*;
@@ -94,6 +95,7 @@ impl fmt::Display for GetBlockchainInfoError {
9495
}
9596
}
9697

98+
#[cfg(feature = "std")]
9799
impl std::error::Error for GetBlockchainInfoError {
98100
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
99101
use GetBlockchainInfoError::*;
@@ -131,6 +133,7 @@ impl fmt::Display for GetBlockHeaderError {
131133
}
132134
}
133135

136+
#[cfg(feature = "std")]
134137
impl std::error::Error for GetBlockHeaderError {
135138
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
136139
use GetBlockHeaderError::*;
@@ -179,6 +182,7 @@ impl fmt::Display for GetBlockHeaderVerboseError {
179182
}
180183
}
181184

185+
#[cfg(feature = "std")]
182186
impl std::error::Error for GetBlockHeaderVerboseError {
183187
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
184188
use GetBlockHeaderVerboseError::*;
@@ -219,6 +223,7 @@ impl fmt::Display for GetBlockStatsError {
219223
}
220224
}
221225

226+
#[cfg(feature = "std")]
222227
impl std::error::Error for GetBlockStatsError {
223228
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
224229
use GetBlockStatsError::*;
@@ -254,6 +259,7 @@ impl fmt::Display for ChainTipsError {
254259
}
255260
}
256261

262+
#[cfg(feature = "std")]
257263
impl std::error::Error for ChainTipsError {
258264
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
259265
use ChainTipsError::*;
@@ -290,6 +296,7 @@ impl fmt::Display for GetChainTxStatsError {
290296
}
291297
}
292298

299+
#[cfg(feature = "std")]
293300
impl std::error::Error for GetChainTxStatsError {
294301
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
295302
use GetChainTxStatsError::*;
@@ -325,6 +332,7 @@ impl fmt::Display for MapMempoolEntryError {
325332
}
326333
}
327334

335+
#[cfg(feature = "std")]
328336
impl std::error::Error for MapMempoolEntryError {
329337
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
330338
use MapMempoolEntryError as E;
@@ -369,6 +377,7 @@ impl fmt::Display for MempoolEntryError {
369377
}
370378
}
371379

380+
#[cfg(feature = "std")]
372381
impl std::error::Error for MempoolEntryError {
373382
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
374383
use MempoolEntryError as E;
@@ -409,6 +418,7 @@ impl fmt::Display for MempoolEntryFeesError {
409418
}
410419
}
411420

421+
#[cfg(feature = "std")]
412422
impl std::error::Error for MempoolEntryFeesError {
413423
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
414424
use MempoolEntryFeesError as E;
@@ -442,6 +452,7 @@ impl fmt::Display for GetMempoolInfoError {
442452
}
443453
}
444454

455+
#[cfg(feature = "std")]
445456
impl std::error::Error for GetMempoolInfoError {
446457
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
447458
use GetMempoolInfoError as E;
@@ -491,6 +502,7 @@ impl fmt::Display for GetTxOutError {
491502
}
492503
}
493504

505+
#[cfg(feature = "std")]
494506
impl std::error::Error for GetTxOutError {
495507
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
496508
use GetTxOutError::*;
@@ -532,6 +544,7 @@ impl fmt::Display for GetTxOutSetInfoError {
532544
}
533545
}
534546

547+
#[cfg(feature = "std")]
535548
impl std::error::Error for GetTxOutSetInfoError {
536549
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
537550
use GetTxOutSetInfoError::*;

types/src/v17/blockchain/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
mod error;
88
mod into;
99

10-
use std::collections::BTreeMap;
10+
use alloc::collections::BTreeMap;
1111

1212
use bitcoin::hex::FromHex;
1313
use bitcoin::{Address, Amount, FeeRate, Network, ScriptBuf, TxMerkleNode, TxOut, Wtxid};

0 commit comments

Comments
 (0)