Lightweight compile-time UUID parser.

Overview

compiled-uuid

github-img crates-img docs-img

Anywhere you're building Uuids from a string literal, you should use uuid.

Motivation

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");

Usage

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");
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

License

compiled-uuid is open-source software, distributed under the MIT license.

You might also like...
A lightweight distributed message queue. Like AWS SQS and RSMQ but on Postgres.

Postgres Message Queue (PGMQ) A lightweight distributed message queue. Like AWS SQS and RSMQ but on Postgres. Features Lightweight - Built with Rust a

Check Have I Been Pwned and see if it's time for you to change passwords.

checkpwn Check Have I Been Pwned and see if it's time for you to change passwords. Getting started Install: cargo install checkpwn Update: cargo inst

Estimate the amount of time spent working on a Git repository

jikyuu (時給) A tool to estimate the amount of time spent working on a Git repository. It is a direct port of git-hours, written in Node.js, because the

Measure the execution time of an application

Execution Timer Drag an executable file on the binary or enter the path as an argument to measure the execution time of the program. Building cargo bu

Simple and efficient time representation in Rust.

timens-rs Simple and efficient timestamp representation. The main objective being interoperability with OCaml Core_kernel.Time_ns. A significant part

A real-time event-oriented data-hub

Redcar A real-time event-oriented data-hub, inspired by the data hub. It is: Universal: the front end uses gRPC to provide services. Fast: benchmarked

This article is about the unsound api which I found in owning_ref. Owning_ref is a library that has 11 million all-time downloads and 60 reverse dependencies.

Unsoundness in owning_ref This article is about the unsound api which I found in owning_ref. Owning_ref is a library that has 11 million all-time down

A timer based on a multi-time wheel structure

wheel-timer2 A timer based on a multi-time wheel structure This library uses a multi-layered time wheel structure. When a task is added to the wheel,

Time related types (and conversions) for scientific and astronomical usage.

astrotime Time related types (and conversions) for scientific and astronomical usage. This library is lightweight and high performance. Features The f

Comments
  • Instead of macro it can be done via const fn

    Instead of macro it can be done via const fn

    JFYI macros are not necessary for this purpose. String parsing is possible within const fn and since panic is stable recently, you can implement error handling via panic rather than relying on slow proc macro

    Example of parsing here: https://github.com/DoumanAsh/uuid/blob/master/src/lib.rs#L419

    opened by DoumanAsh 2
Owner
Quinn
CS + Stats @ Swarthmore College GSoC '21 @ Kanidm
Quinn
A library to compile USDT probes into a Rust library

sonde sonde is a library to compile USDT probes into a Rust library, and to generate a friendly Rust idiomatic API around it. Userland Statically Defi

Ivan Enderlin 40 Jan 7, 2023
A rollup plugin that compile Rust code into WebAssembly modules

rollup-plugin-rust tl;dr -- see examples This is a rollup plugin that loads Rust code so it can be interop with Javascript base project. Currently, th

Fahmi Akbar Wildana 37 Aug 1, 2022
Showcase for pathological compile times when using knuffel / chumsky / VERY LARGE types

netherquote Showcase for pathological compile times when using knuffel / chumsky / VERY LARGE types. How to reproduce The rust toolchain version is pi

Amos Wenger 7 Jan 1, 2023
Rustymind is a driver and parser for NeuroSky MindWave EEG headset written in pure Rust.

Rustymind is a driver and parser for NeuroSky MindWave EEG headset written in pure Rust. You can use it to connect, interact, and plot real time data from the headset.

Junjun Dong 34 Sep 13, 2022
Rust+Cargo lightweight hello world with the most minimum binary size possible.

Lightweight Cargo Hello World Rust+Cargo lightweight hello world with the most minimum binary size possible. requirements 1: Rustup (Rustc, Cargo) Ins

Raymond 1 Dec 13, 2021
bustd is a lightweight process killer daemon for out-of-memory scenarios for Linux!

bustd: Available memory or bust! bustd is a lightweight process killer daemon for out-of-memory scenarios for Linux! Features Small memory usage! bust

Pop!_OS 8 Oct 6, 2022
Parser for UltraStar Deluxe song files

This is a rust parser for USDX song files. Files are written as a plaintext files that contain data about the song and notes/lyrics.

null 1 Apr 3, 2022
Motoko concrete syntax parser in Rust.

motoko.rs Motoko concrete syntax parser and dynamic evaluator (VM) in Rust. Motoko VM The Motoko VM explores a more dynamic way for Motoko to execute.

DFINITY 8 Dec 15, 2022
A lightweight new Bing (AI chat) desktop application which based on Tauri.

Bing Lite A lightweight new Bing (AI chat) desktop application which based on Tauri. No more Microsoft Edge, no more Chromium/Electron! Download The l

a.e. 6 Apr 5, 2023
λ-calculus parser made by rust

Lambda Calculus Parser This is a parser for λ-calculus expressions. It takes a λ-terms as input, parses it and returns a JSON representation of the te

Lee ByeongJun 4 Apr 17, 2023