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

from_rng_bytes added #172

Merged
merged 27 commits into from
Mar 20, 2018
Merged

from_rng_bytes added #172

merged 27 commits into from
Mar 20, 2018

Conversation

Dylan-DPC-zz
Copy link
Member

I'm submitting a ...

  • bug fix
  • feature enhancement
  • deprecation or removal
  • refactor

Description

As discussed in the issue, this method is a solution to avoid making rand a public dependency

Motivation

To avoid making rand public dependency

Tests

none, test added

Related Issue(s)

#170

src/lib.rs Outdated
@@ -1804,6 +1831,20 @@ mod tests {
assert_eq!(&b_in, b_out);
}

#[test]
#[cfg(feature = "v4")]
fn test_from_rng_bytes(){
Copy link
Member

Choose a reason for hiding this comment

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

Add space before {, rustfmt is complaining

src/lib.rs Outdated
#[cfg(feature = "v4")]
pub fn from_rng_bytes(b: [u8; 16]) -> Uuid {
let mut uuid = Uuid { bytes: [0; 16] };
copy_memory(&mut uuid.bytes, &b);
Copy link
Member

Choose a reason for hiding this comment

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

We also need to call set_variant and set_version here since the random bytes probably won't have the right values:

let mut uuid = Uuid { bytes: [0; 16] };
copy_memory(&mut uuid.bytes, &b);

uuid.set_variant(UuidVariant::RFC4122);
uuid.set_version(UuidVersion::Random);

uuid

@KodrAus
Copy link
Member

KodrAus commented Mar 18, 2018

🚲🏚: I think I also agree with @nox's suggestion of using from_random_bytes instead of from_rng_bytes, since they're not really random number generator bytes, they're just random bytes.

@nox
Copy link
Contributor

nox commented Mar 18, 2018

Neither from_rng_bytes nor from_random_bytes convey that we are building a v4 UUID, and those names are inconsistent with Uuid::new_v4.

@KodrAus
Copy link
Member

KodrAus commented Mar 18, 2018

Hmm, I think Uuid::from_random_bytes(b) conveys that at least as well as rng.gen::<Uuid>() did.

name changed to from_random_bytes
@nox
Copy link
Contributor

nox commented Mar 18, 2018

This PR should include a patch to Uuid::new_v4 to make it call the new method we are introducing.

src/lib.rs Outdated
/// assert_eq!(expected_uuid, uuid);
/// ```
///
#[cfg(feature = "v4")]
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no reason to feature-gate the method at all.

Copy link
Member Author

Choose a reason for hiding this comment

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

We are only generating v4 uuids from this. So why have it on other features?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because there is no reason to have to bring in rand if one does not need it. For example in Servo we won't use new_v4 at all (and thus the rand in uuid) because we need a better RNG.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok Cool.

Dylan-DPC and others added 7 commits March 18, 2018 17:41
`<[_]>::copy_from_slice` is a better replacement

Signed-off-by: Hunar Roop Kahlon <hunar.roop@gmail.com>
175: Delete copy_memory function r=Dylan-DPC a=kinggoesgaming

<!--
    As we are working towards a stable version of uuid, we require that you 
    open an issue, before submitting a pull request. If the pull request is 
    imcomplete, prepend the Title with WIP: 
-->

**I'm submitting a ...**
  - [ ] bug fix
  - [ ] feature enhancement
  - [x] deprecation or removal
  - [x] refactor

# Description
Replace `uuid::copy_memory` with `<[_]>::copy_from_slice`

# Motivation
`core` already provides a good optimized version of `slice` copying function, so we dont need to create our own duplicate code.

# Tests
Our tests and benchmarks are running good, other than one or two runs which suggest a 1-2 nanosecond slowdown.

# Related Issue(s)
#173
Copy link
Member

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

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

Thanks @Dylan-DPC! Just a few nitpicks.

src/lib.rs Outdated
///
pub fn from_random_bytes(b: [u8; 16]) -> Uuid {
let mut uuid = Uuid { bytes: [0; 16] };
copy_memory(&mut uuid.bytes, &b);
Copy link
Member

Choose a reason for hiding this comment

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

This wil need to be rebased now since copy_memory is now gone.

/// Basic usage:
///
/// ```
/// use uuid::Uuid;
Copy link
Member

Choose a reason for hiding this comment

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

I still think we should add an example that's a little more motivating for people using this function, I don't think the value of the bytes we pass in, nor the assertion, should be visible in the remdered docs.

Copy link
Member Author

@Dylan-DPC-zz Dylan-DPC-zz Mar 19, 2018

Choose a reason for hiding this comment

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

I just copied it from the examples given for the other functions. Will change it accordingly.

Edit: It is common to display the assertion in rust examples. I have seen it in other crates as well and we are doing it for the other examples too.

src/lib.rs Outdated
@@ -685,6 +684,34 @@ impl Uuid {
Uuid { bytes: b }
}

/// Creates a v4 Uuid from rng bytes (ref issue #170)
Copy link
Member

Choose a reason for hiding this comment

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

Probably don't need to reference GitHub issues in the docs prose.

Copy link
Member Author

Choose a reason for hiding this comment

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

sure. Since we have so many confusing functions to generate a UUID wanted to leave a note on why do we need this one 😄 will remove it

@kinggoesgaming
Copy link
Member

@KodrAus any further nits? Else can you approve?

src/lib.rs Outdated
/// ```
///
pub fn from_random_bytes(b: [u8; 16]) -> Uuid {
let mut uuid = Uuid { bytes: [0; 16] };
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to create a new empty slice here and copy b into it, we could just create a Uuid { bytes: b }.

Copy link
Member

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

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

Thanks @Dylan-DPC! Looks good to me!

@Dylan-DPC-zz
Copy link
Member Author

bors: r+

bors bot added a commit that referenced this pull request Mar 20, 2018
172: from_rng_bytes added r=Dylan-DPC a=Dylan-DPC

<!--
    As we are working towards a stable version of uuid, we require that you 
    open an issue, before submitting a pull request. If the pull request is 
    imcomplete, prepend the Title with WIP: 
-->

**I'm submitting a ...**
  - [ ] bug fix
  - [x] feature enhancement
  - [ ] deprecation or removal
  - [ ] refactor

# Description
<!-- Provide a summary of your changes in the Title above -->
As discussed in the issue, this method is a solution to avoid making `rand` a public dependency

# Motivation
To avoid making `rand` public dependency
<!-- Why is this change required -->

# Tests
<!-- How are these changes tested? -->
none, test added 

# Related Issue(s)
#170 
<!-- 
    As noted above, we require an issue for every PR. Please link to the issue
    here
-->
@kinggoesgaming kinggoesgaming changed the title from_rng_bytes added wip: from_rng_bytes added Mar 20, 2018
@Dylan-DPC-zz Dylan-DPC-zz changed the title wip: from_rng_bytes added [WIP] from_rng_bytes added Mar 20, 2018
@kinggoesgaming kinggoesgaming changed the title [WIP] from_rng_bytes added from_rng_bytes added Mar 20, 2018
@kinggoesgaming kinggoesgaming changed the title from_rng_bytes added WIP from_rng_bytes added Mar 20, 2018
@kinggoesgaming kinggoesgaming changed the title WIP from_rng_bytes added from_rng_bytes added Mar 20, 2018
@kinggoesgaming
Copy link
Member

bors: r+

bors bot added a commit that referenced this pull request Mar 20, 2018
172: from_rng_bytes added r=kinggoesgaming a=Dylan-DPC

<!--
    As we are working towards a stable version of uuid, we require that you 
    open an issue, before submitting a pull request. If the pull request is 
    imcomplete, prepend the Title with WIP: 
-->

**I'm submitting a ...**
  - [ ] bug fix
  - [x] feature enhancement
  - [ ] deprecation or removal
  - [ ] refactor

# Description
<!-- Provide a summary of your changes in the Title above -->
As discussed in the issue, this method is a solution to avoid making `rand` a public dependency

# Motivation
To avoid making `rand` public dependency
<!-- Why is this change required -->

# Tests
<!-- How are these changes tested? -->
none, test added 

# Related Issue(s)
#170 
<!-- 
    As noted above, we require an issue for every PR. Please link to the issue
    here
-->
@bors
Copy link
Contributor

bors bot commented Mar 20, 2018

@bors bors bot merged commit d4c2963 into master Mar 20, 2018
@kinggoesgaming kinggoesgaming deleted the feature/from-rng branch March 25, 2018 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants