Skip to content

Commit 86353a5

Browse files
alloc feature added enabling unicode_normalization in no-std
This commit introduces the `alloc` feature. The alloc feature is intended to use in no-std environments which are allowed to use alloc. New feature enables: - the unicode-normalization, and all related methods (parse_in,normalize_utf8_cow,parse,to_seed) - to_entropy() method as Vec is available in alloc,
1 parent 09d89c5 commit 86353a5

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ edition = "2018"
1313

1414
[features]
1515
default = [ "std" ]
16-
std = [ "unicode-normalization", "serde/std" ]
16+
std = [ "alloc", "serde/std", "unicode-normalization/std" ]
1717
rand = [ "crate_rand", "rand_core" ]
18+
alloc = [ "unicode-normalization" ]
1819

1920
# Note: English is the standard for bip39 so always included
2021
chinese-simplified = []
@@ -49,7 +50,7 @@ zeroize = { version = "1.5", features = ["zeroize_derive"], optional = true }
4950

5051
# Unexported dependnecies
5152
bitcoin_hashes = { version = ">=0.12, <=0.13", default-features = false }
52-
unicode-normalization = { version = "=0.1.22", optional = true }
53+
unicode-normalization = { version = "=0.1.22", default-features = false, optional = true }
5354

5455
[dev-dependencies]
5556
# Enabling the "rand" feature by default to run the benches

src/lib.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
#[cfg(any(test, feature = "std"))]
3030
pub extern crate core;
3131

32+
#[cfg(feature = "alloc")]
3233
extern crate alloc;
3334

3435
extern crate bitcoin_hashes;
3536

36-
#[cfg(feature = "std")]
37+
#[cfg(feature = "unicode-normalization")]
3738
extern crate unicode_normalization;
3839

3940
#[cfg(feature = "rand")]
@@ -43,16 +44,16 @@ pub extern crate rand_core;
4344
#[cfg(feature = "serde")]
4445
pub extern crate serde;
4546

47+
#[cfg(feature = "alloc")]
48+
use alloc::{borrow::Cow, string::ToString, vec::Vec};
4649
use core::{fmt, str};
4750

48-
#[cfg(feature = "std")]
49-
use std::borrow::Cow;
5051
#[cfg(feature = "std")]
5152
use std::error;
5253

5354
use bitcoin_hashes::{sha256, Hash};
5455

55-
#[cfg(feature = "std")]
56+
#[cfg(feature = "unicode-normalization")]
5657
use unicode_normalization::UnicodeNormalization;
5758

5859
#[cfg(feature = "zeroize")]
@@ -95,7 +96,7 @@ impl AmbiguousLanguages {
9596
}
9697

9798
/// Returns a vector of the possible languages.
98-
#[cfg(feature = "std")]
99+
#[cfg(feature = "alloc")]
99100
pub fn to_vec(&self) -> Vec<Language> {
100101
self.iter().collect()
101102
}
@@ -183,7 +184,7 @@ impl Mnemonic {
183184
/// Performing this on a [Cow] means that all allocations for normalization
184185
/// can be avoided for languages without special UTF8 characters.
185186
#[inline]
186-
#[cfg(feature = "std")]
187+
#[cfg(feature = "unicode-normalization")]
187188
pub fn normalize_utf8_cow<'a>(cow: &mut Cow<'a, str>) {
188189
let is_nfkd = unicode_normalization::is_nfkd_quick(cow.as_ref().chars());
189190
if is_nfkd != unicode_normalization::IsNormalized::Yes {
@@ -506,7 +507,7 @@ impl Mnemonic {
506507
}
507508

508509
/// Parse a mnemonic in the given language.
509-
#[cfg(feature = "std")]
510+
#[cfg(feature = "unicode-normalization")]
510511
pub fn parse_in<'a, S: Into<Cow<'a, str>>>(
511512
language: Language,
512513
s: S,
@@ -517,7 +518,7 @@ impl Mnemonic {
517518
}
518519

519520
/// Parse a mnemonic and detect the language from the enabled languages.
520-
#[cfg(feature = "std")]
521+
#[cfg(feature = "unicode-normalization")]
521522
pub fn parse<'a, S: Into<Cow<'a, str>>>(s: S) -> Result<Mnemonic, Error> {
522523
let mut cow = s.into();
523524
Mnemonic::normalize_utf8_cow(&mut cow);
@@ -547,7 +548,7 @@ impl Mnemonic {
547548
}
548549

549550
/// Convert to seed bytes.
550-
#[cfg(feature = "std")]
551+
#[cfg(feature = "unicode-normalization")]
551552
pub fn to_seed<'a, P: Into<Cow<'a, str>>>(&self, passphrase: P) -> [u8; 64] {
552553
let normalized_passphrase = {
553554
let mut cow = passphrase.into();
@@ -596,7 +597,7 @@ impl Mnemonic {
596597
}
597598

598599
/// Convert the mnemonic back to the entropy used to generate it.
599-
#[cfg(feature = "std")]
600+
#[cfg(feature = "alloc")]
600601
pub fn to_entropy(&self) -> Vec<u8> {
601602
let (arr, len) = self.to_entropy_array();
602603
arr[0..len].to_vec()
@@ -648,11 +649,11 @@ impl str::FromStr for Mnemonic {
648649
type Err = Error;
649650

650651
fn from_str(s: &str) -> Result<Mnemonic, Error> {
651-
#[cfg(feature = "std")]
652+
#[cfg(feature = "unicode-normalization")]
652653
{
653654
Mnemonic::parse(s)
654655
}
655-
#[cfg(not(feature = "std"))]
656+
#[cfg(not(feature = "unicode-normalization"))]
656657
{
657658
Mnemonic::parse_normalized(s)
658659
}
@@ -871,7 +872,7 @@ mod tests {
871872
mnemonic_str
872873
);
873874

874-
#[cfg(feature = "std")]
875+
#[cfg(feature = "unicode-normalization")]
875876
{
876877
assert_eq!(&mnemonic.to_string(), mnemonic_str, "failed vector: {}", mnemonic_str);
877878
assert_eq!(

0 commit comments

Comments
 (0)