Skip to content

Commit

Permalink
alloc or std fix
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkucharczyk committed Nov 3, 2023
1 parent a8e5014 commit 053aa09
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extern crate alloc;

extern crate bitcoin_hashes;

#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", feature = "std"))]
extern crate unicode_normalization;

#[cfg(feature = "rand")]
Expand All @@ -45,14 +45,13 @@ pub extern crate serde;

use core::{fmt, str};

#[cfg(feature = "alloc")]
use alloc::borrow::Cow;
#[cfg(feature = "std")]
use std::error;

use bitcoin_hashes::{sha256, Hash};

#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", feature = "std"))]
use unicode_normalization::UnicodeNormalization;

#[cfg(feature = "zeroize")]
Expand Down Expand Up @@ -95,7 +94,7 @@ impl AmbiguousLanguages {
}

/// Returns a vector of the possible languages.
#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn to_vec(&self) -> Vec<Language> {
self.iter().collect()
}
Expand Down Expand Up @@ -183,7 +182,7 @@ impl Mnemonic {
/// Performing this on a [Cow] means that all allocations for normalization
/// can be avoided for languages without special UTF8 characters.
#[inline]
#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn normalize_utf8_cow<'a>(cow: &mut Cow<'a, str>) {
let is_nfkd = unicode_normalization::is_nfkd_quick(cow.as_ref().chars());
if is_nfkd != unicode_normalization::IsNormalized::Yes {
Expand Down Expand Up @@ -506,7 +505,7 @@ impl Mnemonic {
}

/// Parse a mnemonic in the given language.
#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn parse_in<'a, S: Into<Cow<'a, str>>>(
language: Language,
s: S,
Expand All @@ -517,7 +516,7 @@ impl Mnemonic {
}

/// Parse a mnemonic and detect the language from the enabled languages.
#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn parse<'a, S: Into<Cow<'a, str>>>(s: S) -> Result<Mnemonic, Error> {
let mut cow = s.into();
Mnemonic::normalize_utf8_cow(&mut cow);
Expand Down Expand Up @@ -547,7 +546,7 @@ impl Mnemonic {
}

/// Convert to seed bytes.
#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn to_seed<'a, P: Into<Cow<'a, str>>>(&self, passphrase: P) -> [u8; 64] {
let normalized_passphrase = {
let mut cow = passphrase.into();
Expand Down Expand Up @@ -596,7 +595,7 @@ impl Mnemonic {
}

/// Convert the mnemonic back to the entropy used to generate it.
#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn to_entropy(&self) -> Vec<u8> {
let (arr, len) = self.to_entropy_array();
arr[0..len].to_vec()
Expand All @@ -619,11 +618,11 @@ impl str::FromStr for Mnemonic {
type Err = Error;

fn from_str(s: &str) -> Result<Mnemonic, Error> {
#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", feature = "std"))]
{
Mnemonic::parse(s)
}
#[cfg(not(feature = "std"))]
#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
{
Mnemonic::parse_normalized(s)
}
Expand Down

0 comments on commit 053aa09

Please sign in to comment.