Merge pull request #589 from uuid-rs/chore/dep-refactor

Rework optional dependencies to discourage direct use as Cargo features
This commit is contained in:
Ashley Mannix 2022-02-14 08:36:32 +10:00 committed by GitHub
commit 5aa53c880c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 135 additions and 96 deletions

View File

@ -53,54 +53,20 @@ repository = "uuid-rs/uuid"
[features]
default = ["std"]
std = []
macro-diagnostics = ["uuid-macro-internal"]
macro-diagnostics = ["private_uuid-macro-internal"]
v1 = ["atomic"]
v3 = ["md-5"]
v1 = ["private_atomic"]
v3 = ["md5"]
v4 = ["rng"]
v5 = ["sha-1"]
v5 = ["sha1"]
js = ["getrandom", "getrandom/js"]
js = ["private_getrandom", "private_getrandom/js"]
rng = ["getrandom"]
fast-rng = ["rng", "rand"]
rng = ["private_getrandom"]
fast-rng = ["rng", "private_rand"]
# Unstable features (these also need RUSTFLAGS="--cfg uuid_unstable" to work)
zerocopy-unstable = ["zerocopy"]
# Private
[dependencies.getrandom]
optional = true
version = "0.2"
# Private
[dependencies.rand]
optional = true
version = "0.8"
# Private
[dependencies.atomic]
default-features = false
optional = true
version = "0.5"
# Private
[dependencies.md-5]
default-features = false
optional = true
version = "0.10"
# Private
[dependencies.sha-1]
default-features = false
optional = true
version = "0.9"
# Public: Re-exported
[dependencies.uuid-macro-internal]
version = "1.0.0-alpha.1"
path = "macros"
optional = true
sha1 = ["private_sha1"]
md5 = ["private_md-5"]
# Public: Used in trait impls on `Uuid`
[dependencies.serde]
@ -119,10 +85,65 @@ optional = true
version = "1"
# Public (unstable): Used in `zerocopy` derive
# Unstable: also need RUSTFLAGS="--cfg uuid_unstable" to work
# This feature may break between releases, or be removed entirely before
# stabilization.
# See: https://github.com/uuid-rs/uuid/issues/588
[dependencies.zerocopy]
optional = true
version = "0.6"
# Private
# Don't depend on this optional feature directly: it may change at any time
# use the `rng` feature instead
[dependencies.private_getrandom]
package = "getrandom"
optional = true
version = "0.2"
# Private
# Don't depend on this optional feature directly: it may change at any time
# use the `fast-rng` feature instead
[dependencies.private_rand]
package = "rand"
optional = true
version = "0.8"
# Private
# Don't depend on this optional feature directly: it may change at any time
# Use the `md5` feature instead
[dependencies.private_md-5]
package = "md-5"
default-features = false
optional = true
version = "0.10"
# Private
# Don't depend on this optional feature directly: it may change at any time
# Use the `sha1` feature instead
[dependencies.private_sha1]
package = "sha1"
default-features = false
optional = true
version = "0.10"
# Public: Re-exported
# Don't depend on this optional feature directly: it may change at any time
# Use the `macro-diagnostics` feature instead
[dependencies.private_uuid-macro-internal]
package = "uuid-macro-internal"
version = "1.0.0-alpha.1"
path = "macros"
optional = true
# Private
# Don't depend on this optional feature directly: it may change at any time
[dependencies.private_atomic]
package = "atomic"
default-features = false
optional = true
version = "0.5"
[dev-dependencies.bincode]
version = "1.0"

View File

@ -544,6 +544,20 @@ impl Builder {
.with_version(Version::Random)
}
/// Creates a `Builder` using the supplied MD5 hashed bytes.
pub const fn from_md5_bytes(b: Bytes) -> Self {
Builder(Uuid::from_bytes(b))
.with_variant(Variant::RFC4122)
.with_version(Version::Md5)
}
/// Creates a `Builder` using the supplied SHA1 hashed bytes.
pub const fn from_sha1_bytes(b: Bytes) -> Self {
Builder(Uuid::from_bytes(b))
.with_variant(Variant::RFC4122)
.with_version(Version::Sha1)
}
/// Creates a `Builder` using the supplied bytes.
///
/// # Errors

