| Crates.io | compiled-uuid |
| lib.rs | compiled-uuid |
| version | 0.1.2 |
| created_at | 2021-10-10 03:12:26.541829+00 |
| updated_at | 2021-10-10 18:29:41.485232+00 |
| description | Parse UUIDs at compile time |
| homepage | |
| repository | https://github.com/QnnOkabayashi/compiled-uuid |
| max_upload_size | |
| id | 463003 |
| size | 10,912 |
Anywhere you're building Uuids from a string literal, you should use uuid.
If you want to use a fixed Uuid throughout your program and avoid parsing it multiple times, often you might use lazy_static to cache the Uuid after parsing the first time:
lazy_static! {
pub static ref MY_UUID: Uuid = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
}
However, this method introduces overhead through parsing and unwrapping at runtime.
uuid, on the other hand, provides a zero-cost runtime solution:
const MY_UUID: Uuid = uuid!("550e8400-e29b-41d4-a716-446655440000");
compiled_uuid exposes one macro called uuid, which parses Uuids at compile time. On success, it resolves to Uuid::from_bytes, which cannot fail and has zero runtime cost.
When you write this:
let id: Uuid = uuid!("F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4");
It expands to this:
let id: Uuid = ::uuid::Uuid::from_bytes([
249u8, 22u8, 140u8, 94u8, 206u8, 178u8, 79u8, 170u8, 182u8, 191u8, 50u8, 155u8, 243u8,
159u8, 161u8, 228u8,
]);
If the UUID cannot be parsed successfully:
let id: Uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4");
Then a compilation error is raised:
error: invalid character: expected an optional prefix of `urn:uuid:` followed by 0123456789abcdefABCDEF-, found Z at 9
|
| let id: Uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
compiled-uuid is open-source software, distributed under the MIT license.