blizzard_id/lib.rs
1//! # UID crate
2//! This crate is utility crate to create unique identifications.
3//!
4//! Crate originally developed for the Blizzard Game and Server engines.
5
6#[cfg(test)]
7mod tests {
8 use crate::Uid;
9 #[test]
10 fn test() {
11 let id = Uid::new_numerical(2);
12 println!("{}", id);
13 assert!(id < 100);
14 assert!(id >= 0);
15 }
16}
17
18use rand::{thread_rng, Rng};
19
20/// UID struct that has different UID implementations.
21///
22/// # Example
23/// ```
24/// let id = uid::Uid::new_numerical(1);
25/// assert!(id < 10);
26/// assert!(id >= 0);
27/// ```
28pub struct Uid {}
29
30impl Uid {
31 /// Make a numerical uid with size = length
32 ///
33 /// # Example
34 /// ```
35 /// let id = uid::Uid::new_numerical(2);
36 /// assert!(id < 100);
37 /// assert!(id >= 0);
38 /// ```
39 pub fn new_numerical(length: u32) -> u32 {
40 let mut id = 0;
41 for i in 0..length {
42 let mut rng = thread_rng();
43
44 // Exclusive range
45 let int: u32 = rng.gen_range(0..10);
46 let base = u32::pow(10, i);
47 id += base * int;
48 }
49 id
50 }
51}