mirror of
https://github.com/uuid-rs/uuid.git
synced 2025-09-28 21:42:19 +00:00
Merge pull request #566 from Nugine/lightweight-macro
Provide uuid macro without dependencies
This commit is contained in:
commit
89530dbb2e
@ -54,7 +54,7 @@ repository = "uuid-rs/uuid"
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
macros = ["uuid_macro"]
|
||||
macro-diagnostics = ["uuid_macro"]
|
||||
|
||||
v1 = ["atomic"]
|
||||
v3 = ["md-5"]
|
||||
|
@ -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]
|
||||
@ -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`.
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
@ -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;
|
||||
|
@ -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
|
||||
@ -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
|
||||
@ -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))
|
||||
}};
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
#[cfg(feature = "macros")]
|
||||
#[cfg(feature = "macro-diagnostics")]
|
||||
#[test]
|
||||
fn ui() {
|
||||
let t = trybuild::TestCases::new();
|
||||
|
Loading…
x
Reference in New Issue
Block a user