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

Remove the set_version and set_variant methods #400

Merged
merged 8 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
29 changes: 22 additions & 7 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::prelude::*;
/// ```
#[allow(missing_copy_implementations)]
#[derive(Debug)]
pub struct Builder(crate::Uuid);
pub struct Builder(crate::Bytes);

impl Builder {
/// Creates a `Builder` using the supplied big-endian bytes.
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Builder {
/// let uuid = Builder::from_bytes(bytes);
/// ```
pub const fn from_bytes(b: Bytes) -> Self {
Builder(crate::Uuid::from_bytes(b))
Builder(b)
}

/// Creates a `Builder` using the supplied big-endian bytes.
Expand Down Expand Up @@ -168,7 +168,11 @@ impl Builder {
d3: u16,
d4: &[u8],
) -> Result<Self, crate::BytesError> {
crate::Uuid::from_fields(d1, d2, d3, d4).map(Builder)
crate::Uuid::from_fields(d1, d2, d3, d4).map(|uuid| {
let bytes = uuid.as_bytes().to_owned();

Builder::from_bytes(bytes)
})
}

/// Creates a `Builder` with an initial [`Uuid::nil`]
Expand All @@ -187,18 +191,27 @@ impl Builder {
/// );
/// ```
pub const fn nil() -> Self {
Builder(crate::Uuid::nil())
Builder([0; 16])
}

/// Specifies the variant of the internal [`Uuid`].
pub fn set_variant(&mut self, v: crate::Variant) -> &mut Self {
self.0.set_variant(v);
let byte = self.0[8];

self.0[8] = match v {
crate::Variant::NCS => byte & 0x7f,
crate::Variant::RFC4122 => (byte & 0x3f) | 0x80,
crate::Variant::Microsoft => (byte & 0x1f) | 0xc0,
crate::Variant::Future => (byte & 0x1f) | 0xe0,
};

self
}

/// Specifies the version number of the internal [`Uuid`].
pub fn set_version(&mut self, v: crate::Version) -> &mut Self {
self.0.set_version(v);
self.0[6] = (self.0[6] & 0x0f) | ((v as u8) << 4);

self
}

Expand All @@ -218,6 +231,8 @@ impl Builder {
/// );
/// ```
pub fn build(&mut self) -> Uuid {
self.0
let uuid = crate::Uuid::from_bytes(self.0);

uuid
}
}
17 changes: 1 addition & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,17 +527,6 @@ impl Uuid {
Uuid(bytes)
}

/// Specifies the variant of the UUID structure
fn set_variant(&mut self, v: Variant) {
// Octet 8 contains the variant in the most significant 3 bits
self.0[8] = match v {
Variant::NCS => self.as_bytes()[8] & 0x7f, // b0xx...
Variant::RFC4122 => (self.as_bytes()[8] & 0x3f) | 0x80, // b10x...
Variant::Microsoft => (self.as_bytes()[8] & 0x1f) | 0xc0, // b110...
Variant::Future => (self.as_bytes()[8] & 0x1f) | 0xe0, // b111...
}
}

/// Returns the variant of the `Uuid` structure.
///
/// This determines the interpretation of the structure of the UUID.
Expand All @@ -554,11 +543,6 @@ impl Uuid {
}
}

/// Specifies the version number of the `Uuid`.
fn set_version(&mut self, v: Version) {
self.0[6] = (self.as_bytes()[6] & 0xF) | ((v as u8) << 4);
}

/// Returns the version number of the `Uuid`.
///
/// This represents the algorithm used to generate the contents.
Expand Down Expand Up @@ -956,6 +940,7 @@ mod tests {
Uuid::new_v3(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());

assert_eq!(uuid.get_version().unwrap(), Version::Md5);
println!("pass1");
kinggoesgaming marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(uuid.get_version_num(), 3);
}

Expand Down
14 changes: 10 additions & 4 deletions src/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ impl Uuid {
context.consume(namespace.as_bytes());
context.consume(name);

let mut uuid = Uuid::from_bytes(context.compute().into());
let computed = context.compute();
let bytes = computed.into();

uuid.set_variant(Variant::RFC4122);
uuid.set_version(Version::Md5);
uuid
let mut builder =
crate::builder::Builder::from_bytes(bytes);

builder
.set_variant(Variant::RFC4122)
.set_version(Version::Md5);

builder.build()
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ impl Uuid {
hash.update(name);

let buffer = hash.digest().bytes();
let mut uuid = Uuid::default();

uuid.0.copy_from_slice(&buffer[..16]);
uuid.set_variant(Variant::RFC4122);
uuid.set_version(Version::Sha1);
let mut bytes = crate::Bytes::default();
bytes.copy_from_slice(&buffer[..16]);

uuid
let mut builder = crate::builder::Builder::from_bytes(bytes);
builder
.set_variant(Variant::RFC4122)
.set_version(Version::Sha1);

builder.build()
}
}

Expand Down