A type-safe, K-sortable, globally unique identifier

Overview

type-safe-id

A type-safe, K-sortable, globally unique identifier.

Typed implementation of https://github.com/jetpack-io/typeid in Rust.

Examples

StaticType prefixes

This is the intended happy path. Using a StaticType implementation, you ensure that the ID being parsed is of the intended type.

use type_safe_id::{StaticType, TypeSafeId};

#[derive(Default)]
struct User;

impl StaticType for User {
    // must be lowercase ascii [a-z] only
    const TYPE: &'static str = "user";
}

// type alias for your custom typed id
type UserId = TypeSafeId<User>;

let user_id1 = UserId::new();
# std::thread::sleep(std::time::Duration::from_millis(10));
let user_id2 = UserId::new();

let uid1 = user_id1.to_string();
let uid2 = user_id2.to_string();
dbg!(&uid1, &uid2);
assert!(uid2 > uid1, "type safe IDs are ordered");

let user_id3: UserId = uid1.parse().expect("invalid user id");
let user_id4: UserId = uid2.parse().expect("invalid user id");

assert_eq!(user_id1.uuid(), user_id3.uuid(), "round trip works");
assert_eq!(user_id2.uuid(), user_id4.uuid(), "round trip works");

DynamicType prefixes

If you can't know what the prefix will be, you can use the DynamicType prefix.

use type_safe_id::{DynamicType, TypeSafeId};

let id: TypeSafeId<DynamicType> = "prefix_01h2xcejqtf2nbrexx3vqjhp41".parse().unwrap();

assert_eq!(id.type_prefix(), "prefix");
assert_eq!(id.uuid(), uuid::uuid!("0188bac7-4afa-78aa-bc3b-bd1eef28d881"));

Help

error[E0080]: evaluation of `<Index as type_safe_id::StaticType>::__TYPE_PREFIX_IS_VALID` failed
  --> /Users/conrad/Documents/code/type-safe-id/src/lib.rs:76:13
   |
76 |             assert!(Self::TYPE.as_bytes()[i].is_ascii_lowercase());
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: Self::TYPE.as_bytes()[i].is_ascii_lowercase()', /Users/conrad/Documents/code/type-safe-id/src/lib.rs:76:13

This compiler error suggests that your static type prefix is not valid because it contains non ascii-lowercase values.

error[E0080]: evaluation of `<Index as type_safe_id::StaticType>::__TYPE_PREFIX_IS_VALID` failed
  --> /Users/conrad/Documents/code/type-safe-id/src/lib.rs:73:9
   |
73 |         assert!(Self::TYPE.len() < 64);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: Self::TYPE.len() < 64', /Users/conrad/Documents/code/type-safe-id/src/lib.rs:73:9
   |

This compiler error suggests that your static type prefix is not valid because it contains more than 63 characters.

You might also like...
Usable, easy and safe pure-Rust crypto

orion About Orion is a cryptography library written in pure Rust. It aims to provide easy and usable crypto while trying to minimize the use of unsafe

A safe implementation of the secure remote password authentication and key-exchange protocol (SRP), SRP6a and legacy are as features available.

Secure Remote Password (SRP 6 / 6a) A safe implementation of the secure remote password authentication and key-exchange protocol (SRP version 6a). Ver

Tool for computing vanity Gnosis Safe addresses

0xdeadbeef Tool used for computing vanity Gnosis Safe addresses. This tool is currently hard-coded to only support the v1.3.0 Safe deployment: GnosisS

Usable, easy and safe pure-Rust crypto

orion About Orion is a cryptography library written in pure Rust. It aims to provide easy and usable crypto while trying to minimize the use of unsafe

Gnosis Safe Tx Service API client & associated tooling

Safe Transaction Service API Client Using the SDK Instantiate an API client use safe_sdk::SafeClient; /// From a chain id, by looking up hardcoded en

A blazingly fast and memory safe password cracker with user interface.
A blazingly fast and memory safe password cracker with user interface.

HashVat A blazingly fast and memory safe password cracker with user interface. HashVat runs with user interface and is capable of cracking the 1.000.0

Hash trait that is object-safe

Hash trait that is object-safe This crate provides a DynHash trait that can be used in trait objects. Types that implement the standard library's std:

Prefix tree (ordered map and set) data structure using 100% safe Rust

PFX: A 100% safe, blob-oriented prefix tree This crate provides a prefix tree map and set data structure, implemented purely in safe Rust. The API is

A small, 8-byte, ID type for use in rust applications that need a pretty unique identifier

TinyId A small, 8-byte, ID type for use in rust applications that need a pretty unique identifier that is not required to be cryptographically secure

Generate unique, yet sortable identifiers

