Skip to content

Commit f177d20

Browse files
bors[bot]ammgws
andauthored
Merge #447
447: Use getrandom crate for uuid r=kinggoesgaming a=ammgws <!-- If this PR is a breaking change, ensure that you are opening it against the `breaking` branch. If the pull request is incomplete, prepend the Title with WIP: --> **I'm submitting a(n)** other # Description Use `getrandom` crate directly instead of `rand` for generating UUIDs using v4. # Motivation Reduce the number of dependencies used by the crate. Running the example in the README: - Before (10): c2-chacha, cfg-if, getrandom, libc, ppv-lite86, rand, rand_chacha, rand_core, rand_hc, wasi - After (4): cfg-if, getrandom, libc, wasi # Tests <!-- How are these changes tested? --> `cargo test` locally and test usage in a simple program. # Related Issue(s) # Other Wasn't sure whether I should bump the version here or not. Co-authored-by: Jason Nader <jason@kayoway.com> Co-authored-by: Jason <ammgws@users.noreply.github.com>
2 parents 257b0e5 + 4899012 commit f177d20

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ status = "actively-developed"
5454
[badges.travis-ci]
5555
repository = "uuid-rs/uuid"
5656

57-
[dependencies.md5]
57+
[dependencies.getrandom]
5858
optional = true
59-
version = "0.7"
59+
version = "0.1"
6060

61-
[dependencies.rand]
61+
[dependencies.md5]
6262
optional = true
6363
version = "0.7"
6464

@@ -92,12 +92,12 @@ version = "1.0.56"
9292
default = ["std"]
9393
guid = ["winapi"]
9494
std = []
95-
stdweb = [ "rand/stdweb" ]
95+
stdweb = ["getrandom"]
9696
v1 = []
9797
v3 = ["md5"]
98-
v4 = ["rand"]
98+
v4 = ["getrandom"]
9999
v5 = ["sha1"]
100-
wasm-bindgen = ["rand/wasm-bindgen"]
100+
wasm-bindgen = ["getrandom"]
101101

102102
[target.'cfg(windows)'.dependencies.winapi]
103103
optional = true

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ To create a new random (V4) UUID and print it out in hexadecimal form:
9595

9696
use uuid::Uuid;
9797

98-
fn main() {
99-
let my_uuid = Uuid::new_v4();
98+
fn main() -> Result<(), Box<std::error::Error> {
99+
let my_uuid = Uuid::new_v4()?;
100100
println!("{}", my_uuid);
101101
}
102102
```

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
9393
//!
9494
//! use uuid::Uuid;
9595
//!
96-
//! fn main() {
97-
//! let my_uuid = Uuid::new_v4();
96+
//! fn main() -> Result<(), Box<std::error::Error> {
97+
//! let my_uuid = Uuid::new_v4()?;
9898
//! println!("{}", my_uuid);
9999
//! }
100100
//! ```

src/v4.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::prelude::*;
2-
use rand;
32

43
impl Uuid {
54
/// Creates a random UUID.
65
///
7-
/// This uses the [`rand`] crate's default task RNG as the source of random
8-
/// numbers. If you'd like to use a custom generator, don't use this
9-
/// method: use the `rand::Rand trait`'s `rand()` method instead.
6+
/// This uses the [`getrandom`] crate to utilise the operating system's RNG
7+
/// as the source of random numbers. If you'd like to use a custom generator,
8+
/// don't use this method: use the `rand::Rand trait`'s `rand()` method instead.
109
///
1110
/// Note that usage of this method requires the `v4` feature of this crate
1211
/// to be enabled.
@@ -21,19 +20,18 @@ impl Uuid {
2120
/// let uuid = Uuid::new_v4();
2221
/// ```
2322
///
23+
/// [`getrandom`]: https://crates.io/crates/getrandom
2424
/// [`rand`]: https://crates.io/crates/rand
25-
pub fn new_v4() -> Self {
26-
use rand::RngCore;
25+
// TODO: change signature to support uuid's Error.
26+
pub fn new_v4() -> Result<Uuid, getrandom::Error> {
27+
let mut bytes = [0u8; 16];
28+
getrandom::getrandom(&mut bytes)?;
2729

28-
let mut rng = rand::thread_rng();
29-
let mut bytes = [0; 16];
30-
31-
rng.fill_bytes(&mut bytes);
32-
33-
Builder::from_bytes(bytes)
30+
let uuid = crate::builder::Builder::from_bytes(bytes)
3431
.set_variant(Variant::RFC4122)
3532
.set_version(Version::Random)
36-
.build()
33+
.build();
34+
Ok(uuid)
3735
}
3836
}
3937

@@ -43,15 +41,15 @@ mod tests {
4341

4442
#[test]
4543
fn test_new() {
46-
let uuid = Uuid::new_v4();
44+
let uuid = Uuid::new_v4().unwrap();
4745

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

5250
#[test]
5351
fn test_get_version() {
54-
let uuid = Uuid::new_v4();
52+
let uuid = Uuid::new_v4().unwrap();
5553

5654
assert_eq!(uuid.get_version(), Some(Version::Random));
5755
assert_eq!(uuid.get_version_num(), 4)

0 commit comments

Comments
 (0)