Skip to content

Commit

Permalink
provide uuid macro without dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Nov 16, 2021
1 parent 27fc858 commit daefb3e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ mod rng;

mod external;

#[cfg(feature = "macros")]
#[macro_use]
mod macros;
#[doc(hidden)]
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 = "macros")]
#[macro_export]
macro_rules! uuid {
($uuid:literal) => {{
$crate::Uuid::from_bytes($crate::uuid_macro::parse_lit!($uuid))
}};
}

$(#[$doc])*
#[cfg(not(feature = "macros"))]
#[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 `macros` 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))
}};
}

0 comments on commit daefb3e

Please sign in to comment.