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

Un-breaking-change Uuid::new_v4 #503

Merged
merged 1 commit into from
Dec 21, 2020
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
25 changes: 13 additions & 12 deletions src/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ impl Uuid {
/// Creates a random UUID.
///
/// This uses the [`getrandom`] crate to utilise the operating system's RNG
/// as the source of random numbers. If you'd like to use a custom generator,
/// don't use this method: generate random bytes using your custom generator
/// and pass them to the [`uuid::Builder::from_bytes`][from_bytes] function
/// instead.
/// as the source of random numbers. If you'd like to use a custom
/// generator, don't use this method: generate random bytes using your
/// custom generator and pass them to the
/// [`uuid::Builder::from_bytes`][from_bytes] function instead.
///
/// Note that usage of this method requires the `v4` feature of this crate
/// to be enabled.
Expand All @@ -24,16 +24,17 @@ impl Uuid {
///
/// [`getrandom`]: https://crates.io/crates/getrandom
/// [from_bytes]: struct.Builder.html#method.from_bytes
// TODO: change signature to support uuid's Error.
pub fn new_v4() -> Result<Uuid, getrandom::Error> {
pub fn new_v4() -> Uuid {
let mut bytes = [0u8; 16];
getrandom::getrandom(&mut bytes)?;
getrandom::getrandom(&mut bytes).unwrap_or_else(|err| {
// NB: getrandom::Error has no source; this is adequate display
panic!("could not retreive random bytes for uuid: {}", err)
});

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

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

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

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().unwrap();
let uuid = Uuid::new_v4();

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