View File

@ -97,7 +97,7 @@
//! Some features are unstable. They may be incomplete or depend on other
//! unstable libraries. These include:
//!
//! * `zerocopy-unstable` - adds support for zero-copy deserialization using the
//! * `zerocopy` - adds support for zero-copy deserialization using the
//! `zerocopy` library.
//!
//! Unstable features may break between minor releases.
@ -212,10 +212,10 @@ extern crate std;
extern crate core as std;
// Check that unstable features are accompanied by a the `uuid_unstable` cfg
#[cfg(all(not(uuid_unstable), feature = "zerocopy-unstable"))]
compile_error!("The `zerocopy-unstable` feature is unstable and may break between releases. Please also pass `RUSTFLAGS=\"--cfg uuid_unstable\"` to allow it.");
#[cfg(all(not(uuid_unstable), feature = "zerocopy"))]
compile_error!("The `zerocopy` feature is unstable and may break between releases. Please also pass `RUSTFLAGS=\"--cfg uuid_unstable\"` to allow it.");
#[cfg(feature = "zerocopy-unstable")]
#[cfg(feature = "zerocopy")]
use zerocopy::{AsBytes, FromBytes, Unaligned};
mod builder;
@ -235,6 +235,10 @@ mod v5;
#[cfg(feature = "rng")]
mod rng;
#[cfg(feature = "md5")]
mod md5;
#[cfg(feature = "sha1")]
mod sha1;
mod external;
@ -243,7 +247,7 @@ mod macros;
#[doc(hidden)]
#[cfg(feature = "macro-diagnostics")]
pub extern crate uuid_macro_internal;
pub extern crate private_uuid_macro_internal;
use crate::std::convert;
@ -392,7 +396,7 @@ pub enum Variant {
/// The `Uuid` type is always guaranteed to be have the same ABI as [`Bytes`].
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(
feature = "zerocopy-unstable",
feature = "zerocopy",
derive(AsBytes, FromBytes, Unaligned)
)]
#[repr(transparent)]

View File

