-
Notifications
You must be signed in to change notification settings - Fork 213
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
Add Uuid Builder #332
Conversation
src/builder.rs
Outdated
use super::Version; | ||
|
||
/// A builder struct for creating a [`Uuid`] | ||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] |
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.
Let's remove the Copy
implementation here, because the behaviour of Copy
types and &mut self
methods can be surprising.
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.
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.
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.
Actually removing the copy implementation will trigger the missing_copy_implementations
lint.
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.
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.
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.
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.
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.
Ah yes, that makes never though of it like that. That is good thing to keep in mind.
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 |
Should I deprecate it with |
I don't think it's a good idea to deprecate we are just giving the users to
use a builder pattern if they want to.
…On Tue, Oct 2, 2018, 16:04 Lukas Wirth ***@***.***> wrote:
Should I deprecate it with #[deprecated(since="0.7.2", note="please use
theUuidBuilderinstead")] then?
—
You are receiving this because your review was requested.
Reply to this email directly, view it on GitHub
<#332 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ALzEtR-S-qrWTiXaSvCwO82aJWvZ8ZxOks5ug0EigaJpZM4XDnvz>
.
|
@Dylan-DPC I think |
@KodrAus oops yeah just checked the function again. That's fine by me 👍 |
Alright, I made the proposed changes. Is the version I put into the attribute correct for this? |
yep @Veykril that's fine |
src/builder.rs
Outdated
@@ -0,0 +1,238 @@ | |||
use super::Bytes; | |||
use super::BytesError; | |||
use super::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.
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; |
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.
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" |
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.
note = "use
uuid::builder::UuidBuilder instead"
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.
Something about uuid::builder::UuidBuilder
doesn't sit right with me... We could just go with uuid::Builder
?
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.
yeah its not like we are going to have several builders so uuid::Builder
should be fine
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.
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 { |
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.
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 { |
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.
pub fn build(&self) -> Uuid
-> pub fn build(self) -> 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.
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.
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 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?
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 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> { |
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.
maybe have two be/le from_slice
functions instead?
src/builder.rs
Outdated
@@ -0,0 +1,238 @@ | |||
use super::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 the copyright header (copy the one in lib.rs, first few lines)
I think I changed everything that was requested now |
@Veykril can fix the build failures and then its good to merge |
I dont know what that error message is 😕 @Dylan-DPC @KodrAus ideas? |
Oh I think Rust 1.22 doesn't support nested import groups, yeah that feature was added in 1.25. |
bors: r+ |
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]>
I'm submitting a feature
Description
This PR implements a basic UuidBuilder that takes itself by
&mut
instead ofself
as shown in the issue #171 description.Motivation
Issue #171
Tests
Through Doctests
Related Issue(s)
#171