Skip to content

Commit bd556d1

Browse files
committed
no_std
1 parent 2d7dccf commit bd556d1

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ categories = ["encoding"]
1010
license = "MIT"
1111

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

src/lib.rs

+33-16
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,21 @@
2929
//! The original description in [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki)
3030
//! has more details.
3131
//!
32-
//! # Examples
33-
//!
34-
//! ```
35-
//! use bech32::{self, FromBase32, ToBase32, Variant};
36-
//!
37-
//! let encoded = bech32::encode("bech32", vec![0x00, 0x01, 0x02].to_base32(), Variant::Bech32).unwrap();
38-
//! assert_eq!(encoded, "bech321qqqsyrhqy2a".to_string());
39-
//!
40-
//! let (hrp, data, variant) = bech32::decode(&encoded).unwrap();
41-
//! assert_eq!(hrp, "bech32");
42-
//! assert_eq!(Vec::<u8>::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]);
43-
//! assert_eq!(variant, Variant::Bech32);
44-
//! ```
32+
#![cfg_attr(
33+
feature = "std",
34+
doc = "
35+
# Examples
36+
```
37+
use bech32::{self, FromBase32, ToBase32, Variant};
38+
let encoded = bech32::encode(\"bech32\", vec![0x00, 0x01, 0x02].to_base32(), Variant::Bech32).unwrap();
39+
assert_eq!(encoded, \"bech321qqqsyrhqy2a\".to_string());
40+
let (hrp, data, variant) = bech32::decode(&encoded).unwrap();
41+
assert_eq!(hrp, \"bech32\");
42+
assert_eq!(Vec::<u8>::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]);
43+
assert_eq!(variant, Variant::Bech32);
44+
```
45+
"
46+
)]
4547
//!
4648
4749
// Allow trait objects without dyn on nightly and make 1.22 ignore the unknown lint
@@ -53,9 +55,23 @@
5355
#![deny(non_snake_case)]
5456
#![deny(unused_mut)]
5557
#![cfg_attr(feature = "strict", deny(warnings))]
58+
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
5659

60+
#[cfg(all(not(feature = "std"), not(test)))]
61+
extern crate alloc;
62+
63+
#[cfg(all(not(feature = "std"), not(test)))]
64+
use alloc::{string::String, vec::Vec};
65+
66+
#[cfg(all(not(feature = "std"), not(test)))]
67+
use alloc::borrow::Cow;
68+
#[cfg(any(feature = "std", test))]
5769
use std::borrow::Cow;
58-
use std::{error, fmt};
70+
71+
#[cfg(all(not(feature = "std"), not(test)))]
72+
use core::{fmt, mem};
73+
#[cfg(any(feature = "std", test))]
74+
use std::{fmt, mem};
5975

6076
/// Integer in the range `0..32`
6177
#[derive(PartialEq, Eq, Debug, Copy, Clone, Default, PartialOrd, Ord, Hash)]
@@ -165,7 +181,7 @@ impl<'a> Bech32Writer<'a> {
165181
/// Write out the checksum at the end. If this method isn't called this will happen on drop.
166182
pub fn finalize(mut self) -> fmt::Result {
167183
self.inner_finalize()?;
168-
std::mem::forget(self);
184+
mem::forget(self);
169185
Ok(())
170186
}
171187

@@ -622,7 +638,8 @@ impl fmt::Display for Error {
622638
}
623639
}
624640

625-
impl error::Error for Error {
641+
#[cfg(any(feature = "std", test))]
642+
impl std::error::Error for Error {
626643
fn description(&self) -> &str {
627644
match *self {
628645
Error::MissingSeparator => "missing human-readable separator",

0 commit comments

Comments
 (0)