Expand description
Generate random uid strings containing letters, numbers, or base62 values.
This module provides a way to generate various types of
random strings that can be used as a UID. It also allows
generation of UID strings that are base62 representation
of numbers to allow for converstion to and from u16, u32,
and u64.
The UidStore struct provides helper functions that helps
avoid generation of duplicate uid values, which becomes
very likely when using short UID’s.
Standalone functions to generate random strings:
let uid = random_string(8);
let uid = random_number(10);
let uid = human_random_string(8);Convert a number to and from a base62 uid:
let uid = number_to_uid(1000);
let number = uid_to_number(&uid).unwrap();Generate a sequence of UID’s that should be unique:
let mut u = UidStore::new();
let uid = u.next(6);
let uid = u.next_u16();
let uid = u.next_u32();
let uid = u.next_u64();Initialise a UidStore with a sequence of previously
generated uid values.
let mut u = UidStore::new();
u.make_unique("ifh983u");
u.make_unique("Rig3hGa");
u.make_unique("h84gh8A");
u.make_unique("h84gh8A"); // Duplicate uid triggers new uid generationNotice above that make_unique was passed the same UID twice, when
this happened, a new UID was generated and returned. This should be
handled:
if let Some(deduplicate) = u.make_unique("h84gh8A") {
println!("Duplicate UID for item replaced with: {}", deduplicate);
}Structs§
- UidStore
- UidStore holds a collection of previously generated UID values to ensure a value is only ever generated once.
Functions§
- human_
random_ string - Generate a random string that doedn’t include easily confused characters such as i,I,1 and o,O,0.
- number_
to_ uid - Convert the contents of a base62 string back to
the number that was used to generate the string.
Reverse using
uid_to_number(). - random_
max_ size - Generate a base62 string using a random number no larger than a specified maximum size.
- random_
number - Generate a string of numbers with the specified
length. - random_
string - Generate a random base62 string with a fixed string
length. - uid_
to_ number - Convert a base62 string into the underlying number it
represents. Returns None if the string is not a valid
base62 number. Reverse using
number_to_uid().