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

Add Uuid Builder #332

Merged
merged 11 commits into from
Oct 5, 2018
Merged

Add Uuid Builder #332

merged 11 commits into from
Oct 5, 2018

Conversation

Veykril
Copy link
Contributor

@Veykril Veykril commented Oct 2, 2018

I'm submitting a feature

Description

This PR implements a basic UuidBuilder that takes itself by &mut instead of self as shown in the issue #171 description.

Motivation

Issue #171

Tests

Through Doctests

Related Issue(s)

#171

src/builder.rs Outdated
use super::Version;

/// A builder struct for creating a [`Uuid`]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove the Copy implementation here, because the behaviour of Copy types and &mut self methods can be surprising.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh interesting, I didn't know that this might cause weird behaviour. Will remove it then, this builder doesn't consume itself anyways so there is no need to copy it anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually removing the copy implementation will trigger the missing_copy_implementations lint.

Copy link
Member

@KodrAus KodrAus Oct 2, 2018

Choose a reason for hiding this comment

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

Actually, we should probably go ahead and remove all of those derives besides Debug, the others don't really make sense for a builder type.

We can override that lint with #[allow(missing_copy_implementations)] alongside this #[derive] attribute.

Copy link
Member

Choose a reason for hiding this comment

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

Oh interesting, I didn't know that this might cause weird behaviour.

Yeh, the wierdness is that a type that implements Copy can be implicitly copied around, so when you're passing the value around and calling mutating methods you can end up mutating a copy by accident.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, that makes never though of it like that. That is good thing to keep in mind.

@KodrAus
Copy link
Member

KodrAus commented Oct 2, 2018

Hi @Veykril 👋

I'm on mobile at the moment so just left a little comment from a quick scan.

I'd be tempted to mark the from_random_bytes method as deprecated now and suggest using this builder type.

@Veykril
Copy link
Contributor Author

Veykril commented Oct 2, 2018

Should I deprecate it with #[deprecated(since="0.7.2", note="please use the UuidBuilder instead")] then?

@Dylan-DPC-zz
Copy link
Member

Dylan-DPC-zz commented Oct 2, 2018 via email

@KodrAus
Copy link
Member

KodrAus commented Oct 2, 2018

@Dylan-DPC I think from_random_bytes was really only added specifically to call some of the mutating methods we now have on this builder without exposing them on Uuid, which is Copy. I don't think we need to hang on to APIs indefinitely when new approaches emerge, otherwise we could risk winding up with a large API surface area that's difficult to navigate. Pushing users towards the Builder for any non-standard initialisation seems like it would be helpful for users trying to find their way around.

@Dylan-DPC-zz
Copy link
Member

@KodrAus oops yeah just checked the function again. That's fine by me 👍

@Veykril
Copy link
Contributor Author

Veykril commented Oct 2, 2018

Alright, I made the proposed changes. Is the version I put into the attribute correct for this?

@Dylan-DPC-zz
Copy link
Member

yep @Veykril that's fine

@Dylan-DPC-zz Dylan-DPC-zz added this to the 0.7.2 milestone Oct 2, 2018
src/builder.rs Outdated
@@ -0,0 +1,238 @@
use super::Bytes;
use super::BytesError;
use super::Uuid;
Copy link
Member

Choose a reason for hiding this comment

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

use uuid::prelude::* where possible

src/lib.rs Outdated
@@ -137,6 +137,9 @@ extern crate sha1;
#[cfg_attr(test, macro_use)]
extern crate slog;

mod builder;
pub use builder::UuidBuilder;
Copy link
Member

Choose a reason for hiding this comment

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

make the builder module pub and instead of the reexport here, reexport in prelude module

src/lib.rs Outdated
@@ -619,6 +622,10 @@ impl Uuid {
///
/// assert_eq!(expected_uuid, uuid);
/// ```
#[deprecated(
since = "0.7.2",
note = "please use the `UuidBuilder` instead"
Copy link
Member

Choose a reason for hiding this comment

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

note = "use uuid::builder::UuidBuilder instead"

Copy link
Member

Choose a reason for hiding this comment

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

Something about uuid::builder::UuidBuilder doesn't sit right with me... We could just go with uuid::Builder?

Copy link
Member

Choose a reason for hiding this comment

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

yeah its not like we are going to have several builders so uuid::Builder should be fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So should I reexport the Builder via pub use builder::Builder; in the lib.rs as well as reexporting it through the prelude?

src/builder.rs Outdated
/// A builder struct for creating a [`Uuid`]
#[allow(missing_copy_implementations)]
#[derive(Debug)]
pub struct UuidBuilder {
Copy link
Member

Choose a reason for hiding this comment

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

maybe make this a newtype tuple? Just to make it consistent with the rest of the crate

src/builder.rs Outdated
/// "00000000-0000-0000-0000-000000000000"
/// );
/// ```
pub fn build(&self) -> Uuid {
Copy link
Member

Choose a reason for hiding this comment

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

pub fn build(&self) -> Uuid -> pub fn build(self) -> Uuid

Copy link
Member

Choose a reason for hiding this comment

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

We could make this method take &mut self, so that we have the future option to move the internal uuid out instead of copying it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used &self since that was what was given in the issue example, normally you'd consume the builder when building but since our internal type is copy both works here I'd say. What should I pick?

Copy link
Member

Choose a reason for hiding this comment

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

I would go with &mut self here, it's consistent with the other mutating methods we've added, and lets us do some extra work in the build method if we need it. Using &mut self over self means that we can chain the method calls in a single statement like this:

let uuid = Builder::from_slice(bytes).set_version(..).build();

If the build method took self by value then we wouldn't be able to chain it with set_version.

src/builder.rs Outdated
///
/// assert!(builder.is_err());
/// ```
pub fn from_slice(b: &[u8]) -> Result<Self, BytesError> {
Copy link
Member

Choose a reason for hiding this comment

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

maybe have two be/le from_slice functions instead?

src/builder.rs Outdated
@@ -0,0 +1,238 @@
use super::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 the copyright header (copy the one in lib.rs, first few lines)

@Veykril
Copy link
Contributor Author

Veykril commented Oct 5, 2018

I think I changed everything that was requested now

@kinggoesgaming
Copy link
Member

@Veykril can fix the build failures and then its good to merge

@kinggoesgaming
Copy link
Member

I dont know what that error message is 😕

@Dylan-DPC @KodrAus ideas?

@Veykril
Copy link
Contributor Author

Veykril commented Oct 5, 2018

Oh I think Rust 1.22 doesn't support nested import groups, yeah that feature was added in 1.25.

@Dylan-DPC-zz
Copy link
Member

bors: r+

bors bot added a commit that referenced this pull request Oct 5, 2018
332: Add Uuid Builder r=Dylan-DPC a=Veykril

**I'm submitting a feature**

# Description
This PR implements a basic UuidBuilder that takes itself by `&mut` instead of `self` as shown in the issue #171 description.

# Motivation
Issue #171

# Tests
Through Doctests

# Related Issue(s)
#171

Co-authored-by: Lukas Wirth <[email protected]>
Co-authored-by: Dylan DPC <[email protected]>
@bors
Copy link
Contributor

bors bot commented Oct 5, 2018

@bors bors bot merged commit 4f0caa5 into uuid-rs:master Oct 5, 2018
@kinggoesgaming kinggoesgaming mentioned this pull request Oct 5, 2018
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.

5 participants