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
pub use imp::*;
pub use uuid::Uuid;
#[cfg(not(test))]
mod imp {
use uuid::Uuid;
/// Generates a new instance of unique identifier.
pub fn new() -> Uuid {
Uuid::new_v4()
}
}
#[cfg(test)]
mod imp {
use std::cell::RefCell;
use uuid::Uuid;
thread_local!(static ID: RefCell<Option<Uuid>> = RefCell::new(None));
/// Generates a new instance of unique identifier or predefined value to test against it.
pub fn new() -> Uuid {
ID.with(|is| {
if let Some(id) = *is.borrow() {
id
} else {
Uuid::new_v4()
}
})
}
/// Sets known Uuid value as now to assert test against it.
pub fn set(uuid: Uuid) {
ID.with(|is| *is.borrow_mut() = Some(uuid))
}
/// Resets pre-defined Uuid value to use Uuid::new_v4() instead.
pub fn reset() {
ID.with(|is| *is.borrow_mut() = None)
}
}