#id #uuid #guid #shortid

tinyid

A tiny ID type that's like UUID except shorter and easier for a user to type in

4 stable releases

1.0.3 Jan 18, 2023
1.0.1 May 4, 2022

#1549 in Parser implementations

Download history 78/week @ 2024-03-13 119/week @ 2024-03-20 35/week @ 2024-03-27 67/week @ 2024-04-03 134/week @ 2024-04-10 62/week @ 2024-04-17 142/week @ 2024-04-24 98/week @ 2024-05-01 119/week @ 2024-05-08 106/week @ 2024-05-15 56/week @ 2024-05-22 170/week @ 2024-05-29 194/week @ 2024-06-05 130/week @ 2024-06-12 164/week @ 2024-06-19 75/week @ 2024-06-26

595 downloads per month

MPL-2.0 license

25KB
476 lines

Rust coveralls.io codecov

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 / correct. They can be randomly generated but no work has been done to make sure that these random generations are secure (all RNG is done through the excellent fastrand crate).

I made this type because I needed mostly / somewhat random identifiers that could be easily read and retyped by a user, but would also prevent collisions in somewhat small (less than a million or so) use-cases.

Examples collision.rs or collision_average.rs (beware, this can take quite a while to run) can be run to get an idea of how many IDs can be generated before collision occurs, but this is ultimately down to luck I suppose.
Generally, an average of 50-100 times gave me results in the 20 million range (IDs created before collision), but unlucky RNG has lead to results as low as 6-8 million.

Dependencies

The crate has either one or two dependencies, depending on whether serialization is needed. fastrand is used for RNG, serde is used for de/serialization only if the serde feature flag is enabled.

Example

Further examples can be found in ./examples/basic.rs.

use tinyid::TinyId;

// Generate a random ID.
let mut id = TinyId::random();
// Ensure that the ID is valid.
assert!(id.is_valid());
assert!(!id.is_null());

id.make_null();
assert!(!id.is_valid());
assert!(id.is_null());
assert_eq!(id, TinyId::null());

Features

The crate only has one feature, serde, which will enable serde serialization and deserialization of the TinyId type. It will also bring in the serde dependency.

Dependencies

~205KB