-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuuid.rc
142 lines (116 loc) · 3.7 KB
/
uuid.rc
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
#[link(name = "uuid",
vers = "0.3.0",
uuid = "3ef20bf5-f609-4c96-bfb5-8e617274e705")];
#[crate_type = "lib"];
extern mod std;
#[cfg(target_os = "macos")]
#[nolink]
extern mod uuid {
fn uuid_generate(out: &mut UUID);
fn uuid_generate_random(out: &mut UUID);
fn uuid_generate_time(out: &mut 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: &mut UUID);
fn uuid_generate_random(out: &mut UUID);
fn uuid_generate_time(out: &mut 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([u8, .. 36]);
impl UUID {
/// Create a new uuid
fn new() -> UUID {
let mut uuid = UUID([0, ..36]);
unsafe { uuid::uuid_generate(&mut uuid) };
uuid
}
/// Create a uuid from the current time and mac address
fn new_random() -> UUID {
let mut uuid = UUID([0, ..36]);
unsafe { uuid::uuid_generate_random(&mut uuid) };
uuid
}
/// Create a uuid from a random number generator
fn new_time() -> UUID {
let mut uuid = UUID([0, ..36]);
unsafe { uuid::uuid_generate_time(&mut uuid) };
uuid
}
}
impl cmp::Eq for UUID {
fn eq(&self, other: &UUID) -> bool { **self == **other }
fn ne(&self, other: &UUID) -> bool { !self.eq(other) }
}
impl cmp::Ord for UUID {
fn lt(&self, other: &UUID) -> bool { **self < **other }
fn le(&self, other: &UUID) -> bool { !(*other).lt(self) }
fn ge(&self, other: &UUID) -> bool { !self.lt(other) }
fn gt(&self, other: &UUID) -> bool { (*other).lt(self) }
}
/// Convert a uuid to a string
impl ToStr for UUID {
fn to_str(&self) -> ~str {
let mut s = str::with_capacity(36);
do str::as_buf(s) |buf, _| {
unsafe { uuid::uuid_unparse(self, buf) };
}
unsafe { str::raw::set_len(&mut s, 36u); }
s
}
}
/// Convert a string to a uuid
impl core::from_str::FromStr for UUID {
fn from_str(s: &str) -> Option<UUID> {
assert_eq!(s.len(), 36u);
let mut uuid = UUID([0, ..36]);
let r = do str::as_buf(s) |buf, _| {
unsafe { uuid::uuid_parse(buf, &uuid) }
};
if r == 0 { Some(uuid) } else { None }
}
}
#[cfg(test)]
mod test {
use super::*;
use core::from_str::FromStr;
#[test]
fn test_invalid() {
let s = " ";
assert_eq!(FromStr::from_str::<UUID>(s), None);
}
#[test]
fn test_valid() {
for uint::range(0u, 100000) |_| {
let uuid = UUID::new();
assert!(*uuid != [0, ..36]);
assert!(FromStr::from_str(uuid.to_str()) == Some(uuid));
let uuid = UUID::new_random();
assert!(*uuid != [0, ..36]);
assert!(FromStr::from_str(uuid.to_str()) == Some(uuid));
let uuid = UUID::new_time();
assert!(*uuid != [0, ..36]);
assert!(FromStr::from_str(uuid.to_str()) == Some(uuid));
}
}
#[test]
fn test_ordering() {
let s1 = "00000000-0000-0000-0000-000000000000";
let s2 = "00000000-0000-0000-0000-000000000001";
let u1 = FromStr::from_str::<UUID>(s1).unwrap();
let u2 = FromStr::from_str::<UUID>(s2).unwrap();
assert!(u1 < u2);
assert!(u2 > u1);
assert!(u1 <= u1);
assert!(u2 >= u2);
}
}