Skip to content

Commit 66b4fce

Browse files
authored
Merge pull request #764 from Vrajs16/main
Use const identifier in uuid macro
2 parents 36e6f57 + 8896e26 commit 66b4fce

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/macros.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ macro_rules! define_uuid_macro {
44
#[cfg(feature = "macro-diagnostics")]
55
#[macro_export]
66
macro_rules! uuid {
7+
($uuid:expr) => {{
8+
const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) {
9+
$crate::__macro_support::Ok(u) => u,
10+
$crate::__macro_support::Err(_) => panic!("invalid UUID"),
11+
};
12+
OUTPUT
13+
}};
714
($uuid:literal) => {{
815
$crate::Uuid::from_bytes($crate::uuid_macro_internal::parse_lit!($uuid))
916
}};
@@ -13,7 +20,7 @@ macro_rules! define_uuid_macro {
1320
#[cfg(not(feature = "macro-diagnostics"))]
1421
#[macro_export]
1522
macro_rules! uuid {
16-
($uuid:literal) => {{
23+
($uuid:expr) => {{
1724
const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) {
1825
$crate::__macro_support::Ok(u) => u,
1926
$crate::__macro_support::Err(_) => panic!("invalid UUID"),
@@ -50,6 +57,12 @@ define_uuid_macro! {
5057
/// # use uuid::uuid;
5158
/// let uuid = uuid!("urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4");
5259
/// ```
60+
/// Using a const variable:
61+
/// ```
62+
/// # use uuid::uuid;
63+
/// const UUID_STR: &str = "12345678-1234-5678-1234-567812345678";
64+
/// let UUID = uuid!(UUID_STR);
65+
/// ```
5366
///
5467
/// ## Compilation Failures
5568
///
@@ -71,22 +84,5 @@ define_uuid_macro! {
7184
/// | ^
7285
/// ```
7386
///
74-
/// Tokens that aren't string literals are also rejected:
75-
///
76-
/// ```compile_fail
77-
/// # use uuid::uuid;
78-
/// let uuid_str: &str = "550e8400e29b41d4a716446655440000";
79-
/// let uuid = uuid!(uuid_str);
80-
/// ```
81-
///
82-
/// Provides the following compilation error:
83-
///
84-
/// ```txt
85-
/// error: expected string literal
86-
/// |
87-
/// | let uuid = uuid!(uuid_str);
88-
/// | ^^^^^^^^
89-
/// ```
90-
///
9187
/// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
9288
}

0 commit comments

Comments
 (0)