-
Notifications
You must be signed in to change notification settings - Fork 191
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
from_rng_bytes added #172
Conversation
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(){ |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
🚲🏚: I think I also agree with @nox's suggestion of using |
Neither |
Hmm, I think |
name changed to from_random_bytes
This PR should include a patch to |
src/lib.rs
Outdated
/// assert_eq!(expected_uuid, uuid); | ||
/// ``` | ||
/// | ||
#[cfg(feature = "v4")] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok Cool.
`<[_]>::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
There was a problem hiding this 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); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
name changed to from_random_bytes
issue number removed from docs example changed
@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] }; |
There was a problem hiding this comment.
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 }
.
There was a problem hiding this 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!
bors: r+ |
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 -->
bors: r+ |
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 -->
I'm submitting a ...
Description
As discussed in the issue, this method is a solution to avoid making
rand
a public dependencyMotivation
To avoid making
rand
public dependencyTests
none, test added
Related Issue(s)
#170