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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-License-Identifier: MIT
// Copyright (c) ids_service contributors
//! Library that allows generating unique ids. It keeps a cache filled using background threads.
//!
//! If the cache is empty (the number of requests is too high) an id is calculated on the fly.
//! This will slow down obtaining the id but there is no error when the cache is empty.
//!
//! To generate unique ids the library uses an array of bytes from a random number generator
//! combined with a nanosecond timestamp since UNIX_EPOCH, then hashes both together.
//!
//! The library is composed of two modules with similar API but different hash algorithms:
//!
//! ## `crypto_hash`
//! Uses one of the SHA-3 algorithms from the [`sha3`](https://crates.io/crates/sha3) crate.
//!
//! ## `rust_hash`
//! Uses [`std::collections::hash_map::DefaultHasher`] from the Rust standard library.
//!
//! According to your requirements, you may choose which module you need. The negative impact of
//! using `crypto_hash` is performance and resource usage — throughput is roughly half compared to
//! `rust_hash`.
//!
//! Indicative throughput on an Intel i7:
//! * `crypto_hash` (SHA-512): above 2'000'000 ids/sec
//! * `rust_hash`: above 5'000'000 ids/sec
//!
//! The id can be encoded as:
//! * Hexadecimal lower case string
//! * Base64 string
//! * Base64-URL string
//! * Base32 string
//! * JSON string
//!
//! # Quick Start
//!
//! ## `crypto_hash`
//! ```
//! #[cfg(feature = "crypto")]
//! {
//! use ids_service::crypto_hash::*;
//! use ids_service::common::*;
//!
//! let mut ids = IdsService::default();
//! ids.start();
//! let _ = ids.filled_at_percent_event(10).recv();
//! println!("Get an id: {}", ids.get_id().as_hex());
//! println!("Get another id: {}", ids.get_id().as_base64());
//! println!("Current cache size: {}", ids.get_cache_len());
//! ids.stop();
//! }
//! ```
//!
//! Create an id without the caching service:
//! ```
//! #[cfg(feature = "crypto")]
//! {
//! use ids_service::crypto_hash::*;
//!
//! println!("SHA-512 id: {}", create_id_as_sha512());
//! println!("SHA-256 id: {}", create_id_as_sha256());
//! }
//! ```
//!
//! ## `rust_hash`
//! ```
//! #[cfg(feature = "rust")]
//! {
//! use ids_service::rust_hash::*;
//! use ids_service::common::*;
//!
//! let mut ids = IdsService::default();
//! ids.start();
//! let _ = ids.filled_event().recv();
//! println!("Get an id: {}", ids.get_id().as_hex());
//! println!("Get an id as base64: {}", ids.get_id().as_base64());
//! println!("Get an id as json: {}", ids.get_id().as_json());
//! ids.stop();
//! }
//! ```
//!
//! Create an id without the caching service:
//! ```
//! #[cfg(feature = "rust")]
//! {
//! use ids_service::rust_hash::*;
//!
//! println!("id: {}", create_id());
//! }
//! ```
/// Crypto hash module - available with `crypto` or `all` features (default).
/// Rust hash module — **deprecated**, use the `crypto` feature and `crypto_hash` module instead.
/// Module with shared types used by both `crypto_hash` and `rust_hash`.