Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rand crate re-exports when available #64

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ pub extern crate serde;

use core::{fmt, str};

/// We support a wide range of dependency versions for `rand` and `rand_core` and not
/// all versions play nicely together. These re-exports fix that.
#[cfg(all(feature = "rand", feature = "rand_core"))]
use rand::{CryptoRng, RngCore};
#[cfg(all(not(feature = "rand"), feature = "rand_core"))]
use rand_core::{CryptoRng, RngCore};

#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(feature = "std")]
Expand Down Expand Up @@ -262,15 +269,15 @@ impl Mnemonic {
word_count: usize,
) -> Result<Mnemonic, Error>
where
R: rand_core::RngCore + rand_core::CryptoRng,
R: RngCore + CryptoRng,
{
if is_invalid_word_count(word_count) {
return Err(Error::BadWordCount(word_count));
}

let entropy_bytes = (word_count / 3) * 4;
let mut entropy = [0u8; (MAX_NB_WORDS / 3) * 4];
rand_core::RngCore::fill_bytes(rng, &mut entropy[0..entropy_bytes]);
RngCore::fill_bytes(rng, &mut entropy[0..entropy_bytes]);
Mnemonic::from_entropy_in(language, &entropy[0..entropy_bytes])
}

Expand Down