Skip to content

Commit 37e4d52

Browse files
nathanwhitclarkmoody
authored andcommitted
Support no_std
1 parent 6b5dd29 commit 37e4d52

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ categories = ["encoding"]
1010
license = "MIT"
1111

1212
[features]
13+
default = ["std"]
1314
# Only for CI to make all warnings errors, do not activate otherwise (may break forward compatibility)
1415
strict = []
16+
std = []
1517

1618
[dependencies]
1719
bech32 = "0.8.0"

src/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
//! The authoratative list of Human-readable parts for Bech32 addresses is
2424
//! maintained in [SLIP-0173](https://github.com/satoshilabs/slips/blob/master/slip-0173.md).
2525
26+
use crate::imports::*;
27+
2628
/// The cryptocurrency to act on
2729
#[derive(PartialEq, Eq, Debug, Clone, Copy, PartialOrd, Ord, Hash)]
2830
pub enum Network {

src/lib.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,33 @@
5959
#![deny(non_snake_case)]
6060
#![deny(unused_mut)]
6161
#![cfg_attr(feature = "strict", deny(warnings))]
62+
#![cfg_attr(not(feature = "std"), no_std)]
63+
64+
#[cfg_attr(not(feature = "std"), macro_use)]
65+
#[cfg(not(feature = "std"))]
66+
extern crate alloc;
67+
68+
#[cfg(not(feature = "std"))]
69+
pub(crate) mod imports {
70+
pub use alloc::string::String;
71+
pub use alloc::string::ToString;
72+
pub use alloc::vec::Vec;
73+
pub use core::fmt;
74+
pub use core::str::FromStr;
75+
}
76+
#[cfg(feature = "std")]
77+
pub(crate) mod imports {
78+
pub use std::str::FromStr;
79+
pub use std::string::ToString;
80+
pub use std::{error, fmt};
81+
}
82+
83+
use imports::*;
6284

6385
extern crate bech32;
6486
pub use bech32::u5;
6587
use bech32::{decode, encode, FromBase32, ToBase32, Variant};
6688

67-
use std::str::FromStr;
68-
use std::string::ToString;
69-
use std::{error, fmt};
70-
7189
pub mod constants;
7290
use constants::Network;
7391

@@ -296,6 +314,7 @@ impl fmt::Display for Error {
296314
}
297315
}
298316

317+
#[cfg(feature = "std")]
299318
impl error::Error for Error {
300319
fn description(&self) -> &str {
301320
match *self {

0 commit comments

Comments
 (0)