Skip to content

Commit

Permalink
Use getrandom crate for uuid
Browse files Browse the repository at this point in the history
Reduces the number of dependencies.
  • Loading branch information
ammgws committed Jan 19, 2020
1 parent 257b0e5 commit 97e9bcf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ status = "actively-developed"
[badges.travis-ci]
repository = "uuid-rs/uuid"

[dependencies.getrandom]
optional = true
version = "0.1"

[dependencies.md5]
optional = true
version = "0.7"
Expand Down Expand Up @@ -95,7 +99,7 @@ std = []
stdweb = [ "rand/stdweb" ]
v1 = []
v3 = ["md5"]
v4 = ["rand"]
v4 = ["getrandom"]
v5 = ["sha1"]
wasm-bindgen = ["rand/wasm-bindgen"]

Expand Down
24 changes: 8 additions & 16 deletions src/v4.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
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.
///
/// Note that usage of this method requires the `v4` feature of this crate
/// to be enabled.
///
Expand All @@ -21,19 +16,16 @@ impl Uuid {
/// let uuid = Uuid::new_v4();
/// ```
///
/// [`rand`]: https://crates.io/crates/rand
pub fn new_v4() -> Self {
use rand::RngCore;

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

rng.fill_bytes(&mut bytes);
pub fn new_v4() -> Result<Uuid, getrandom::Error> {
let mut bytes = [0u8; 16];
getrandom::getrandom(&mut bytes)?;

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

Expand Down

0 comments on commit 97e9bcf

Please sign in to comment.