Skip to content

Commit 36d969f

Browse files
committed
switch to a fixed sized array
1 parent ef40543 commit 36d969f

File tree

1 file changed

+48
-53
lines changed

1 file changed

+48
-53
lines changed

uuid.rc

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,69 +22,50 @@ extern mod uuid {
2222
#[cfg(target_os = "linux")]
2323
#[cfg(target_os = "freebsd")]
2424
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);
2828

29-
fn uuid_parse(s: *u8, uuid: UUID) -> libc::c_int;
29+
fn uuid_parse(s: *u8, uuid: &UUID) -> libc::c_int;
3030

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);
3434
}
3535

3636
/// 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]);
4338

4439
impl UUID {
4540
/// Create a new uuid
4641
static fn new() -> UUID {
47-
let mut uuid = UUID { a: 0u32, b: 0u32, c: 0u32, d: 0u32 };
42+
let mut uuid = UUID([0, ..36]);
4843
uuid::uuid_generate(&mut uuid);
4944
uuid
5045
}
5146

5247
/// Create a uuid from the current time and mac address
5348
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]);
5550
uuid::uuid_generate_random(&mut uuid);
5651
uuid
5752
}
5853

5954
/// Create a uuid from a random number generator
6055
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]);
6257
uuid::uuid_generate_time(&mut uuid);
6358
uuid
6459
}
6560
}
6661

6762
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 }
7564
pure fn ne(&self, other: &UUID) -> bool { !self.eq(other) }
7665
}
7766

7867
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 }
8869
pure fn le(&self, other: &UUID) -> bool { !(*other).lt(self) }
8970
pure fn ge(&self, other: &UUID) -> bool { !self.lt(other) }
9071
pure fn gt(&self, other: &UUID) -> bool { (*other).lt(self) }
@@ -102,45 +83,59 @@ pub impl UUID: to_str::ToStr {
10283
}
10384
}
10485

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-
11686
/// Convert a string to a uuid
11787
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+
}
11998
}
12099

121100
#[cfg(test)]
122101
mod test {
123102
pub use from_str::FromStr;
124103

104+
#[test]
105+
fn test_invalid() {
106+
let s = " ";
107+
assert FromStr::from_str::<UUID>(s) == None;
108+
}
109+
125110
#[test]
126111
fn test_valid() {
127-
for uint::range(0u, 100000u) |_i| {
112+
for uint::range(0u, 100000) |_| {
128113
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);
131116

132117
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);
135120

136121
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)
139124
}
140125
}
141126

142127
#[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;
145140
}
146141
}

0 commit comments

Comments
 (0)