@@ -4,6 +4,13 @@ macro_rules! define_uuid_macro {
4
4
#[ cfg( feature = "macro-diagnostics" ) ]
5
5
#[ macro_export]
6
6
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
+ } } ;
7
14
( $uuid: literal) => { {
8
15
$crate:: Uuid :: from_bytes( $crate:: uuid_macro_internal:: parse_lit!( $uuid) )
9
16
} } ;
@@ -13,7 +20,7 @@ macro_rules! define_uuid_macro {
13
20
#[ cfg( not( feature = "macro-diagnostics" ) ) ]
14
21
#[ macro_export]
15
22
macro_rules! uuid {
16
- ( $uuid: literal ) => { {
23
+ ( $uuid: expr ) => { {
17
24
const OUTPUT : $crate:: Uuid = match $crate:: Uuid :: try_parse( $uuid) {
18
25
$crate:: __macro_support:: Ok ( u) => u,
19
26
$crate:: __macro_support:: Err ( _) => panic!( "invalid UUID" ) ,
@@ -50,6 +57,12 @@ define_uuid_macro! {
50
57
/// # use uuid::uuid;
51
58
/// let uuid = uuid!("urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4");
52
59
/// ```
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
+ /// ```
53
66
///
54
67
/// ## Compilation Failures
55
68
///
@@ -71,22 +84,5 @@ define_uuid_macro! {
71
84
/// | ^
72
85
/// ```
73
86
///
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
- ///
91
87
/// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
92
88
}
0 commit comments