Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ repository = "uuid-rs/uuid"
[features]
default = ["std"]
std = []
macros = ["uuid_macro"]
macro-diagnostics = ["uuid_macro"]

v1 = ["atomic"]
v3 = ["md-5"]
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ let my_uuid = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8")?;
assert_eq!(Some(Version::Random), my_uuid.get_version());
```

If you add the `macros` feature then you can parse UUIDs at compile time
instead of at runtime:
You can parse UUIDs at compile time instead of at runtime.

If you add the `macro-diagnostics` feature then you can see much better
error messages.

```rust
#[macro_use]
Expand All @@ -78,7 +80,7 @@ various pieces of functionality:
generate a `Uuid`.
* `v5` - adds the `Uuid::new_v5` function and the ability to create a V5
UUID based on the SHA1 hash of some data.
* `macros` - adds the `uuid!` macro that can parse UUIDs at compile time.
* `macro-diagnostics` - enhances the diagnostics of `uuid!` macro.
* `serde` - adds the ability to serialize and deserialize a `Uuid` using the
`serde` crate.
* `arbitrary` - adds an `Arbitrary` trait implementation to `Uuid`.
Expand Down
5 changes: 3 additions & 2 deletions examples/uuid_macro.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Using the `uuid!` macro.
//!
//! If you enable the `macros` feature you can use the `uuid!` macro.
//! `uuid!` will parse encoded UUIDs at compile time instead of at runtime.
//! If you've got a fixed UUID string handy then consider using `uuid!` instead
//! of `Uuid::parse_str` or `str::parse`.
//!
//! If you enable the `macro-diagnostics` feature, you can see much better
//! error messages.

#[test]
#[cfg(feature = "macros")]
fn parse_uuid_at_compile_time() {
use uuid::uuid;

Expand Down
4 changes: 2 additions & 2 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Implementation details for the `uuid!` macro.
//!
//! This crate is not meant to be used directly. Instead,
//! you can use the `macros` feature of `uuid`:
//! you can use the `macro-diagnostics` feature of `uuid`:
//!
//! ```toml
//! [dependencies.uuid]
//! features = ["macros"]
//! features = ["macro-diagnostics"]
//! ```

use proc_macro::TokenStream;
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
//!
//! Other crate features can also be useful beyond the version support:
//!
//! * `macros` - adds the `uuid!` macro that can parse UUIDs from string literals at compile time.
//! * `macro-diagnostics` - enhances the diagnostics of `uuid!` macro.
//! * `serde` - adds the ability to serialize and deserialize a UUID using
//! `serde`.
//! * `arbitrary` - adds an `Arbitrary` trait implementation to `Uuid` for
Expand Down Expand Up @@ -241,11 +241,11 @@ mod rng;

mod external;

#[cfg(feature = "macros")]
#[macro_use]
mod macros;

#[doc(hidden)]
#[cfg(feature = "macros")]
#[cfg(feature = "macro-diagnostics")]
pub extern crate uuid_macro;

use crate::std::convert;
Expand Down
41 changes: 36 additions & 5 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
macro_rules! define_uuid_macro {
{$(#[$doc:meta])*} => {
$(#[$doc])*
#[cfg(feature = "macro-diagnostics")]
#[macro_export]
macro_rules! uuid {
($uuid:literal) => {{
$crate::Uuid::from_bytes($crate::uuid_macro::parse_lit!($uuid))
}};
}

$(#[$doc])*
#[cfg(not(feature = "macro-diagnostics"))]
#[macro_export]
macro_rules! uuid {
($uuid:literal) => {{
const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) {
Ok(u) => u,
Err(_) => {
// here triggers const_err
// const_panic requires 1.57
#[allow(unconditional_panic)]
let _ = ["invalid uuid representation"][1];

loop {} // -> never type
}
};
OUTPUT
}};
}
}
}

define_uuid_macro! {
/// Parse [`Uuid`][uuid::Uuid]s from string literals at compile time.
///
/// ## Usage
Expand Down Expand Up @@ -33,6 +67,8 @@
/// let uuid: Uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4");
/// ```
///
/// Enable the feature `macro-diagnostics` to see the error messages below.
///
/// Provides the following compilation error:
///
/// ```txt
Expand Down Expand Up @@ -60,9 +96,4 @@
/// ```
///
/// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
#[macro_export]
macro_rules! uuid {
($uuid:tt) => {{
$crate::Uuid::from_bytes($crate::uuid_macro::parse_lit!($uuid))
}};
}
2 changes: 1 addition & 1 deletion tests/macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(feature = "macros")]
#[cfg(feature = "macro-diagnostics")]
#[test]
fn ui() {
let t = trybuild::TestCases::new();
Expand Down