ulid-lite About An implementation of the ULID ("Universally Unique Lexicographically Sortable Identifier") standard. A ULID is 128-bit compatible with

A simple to use rust package to generate or parse Twitter snowflake IDs,generate time sortable 64 bits unique ids for distributed systems

A simple to use rust package to generate or parse Twitter snowflake IDs,generate time sortable 64 bits unique ids for distributed systems (inspired from twitter snowflake)

Rodrigo's Unique Identifier

RUID - Time-Travel-Safe Unique 64 bit ids generated in Rust RUIDs (Rodrigo's Unique Identifiers) are 64 bit ids mathematically guaranteed to be unique

Generates a unique hash/identifier for a system given a set of parameters.

uniqueid 🔍 Generates a unique hash/identifier for a system given a set of parameters. Example usage use uniqueid; pub fn main() { let data = vec

A dead simple logger that works globally and across threads.

Woody A logger for Rust that's *actually* easy to use. Features Easy to use: Just import the macros and you're good to go. No need to configure anythi

Mononym is a library for creating unique type-level names for each value in Rust.

Mononym is a library for creating unique type-level names for each value in Rust.

Build database expression type checker and vectorized runtime executor in type-safe Rust

Typed Type Exercise in Rust Build database expression type checker and vectorized runtime executor in type-safe Rust. This project is highly inspired

Rustato: A powerful, thread-safe global state management library for Rust applications, offering type-safe, reactive state handling with an easy-to-use macro-based API.
Rustato: A powerful, thread-safe global state management library for Rust applications, offering type-safe, reactive state handling with an easy-to-use macro-based API.

Rustato State Manager A generical thread-safe global state manager for Rust Introduction • Features • Installation • Usage • Advanced Usage • Api Refe

Figures out the local timezone as IANA / Olson identifier

localzone Utility crate to figure out the IANA (Olson) timezone of the current machine. The IANA timezones have been largely established as the standa

Parallel finance a decentralized lending protocol built on top of the Polkadot ecosystem. Our unique approach will allow users to earn
Parallel finance a decentralized lending protocol built on top of the Polkadot ecosystem. Our unique approach will allow users to earn "double interests" from staking and lending their tokens simultaneously.

Parallel Finance A new Cumulus-based Substrate node, ready for hacking 🚀 Getting Started Follow these steps to get started with the Cumulus Template

Owner
Conrad Ludgate
Conrad Ludgate
Generates a unique hash/identifier for a system given a set of parameters.

uniqueid ?? Generates a unique hash/identifier for a system given a set of parameters. Example usage use uniqueid; pub fn main() { let data = vec

Checksum 2 Aug 19, 2022
Figures out the local timezone as IANA / Olson identifier

localzone Utility crate to figure out the IANA (Olson) timezone of the current machine. The IANA timezones have been largely established as the standa

Armin Ronacher 11 Jan 4, 2022
A fast, simple and powerful open-source cross platform utility tool for generating strong, unique and random passwords

password-generator-pro A fast, simple and powerful open-source cross platform utility tool for generating strong, unique and random passwords. Feature

Sebastien Rousseau 3 Dec 16, 2022
A blazing fast, type-safe template engine for Rust.

markup.rs A blazing fast, type-safe template engine for Rust. markup.rs is a template engine for Rust powered by procedural macros which parses the te

Utkarsh Kukreti 209 Dec 24, 2022
A type-safe Rust interface to the Nix CLI

runix A typesafe interface to the nix CLI. by flox Installation Install with cargo add (Rust >= 1.64) cargo add runix Alternatively, manually add runi

flox 43 Mar 6, 2023
Onlyfans-type web service based on TOR with maximum privacy features.

onionfans Onlyfans-type web service based on TOR with maximum privacy features. Features "Vanishing" single-use feed CDN links Landing page No JavaScr

Dowland Aiello 8 Sep 14, 2022
Rust compile-time type information experiment

Compile-Time Type Information This crate is an experimental standard library side implementation of potential ctti language feature. The idea is to pr

Auri 12 Jan 20, 2023
A "Type 0" zkEVM. Prove validity of Ethereum blocks using RISC Zero's zkVM

zeth NEW: Zeth now supports Optimism blocks! Just pass in --network=optimism! Zeth is an open-source ZK block prover for Ethereum built on the RISC Ze

RISC Zero 222 Oct 26, 2023
A performant, type-1 zkEVM written in Rust & SP1.

SP1 Reth SP1 Reth is a 100% open-source POC that showcases how any rollup can use SP1 to build a performant (type-1, bytecode compatible) zkEVM with l

Succinct 90 Mar 24, 2024
Safe, fast, small crypto using Rust

THE SOFTWARE IS PROVIDED "AS IS" AND BRIAN SMITH AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES

Brian Smith 3k Jan 2, 2023