From 2d799e232302669120caa41975541eff8a424e5d Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Tue, 7 Apr 2020 14:32:12 +0300 Subject: [PATCH 1/4] changed minimum rust version to 1.36 and added no_std support using the alloc crate --- .travis.yml | 4 ++- Cargo.toml | 2 ++ src/lib.rs | 75 ++++++++++++++++++++++++++++++++++------------------- 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index 16446d222..b02eba332 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,11 +2,13 @@ language: rust rust: - stable - nightly - - 1.22.0 # project wide min version + - 1.36.0 # project wide min version cache: cargo script: + - cargo build --verbose --no-default-features --features strict - cargo build --verbose --features strict + - cargo test --verbose --no-default-features --features strict - cargo test --verbose --features strict jobs: diff --git a/Cargo.toml b/Cargo.toml index 7626155ca..c5827020c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,8 @@ categories = ["encoding"] license = "MIT" [features] +default = ["std"] +std = [] # Only for CI to make all warnings errors, do not activate otherwise (may break forward compatibility) strict = [] diff --git a/src/lib.rs b/src/lib.rs index b12fc7649..0a81f6320 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,19 +27,20 @@ //! //! The original description in [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) has more details. //! -//! # Examples -//! -//! ``` -//! use bech32::{self, FromBase32, ToBase32}; -//! -//! let encoded = bech32::encode("bech32", vec![0x00, 0x01, 0x02].to_base32()).unwrap(); -//! assert_eq!(encoded, "bech321qqqsyrhqy2a".to_string()); -//! -//! let (hrp, data) = bech32::decode(&encoded).unwrap(); -//! assert_eq!(hrp, "bech32"); -//! assert_eq!(Vec::::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]); -//! ``` -//! +#![cfg_attr(feature = "std", doc = " +# Examples + +``` +use bech32::{self, FromBase32, ToBase32}; + +let encoded = bech32::encode(\"bech32\", vec![0x00, 0x01, 0x02].to_base32()).unwrap(); +assert_eq!(encoded, \"bech321qqqsyrhqy2a\".to_string()); + +let (hrp, data) = bech32::decode(&encoded).unwrap(); +assert_eq!(hrp, \"bech32\"); +assert_eq!(Vec::::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]); +``` +")] // Allow trait objects without dyn on nightly and make 1.22 ignore the unknown lint #![allow(unknown_lints)] @@ -51,12 +52,20 @@ #![deny(unused_mut)] #![cfg_attr(feature = "strict", deny(warnings))] -use std::borrow::Cow; -use std::{error, fmt}; +#![cfg_attr(not(feature = "std"), no_std)] +#[cfg(not(feature = "std"))] +#[macro_use] +extern crate alloc as std; -// AsciiExt is needed for Rust 1.14 but not for newer versions -#[allow(unused_imports, deprecated)] -use std::ascii::AsciiExt; +// Ideally we'd write +// use std::prelude::v1::*; +// but that is an unstable feature at the time of writing +// https://github.com/rust-lang/rust/issues/58935 +#[cfg(not(feature = "std"))] +use std::{vec::Vec, string::String}; + +use std::borrow::Cow; +use std::fmt; /// Integer in the range `0..32` #[derive(PartialEq, Eq, Debug, Copy, Clone, Default, PartialOrd, Ord, Hash)] @@ -160,7 +169,7 @@ impl<'a> Bech32Writer<'a> { /// Write out the checksum at the end. If this method isn't called this will happen on drop. pub fn finalize(mut self) -> fmt::Result { self.inner_finalize()?; - std::mem::forget(self); + core::mem::forget(self); Ok(()) } @@ -585,7 +594,8 @@ impl fmt::Display for Error { } } -impl error::Error for Error { +#[cfg(feature = "std")] +impl std::error::Error for Error { fn description(&self) -> &str { match *self { Error::MissingSeparator => "missing human-readable separator", @@ -609,13 +619,21 @@ impl error::Error for Error { /// Function will panic if attempting to convert `from` or `to` a bit size that /// is 0 or larger than 8 bits. /// -/// # Examples -/// -/// ```rust -/// use bech32::convert_bits; -/// let base5 = convert_bits(&[0xff], 8, 5, true); -/// assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]); -/// ``` +#[cfg_attr(feature = "std", doc = " +# Examples + +```rust +# #![cfg_attr(not(feature = \"std\"), no_std)] +# #[cfg(not(feature = \"std\"))] +# #[macro_use] +# extern crate alloc as std; +# fn main() { +use bech32::convert_bits; +let base5 = convert_bits(&[0xff], 8, 5, true); +assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]); +# } +``` +")] pub fn convert_bits(data: &[T], from: u32, to: u32, pad: bool) -> Result, Error> where T: Into + Copy, @@ -713,6 +731,7 @@ mod tests { let (s, expected_error) = p; let dec_result = decode(s); if dec_result.is_ok() { + #[cfg(feature = "std")] println!("{:?}", dec_result.unwrap()); panic!("Should be invalid: {:?}", s); } @@ -767,6 +786,8 @@ mod tests { } } + // unfortunately this test uses panic features which are unavailable in `core` + #[cfg(feature = "std")] #[test] fn convert_bits_invalid_bit_size() { use std::panic::{catch_unwind, set_hook, take_hook}; From c3cd45a6f6d36571dfd56f1077445a6de002d248 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Tue, 7 Apr 2020 14:46:11 +0300 Subject: [PATCH 2/4] fixed warning --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 0a81f6320..4d2c42204 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,7 @@ assert_eq!(Vec::::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]); #![cfg_attr(not(feature = "std"), no_std)] #[cfg(not(feature = "std"))] +#[allow(unused_imports)] #[macro_use] extern crate alloc as std; From 72ce8ff90c36c7aaed98697e381a1162563be8d5 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Tue, 7 Apr 2020 15:02:26 +0300 Subject: [PATCH 3/4] fixed formatter errors --- src/lib.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4d2c42204..35fd7535f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,9 @@ //! //! The original description in [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) has more details. //! -#![cfg_attr(feature = "std", doc = " +#![cfg_attr( + feature = "std", + doc = " # Examples ``` @@ -40,7 +42,8 @@ let (hrp, data) = bech32::decode(&encoded).unwrap(); assert_eq!(hrp, \"bech32\"); assert_eq!(Vec::::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]); ``` -")] +" +)] // Allow trait objects without dyn on nightly and make 1.22 ignore the unknown lint #![allow(unknown_lints)] @@ -63,7 +66,7 @@ extern crate alloc as std; // but that is an unstable feature at the time of writing // https://github.com/rust-lang/rust/issues/58935 #[cfg(not(feature = "std"))] -use std::{vec::Vec, string::String}; +use std::{string::String, vec::Vec}; use std::borrow::Cow; use std::fmt; @@ -620,7 +623,9 @@ impl std::error::Error for Error { /// Function will panic if attempting to convert `from` or `to` a bit size that /// is 0 or larger than 8 bits. /// -#[cfg_attr(feature = "std", doc = " +#[cfg_attr( + feature = "std", + doc = " # Examples ```rust @@ -634,7 +639,8 @@ let base5 = convert_bits(&[0xff], 8, 5, true); assert_eq!(base5.unwrap(), vec![0x1f, 0x1c]); # } ``` -")] +" +)] pub fn convert_bits(data: &[T], from: u32, to: u32, pad: bool) -> Result, Error> where T: Into + Copy, From b3a601250535173444ea1e3e7867c35965e9a649 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Tue, 7 Apr 2020 15:22:33 +0300 Subject: [PATCH 4/4] rustfmt fix --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 35fd7535f..5465752e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,6 @@ assert_eq!(Vec::::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]); ``` " )] - // Allow trait objects without dyn on nightly and make 1.22 ignore the unknown lint #![allow(unknown_lints)] #![allow(bare_trait_objects)] @@ -54,8 +53,8 @@ assert_eq!(Vec::::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]); #![deny(non_snake_case)] #![deny(unused_mut)] #![cfg_attr(feature = "strict", deny(warnings))] - #![cfg_attr(not(feature = "std"), no_std)] + #[cfg(not(feature = "std"))] #[allow(unused_imports)] #[macro_use]