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 getrandom crate for uuid #447

Merged
merged 4 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ status = "actively-developed"
[badges.travis-ci]
repository = "uuid-rs/uuid"

[dependencies.md5]
[dependencies.getrandom]
ammgws marked this conversation as resolved.
Show resolved Hide resolved
optional = true
version = "0.7"
version = "0.1"

[dependencies.rand]
[dependencies.md5]
optional = true
version = "0.7"

Expand Down Expand Up @@ -92,12 +92,12 @@ version = "1.0.56"
default = ["std"]
guid = ["winapi"]
std = []
stdweb = [ "rand/stdweb" ]
stdweb = ["getrandom"]
v1 = []
v3 = ["md5"]
v4 = ["rand"]
v4 = ["getrandom"]
v5 = ["sha1"]
wasm-bindgen = ["rand/wasm-bindgen"]
wasm-bindgen = ["getrandom"]

[target.'cfg(windows)'.dependencies.winapi]
optional = true
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ To create a new random (V4) UUID and print it out in hexadecimal form:

use uuid::Uuid;

fn main() {
let my_uuid = Uuid::new_v4();
fn main() -> Result<(), Box<std::error::Error> {
let my_uuid = Uuid::new_v4()?;
println!("{}", my_uuid);
}
```
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
//!
//! use uuid::Uuid;
//!
//! fn main() {
//! let my_uuid = Uuid::new_v4();
//! fn main() -> Result<(), Box<std::error::Error> {
//! let my_uuid = Uuid::new_v4()?;
//! println!("{}", my_uuid);
//! }
//! ```
Expand Down
28 changes: 13 additions & 15 deletions src/v4.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::prelude::*;
use rand;

impl Uuid {
/// Creates a random UUID.
///
/// This uses the [`rand`] crate's default task RNG as the source of random
/// numbers. If you'd like to use a custom generator, don't use this
/// method: use the `rand::Rand trait`'s `rand()` method instead.
/// This uses the [`getrandom`] crate to utilise the operating system's RNG
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KodrAus should we expose the fact that we are using the getrandom crate implementation in the docs?

/// as the source of random numbers. If you'd like to use a custom generator,
/// don't use this method: use the `rand::Rand trait`'s `rand()` method instead.
///
/// Note that usage of this method requires the `v4` feature of this crate
/// to be enabled.
Expand All @@ -21,19 +20,18 @@ impl Uuid {
/// let uuid = Uuid::new_v4();
/// ```
///
/// [`getrandom`]: https://crates.io/crates/getrandom
/// [`rand`]: https://crates.io/crates/rand
pub fn new_v4() -> Self {
use rand::RngCore;
// TODO: change signature to support uuid's Error.
pub fn new_v4() -> Result<Uuid, getrandom::Error> {
ammgws marked this conversation as resolved.
Show resolved Hide resolved
let mut bytes = [0u8; 16];
getrandom::getrandom(&mut bytes)?;

let mut rng = rand::thread_rng();
let mut bytes = [0; 16];

rng.fill_bytes(&mut bytes);

Builder::from_bytes(bytes)
let uuid = crate::builder::Builder::from_bytes(bytes)
.set_variant(Variant::RFC4122)
.set_version(Version::Random)
.build()
.build();
Ok(uuid)
}
}

Expand All @@ -43,15 +41,15 @@ mod tests {

#[test]
fn test_new() {
let uuid = Uuid::new_v4();
let uuid = Uuid::new_v4().unwrap();

assert_eq!(uuid.get_version(), Some(Version::Random));
assert_eq!(uuid.get_variant(), Some(Variant::RFC4122));
}

#[test]
fn test_get_version() {
let uuid = Uuid::new_v4();
let uuid = Uuid::new_v4().unwrap();

assert_eq!(uuid.get_version(), Some(Version::Random));
assert_eq!(uuid.get_version_num(), 4)
Expand Down