mirror of
https://github.com/uuid-rs/uuid.git
synced 2025-10-02 15:24:57 +00:00
provide uuid macro without dependencies
This commit is contained in:
parent
27fc858fde
commit
daefb3e4c9
@ -241,7 +241,6 @@ mod rng;
|
|||||||
|
|
||||||
mod external;
|
mod external;
|
||||||
|
|
||||||
#[cfg(feature = "macros")]
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros;
|
mod macros;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
@ -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.
|
/// Parse [`Uuid`][uuid::Uuid]s from string literals at compile time.
|
||||||
///
|
///
|
||||||
/// ## Usage
|
/// ## Usage
|
||||||
@ -33,6 +67,8 @@
|
|||||||
/// let uuid: Uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4");
|
/// let uuid: Uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4");
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
/// Enable the feature `macros` to see the error messages below.
|
||||||
|
///
|
||||||
/// Provides the following compilation error:
|
/// Provides the following compilation error:
|
||||||
///
|
///
|
||||||
/// ```txt
|
/// ```txt
|
||||||
@ -60,9 +96,4 @@
|
|||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
|
/// [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))
|
|
||||||
}};
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user