Differentiate std and core

This commit is contained in:
Steven Roose 2021-03-24 16:59:32 +00:00
parent d41e0722dc
commit ca454327b2
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
4 changed files with 35 additions and 14 deletions

View File

@ -21,6 +21,8 @@ script:
- cargo test --verbose
- cargo build --verbose --no-default-features
- cargo test --verbose --no-default-features
- cargo build --verbose --no-default-features --features core,all-languages
- cargo test --verbose --no-default-features --features core,all-languages
- cargo build --verbose --features rand,all-languages
- cargo test --verbose --features rand,all-languages
# benchmarks

View File

@ -16,7 +16,8 @@ path = "src/lib.rs"
[features]
default = [ "std" ]
std = [ "serde/std" ]
core = []
std = [ "core", "unicode-normalization", "serde/std" ]
# Note: English is the standard for bip39 so always included
chinese-simplified = []
@ -41,8 +42,8 @@ all-languages = [
[dependencies]
bitcoin_hashes = "0.9.4"
unicode-normalization = "=0.1.9"
unicode-normalization = { version = "=0.1.9", optional = true }
rand = { version = "0.6.0", optional = true }
serde = { version = "1.0", default-features = false, optional = true }

View File

@ -1,6 +1,6 @@
#[cfg(feature = "std")]
use std::fmt;
#[cfg(feature = "core")]
use core::fmt;
mod english;
#[cfg(feature = "chinese-simplified")]
@ -153,7 +153,7 @@ impl Language {
}
}
#[cfg(feature = "std")]
#[cfg(feature = "core")]
impl fmt::Display for Language {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)

View File

@ -27,7 +27,9 @@
#![deny(missing_docs)]
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#[cfg(any(test, feature = "std"))] pub extern crate core;
#[cfg(any(test, feature = "std"))]
pub extern crate core;
extern crate bitcoin_hashes;
#[cfg(feature = "std")]
@ -38,8 +40,11 @@ extern crate rand;
#[cfg(feature = "serde")]
pub extern crate serde;
#[cfg(feature = "core")]
use core::{fmt, str};
#[cfg(feature = "std")]
use std::{error, fmt, str};
use std::error;
#[cfg(feature = "std")]
use std::borrow::Cow;
@ -75,7 +80,7 @@ impl AmbiguousLanguages {
}
/// An iterator over the possible languages.
#[cfg(feature = "std")]
#[cfg(feature = "core")]
pub fn iter(&self) -> impl Iterator<Item = Language> + '_ {
Language::all().iter().enumerate().filter(move |(i, _)| self.0[*i]).map(|(_, l)| *l)
}
@ -106,7 +111,7 @@ pub enum Error {
AmbiguousLanguages(AmbiguousLanguages),
}
#[cfg(feature = "std")]
#[cfg(feature = "core")]
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
@ -120,7 +125,17 @@ impl fmt::Display for Error {
"entropy was not between 128-256 bits or not a multiple of 32 bits: {} bits", c,
),
Error::InvalidChecksum => write!(f, "the mnemonic has an invalid checksum"),
Error::AmbiguousLanguages(a) => write!(f, "ambiguous word list: {:?}", a.to_vec()),
Error::AmbiguousLanguages(a) => {
write!(f, "ambiguous word list: ")?;
for (i, lang) in a.iter().enumerate() {
if i == 0 {
write!(f, "{}", lang)?;
} else {
write!(f, ", {}", lang)?;
}
}
Ok(())
}
}
}
}
@ -130,7 +145,7 @@ impl error::Error for Error {}
/// A mnemonic code.
///
/// The [std::str::FromStr] implementation will try to determine the language of the
/// The [core::str::FromStr] implementation will try to determine the language of the
/// mnemonic from all the supported languages. (Languages have to be explicitly enabled using
/// the Cargo features.)
///
@ -443,7 +458,7 @@ impl Mnemonic {
}
}
#[cfg(feature = "std")]
#[cfg(feature = "core")]
impl fmt::Display for Mnemonic {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in 0..self.0.len() {
@ -460,12 +475,15 @@ impl fmt::Display for Mnemonic {
}
}
#[cfg(feature = "std")]
#[cfg(feature = "core")]
impl str::FromStr for Mnemonic {
type Err = Error;
fn from_str(s: &str) -> Result<Mnemonic, Error> {
Mnemonic::parse(s)
#[cfg(feature = "std")]
{ Mnemonic::parse(s) }
#[cfg(not(feature = "std"))]
{ Mnemonic::parse_normalized(s) }
}
}