@ -5,7 +5,7 @@ macro_rules! define_uuid_macro {
#[macro_export]
macro_rules! uuid {
($uuid:literal) => {{
$crate::Uuid::from_bytes($crate::uuid_macro_internal::parse_lit!($uuid))
$crate::Uuid::from_bytes($crate::private_uuid_macro_internal::parse_lit!($uuid))
}};
}

14
src/md5.rs Normal file
View File

@ -0,0 +1,14 @@
#[cfg(feature = "v3")]
pub(crate) fn hash(ns: &[u8], src: &[u8]) -> [u8; 16] {
use private_md_5::{Md5, Digest};
let mut hasher = Md5::new();
hasher.update(ns);
hasher.update(src);
let mut bytes = [0; 16];
bytes.copy_from_slice(&hasher.finalize()[..16]);
bytes
}

View File

@ -4,7 +4,7 @@ pub(crate) fn bytes() -> [u8; 16] {
{
let mut bytes = [0u8; 16];
getrandom::getrandom(&mut bytes).unwrap_or_else(|err| {
private_getrandom::getrandom(&mut bytes).unwrap_or_else(|err| {
// NB: getrandom::Error has no source; this is adequate display
panic!("could not retrieve random bytes for uuid: {}", err)
});
@ -14,7 +14,7 @@ pub(crate) fn bytes() -> [u8; 16] {
#[cfg(feature = "fast-rng")]
{
rand::random()
private_rand::random()
}
}
@ -24,7 +24,7 @@ pub(crate) fn u16() -> u16 {
{
let mut bytes = [0u8; 2];
getrandom::getrandom(&mut bytes).unwrap_or_else(|err| {
private_getrandom::getrandom(&mut bytes).unwrap_or_else(|err| {
// NB: getrandom::Error has no source; this is adequate display
panic!("could not retrieve random bytes for uuid: {}", err)
});
@ -34,6 +34,6 @@ pub(crate) fn u16() -> u16 {
#[cfg(feature = "fast-rng")]
{
rand::random()
private_rand::random()
}
}

14
src/sha1.rs Normal file
View File

@ -0,0 +1,14 @@
#[cfg(feature = "v5")]
pub(crate) fn hash(ns: &[u8], src: &[u8]) -> [u8; 16] {
use private_sha1::{Sha1, Digest};
let mut hasher = Sha1::new();
hasher.update(ns);
hasher.update(src);
let mut bytes = [0; 16];
bytes.copy_from_slice(&hasher.finalize()[..16]);
bytes
}

View File

@ -5,7 +5,7 @@
use crate::{Uuid, Version};
use atomic::Atomic;
use private_atomic::{Atomic, Ordering};
/// The number of 100 ns ticks between the UUID epoch
/// `1582-10-15 00:00:00` and the Unix epoch `1970-01-01 00:00:00`.
@ -295,7 +295,7 @@ impl ClockSequence for Context {
// increment the clock sequence we want to wrap once it becomes larger
// than what we can represent in a "u14". Otherwise there'd be patches
// where the clock sequence doesn't change regardless of the timestamp
self.count.fetch_add(1, atomic::Ordering::AcqRel) % (u16::MAX >> 2)
self.count.fetch_add(1, Ordering::AcqRel) % (u16::MAX >> 2)
}
}

View File

@ -1,6 +1,4 @@
use crate::{Uuid, Variant, Version};
use md5::{Digest, Md5};
use crate::Uuid;
impl Uuid {
/// Creates a UUID using a name from a namespace, based on the MD5
@ -32,22 +30,7 @@ impl Uuid {
/// [`NAMESPACE_URL`]: #associatedconstant.NAMESPACE_URL
/// [`NAMESPACE_X500`]: #associatedconstant.NAMESPACE_X500
pub fn new_v3(namespace: &Uuid, name: &[u8]) -> Uuid {
let mut hasher = Md5::new();
hasher.update(namespace.as_bytes());
hasher.update(name);
let buffer = hasher.finalize();
let mut bytes = crate::Bytes::default();
bytes.copy_from_slice(&buffer[..16]);
let mut builder = crate::Builder::from_bytes(bytes);
builder
.set_variant(Variant::RFC4122)
.set_version(Version::Md5);
builder.into_uuid()
crate::Builder::from_md5_bytes(crate::md5::hash(namespace.as_bytes(), name)).into_uuid()
}
}
@ -58,7 +41,10 @@ mod tests {
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
use crate::std::string::ToString;
use crate::{
Variant, Version,
std::string::ToString,
};
static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
(

View File

@ -1,6 +1,4 @@
use crate::{Uuid, Variant, Version};
use sha1::{Digest, Sha1};
use crate::Uuid;
impl Uuid {
/// Creates a UUID using a name from a namespace, based on the SHA-1 hash.
@ -31,22 +29,7 @@ impl Uuid {
/// [`NAMESPACE_URL`]: struct.Uuid.html#associatedconst.NAMESPACE_URL
/// [`NAMESPACE_X500`]: struct.Uuid.html#associatedconst.NAMESPACE_X500
pub fn new_v5(namespace: &Uuid, name: &[u8]) -> Uuid {
let mut hasher = Sha1::new();
hasher.update(namespace.as_bytes());
hasher.update(name);
let buffer = hasher.finalize();
let mut bytes = crate::Bytes::default();
bytes.copy_from_slice(&buffer[..16]);
let mut builder = crate::Builder::from_bytes(bytes);
builder
.set_variant(Variant::RFC4122)
.set_version(Version::Sha1);
builder.into_uuid()
crate::Builder::from_sha1_bytes(crate::sha1::hash(namespace.as_bytes(), name)).into_uuid()
}
}
@ -57,7 +40,10 @@ mod tests {
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
use crate::std::string::ToString;
use crate::{
Variant, Version,
std::string::ToString,
};
static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
(