1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (c) 2023 Tony Barbitta
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use tinyid::TinyId;

fn main() {
    println!("Generating TinyIds until a collision occurs...");
    let start = std::time::Instant::now();
    let iters = get_collision();
    let elapsed = start.elapsed();
    let pretty_iters = iters
        .to_string()
        .as_bytes()
        .rchunks(3)
        .rev()
        .map(std::str::from_utf8)
        .collect::<Result<Vec<&str>, _>>()
        .unwrap()
        .join(","); // separator
    println!("Collision after {pretty_iters} iterations.");
    println!("Elapsed time: {elapsed:#?}");
}

fn get_collision() -> usize {
    let mut ids = std::collections::HashSet::new();
    loop {
        let id = TinyId::random();
        if !ids.insert(id) {
            break;
        }
    }
    ids.len()
}