@@ -22,69 +22,50 @@ extern mod uuid {
22
22
#[cfg(target_os = "linux")]
23
23
#[cfg(target_os = "freebsd")]
24
24
extern mod uuid {
25
- fn uuid_generate(out: UUID);
26
- fn uuid_generate_random(out: UUID);
27
- fn uuid_generate_time(out: UUID);
25
+ fn uuid_generate(out: &mut UUID);
26
+ fn uuid_generate_random(out: &mut UUID);
27
+ fn uuid_generate_time(out: &mut UUID);
28
28
29
- fn uuid_parse(s: *u8, uuid: UUID) -> libc::c_int;
29
+ fn uuid_parse(s: *u8, uuid: & UUID) -> libc::c_int;
30
30
31
- fn uuid_unparse(uuid: UUID, out: *u8);
32
- fn uuid_unparse_lower(uuid: UUID, out: *u8);
33
- fn uuid_unparse_upper(uuid: UUID, out: *u8);
31
+ fn uuid_unparse(uuid: & UUID, out: *u8);
32
+ fn uuid_unparse_lower(uuid: & UUID, out: *u8);
33
+ fn uuid_unparse_upper(uuid: & UUID, out: *u8);
34
34
}
35
35
36
36
/// a uuid value
37
- pub struct UUID {
38
- a: u32,
39
- b: u32,
40
- c: u32,
41
- d: u32,
42
- }
37
+ pub struct UUID([u8 * 36]);
43
38
44
39
impl UUID {
45
40
/// Create a new uuid
46
41
static fn new() -> UUID {
47
- let mut uuid = UUID { a: 0u32, b: 0u32, c: 0u32, d: 0u32 } ;
42
+ let mut uuid = UUID([0, ..36]) ;
48
43
uuid::uuid_generate(&mut uuid);
49
44
uuid
50
45
}
51
46
52
47
/// Create a uuid from the current time and mac address
53
48
static fn new_random() -> UUID {
54
- let mut uuid = UUID { a: 0u32, b: 0u32, c: 0u32, d: 0u32 } ;
49
+ let mut uuid = UUID([0, ..36]) ;
55
50
uuid::uuid_generate_random(&mut uuid);
56
51
uuid
57
52
}
58
53
59
54
/// Create a uuid from a random number generator
60
55
static fn new_time() -> UUID {
61
- let mut uuid = UUID { a: 0u32, b: 0u32, c: 0u32, d: 0u32 } ;
56
+ let mut uuid = UUID([0, ..36]) ;
62
57
uuid::uuid_generate_time(&mut uuid);
63
58
uuid
64
59
}
65
60
}
66
61
67
62
impl UUID: cmp::Eq {
68
- pure fn eq(&self, other: &UUID) -> bool {
69
- self.a == other.a &&
70
- self.b == other.b &&
71
- self.c == other.c &&
72
- self.d == other.d
73
- }
74
-
63
+ pure fn eq(&self, other: &UUID) -> bool { **self == **other }
75
64
pure fn ne(&self, other: &UUID) -> bool { !self.eq(other) }
76
65
}
77
66
78
67
impl UUID: cmp::Ord {
79
- pure fn lt(&self, other: &UUID) -> bool {
80
- if self.a < other.a { return true; }
81
- if other.a < self.a { return false; }
82
- if self.b < other.b { return true; }
83
- if other.b < self.b { return false; }
84
- if self.c < other.c { return true; }
85
- if other.c < self.c { return false; }
86
- self.d < other.d
87
- }
68
+ pure fn lt(&self, other: &UUID) -> bool { **self < **other }
88
69
pure fn le(&self, other: &UUID) -> bool { !(*other).lt(self) }
89
70
pure fn ge(&self, other: &UUID) -> bool { !self.lt(other) }
90
71
pure fn gt(&self, other: &UUID) -> bool { (*other).lt(self) }
@@ -102,45 +83,59 @@ pub impl UUID: to_str::ToStr {
102
83
}
103
84
}
104
85
105
- pub pure fn from_str(s: &str) -> Option<UUID> {
106
- assert s.len() == 36u;
107
-
108
- let uuid = UUID { a: 0u32, b: 0u32, c: 0u32, d: 0u32 };
109
- let r = do str::as_buf(s) |buf, _len| {
110
- unsafe { uuid::uuid_parse(buf, &uuid) }
111
- };
112
-
113
- if r == 0 { Some(uuid) } else { None }
114
- }
115
-
116
86
/// Convert a string to a uuid
117
87
impl UUID: from_str::FromStr {
118
- static pure fn from_str(s: &str) -> Option<UUID> { from_str(s) }
88
+ static pure fn from_str(s: &str) -> Option<UUID> {
89
+ assert s.len() <= 36u;
90
+ let mut uuid = UUID([0, ..36]);
91
+
92
+ let r = do str::as_buf(s) |buf, _| {
93
+ unsafe { uuid::uuid_parse(buf, &uuid) }
94
+ };
95
+
96
+ if r == 0 { Some(uuid) } else { None }
97
+ }
119
98
}
120
99
121
100
#[cfg(test)]
122
101
mod test {
123
102
pub use from_str::FromStr;
124
103
104
+ #[test]
105
+ fn test_invalid() {
106
+ let s = " ";
107
+ assert FromStr::from_str::<UUID>(s) == None;
108
+ }
109
+
125
110
#[test]
126
111
fn test_valid() {
127
- for uint::range(0u, 100000u ) |_i | {
112
+ for uint::range(0u, 100000 ) |_ | {
128
113
let uuid = UUID::new();
129
- assert uuid.a != 0 && uuid.b != 0 && uuid.c != 0 && uuid.d != 0 ;
130
- assert from_str(uuid.to_str()) == Some(uuid);
114
+ assert * uuid != [0, ..36] ;
115
+ assert FromStr:: from_str(uuid.to_str()) == Some(uuid);
131
116
132
117
let uuid = UUID::new_random();
133
- assert uuid.a != 0 && uuid.b != 0 && uuid.c != 0 && uuid.d != 0 ;
134
- assert from_str(uuid.to_str()) == Some(uuid);
118
+ assert * uuid != [0, ..36] ;
119
+ assert FromStr:: from_str(uuid.to_str()) == Some(uuid);
135
120
136
121
let uuid = UUID::new_time();
137
- assert uuid.a != 0 && uuid.b != 0 && uuid.c != 0 && uuid.d != 0 ;
138
- assert from_str(uuid.to_str()) == Some(uuid)
122
+ assert * uuid != [0, ..36] ;
123
+ assert FromStr:: from_str(uuid.to_str()) == Some(uuid)
139
124
}
140
125
}
141
126
142
127
#[test]
143
- fn test_invalid() {
144
- assert from_str(" ") == None;
128
+ fn test_ordering() {
129
+ let s1 = "00000000-0000-0000-0000-000000000000";
130
+ let s2 = "00000000-0000-0000-0000-000000000001";
131
+
132
+ let u1 = FromStr::from_str::<UUID>(s1).unwrap();
133
+ let u2 = FromStr::from_str::<UUID>(s2).unwrap();
134
+
135
+ assert u1 < u2;
136
+ assert u2 > u1;
137
+
138
+ assert u1 <= u1;
139
+ assert u2 >= u2;
145
140
}
146
141
}
0 commit comments