-
Notifications
You must be signed in to change notification settings - Fork 1
/
uuid.rs
132 lines (111 loc) · 3.19 KB
/
uuid.rs
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
#[cfg(target_os = "macos")]
#[nolink]
extern mod uuid {
fn uuid_generate(out: UUID);
fn uuid_generate_random(out: UUID);
fn uuid_generate_time(out: UUID);
fn uuid_parse(s: *u8, uuid: UUID) -> libc::c_int;
fn uuid_unparse(uuid: UUID, out: *u8);
fn uuid_unparse_lower(uuid: UUID, out: *u8);
fn uuid_unparse_upper(uuid: UUID, out: *u8);
}
#[cfg(target_os = "linux")]
#[cfg(target_os = "freebsd")]
extern mod uuid {
fn uuid_generate(out: UUID);
fn uuid_generate_random(out: UUID);
fn uuid_generate_time(out: UUID);
fn uuid_parse(s: *u8, uuid: UUID) -> libc::c_int;
fn uuid_unparse(uuid: UUID, out: *u8);
fn uuid_unparse_lower(uuid: UUID, out: *u8);
fn uuid_unparse_upper(uuid: UUID, out: *u8);
}
/// a uuid value
pub struct UUID {
a: u32,
b: u32,
c: u32,
d: u32,
}
impl UUID: cmp::Eq {
pure fn eq(other: &UUID) -> bool {
self.a == other.a &&
self.b == other.b &&
self.c == other.c &&
self.d == other.d
}
pure fn ne(other: &UUID) -> bool {
!self.eq(other)
}
}
impl UUID: cmp::Ord {
pure fn lt(other: &UUID) -> bool {
if self.a < other.a { return true; }
if other.a < self.a { return false; }
if self.b < other.b { return true; }
if other.b < self.b { return false; }
if self.c < other.c { return true; }
if other.c < self.c { return false; }
self.d < other.d
}
pure fn le(other: &UUID) -> bool { !(*other).lt(&self) }
pure fn ge(other: &UUID) -> bool { !self.lt(other) }
pure fn gt(other: &UUID) -> bool { (*other).lt(&self) }
}
/// Create a new uuid
pub fn UUID() -> UUID {
let uuid = UUID { a: 0u32, b: 0u32, c: 0u32, d: 0u32 };
uuid::uuid_generate(uuid);
uuid
}
/// Create a uuid from the current time and mac address
pub fn UUID_random() -> UUID {
let uuid = UUID { a: 0u32, b: 0u32, c: 0u32, d: 0u32 };
uuid::uuid_generate_random(uuid);
uuid
}
/// Create a uuid from a random number generator
pub fn UUID_time() -> UUID {
let uuid = UUID { a: 0u32, b: 0u32, c: 0u32, d: 0u32 };
uuid::uuid_generate_time(uuid);
uuid
}
/// Convert a uuid to a string
pub impl UUID: to_str::ToStr {
fn to_str() -> ~str {
let mut s = ~"";
str::reserve(&mut s, 36u);
unsafe { str::raw::set_len(&mut s, 36u); }
do str::as_buf(s) |buf, _len| {
uuid::uuid_unparse(self, buf);
}
s
}
}
pub fn from_str(s: &str) -> Option<UUID> {
assert s.len() == 36u;
let uuid = UUID { a: 0u32, b: 0u32, c: 0u32, d: 0u32 };
do str::as_buf(s) |buf, _len| {
uuid::uuid_parse(buf, uuid);
}
Some(uuid)
}
/// Convert a string to a uuid
impl UUID: from_str::FromStr {
static fn from_str(s: &str) -> Option<UUID> { from_str(s) }
}
#[cfg(test)]
mod test {
pub use from_str::FromStr;
#[test]
fn test() {
for uint::range(0u, 100000u) |_i| {
let uuid = UUID();
assert from_str(uuid.to_str()) == Some(uuid);
let uuid = UUID_random();
assert from_str(uuid.to_str()) == Some(uuid);
let uuid = UUID_time();
assert from_str(uuid.to_str()) == Some(uuid)
}
}
}