-
Notifications
You must be signed in to change notification settings - Fork 1
/
oid.rs
88 lines (77 loc) · 1.78 KB
/
oid.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
use std::{to_str, str};
use super::OID;
use ext;
impl FromStr for OID {
fn from_str(s: &str) -> Option<OID> {
unsafe {
let mut oid = OID { id: [0, .. 20] };
s.with_c_str(|c_str| {
if ext::git_oid_fromstr(&mut oid, c_str) == 0 {
Some(oid)
} else {
None
}
})
}
}
}
impl to_str::ToStr for OID {
fn to_str(&self) -> ~str {
let mut buf = [0 as u8, ..40];
unsafe {
ext::git_oid_fmt(buf.as_mut_ptr(), self);
}
str::from_utf8(buf).to_owned()
}
}
/* from <git2/oid.h> */
#[inline]
fn git_oid_cmp(a: &OID, b: &OID) -> int {
let mut idx = 0u;
while idx < 20u {
if a.id[idx] != b.id[idx] {
return (a.id[idx] as int) - (b.id[idx] as int)
}
idx += 1;
}
return 0;
}
impl Eq for OID {
fn eq(&self, other: &OID) -> bool {
git_oid_cmp(self, other) == 0
}
fn ne(&self, other: &OID) -> bool {
git_oid_cmp(self, other) != 0
}
}
impl Ord for OID {
fn lt(&self, other: &OID) -> bool {
git_oid_cmp(self, other) < 0
}
fn le(&self, other: &OID) -> bool {
git_oid_cmp(self, other) <= 0
}
fn gt(&self, other: &OID) -> bool {
git_oid_cmp(self, other) > 0
}
fn ge(&self, other: &OID) -> bool {
git_oid_cmp(self, other) >= 0
}
}
impl TotalEq for OID {
fn equals(&self, other: &OID) -> bool {
git_oid_cmp(self, other) == 0
}
}
impl TotalOrd for OID {
fn cmp(&self, other: &OID) -> Ordering {
let cmp = git_oid_cmp(self, other);
if cmp < 0 {
Less
} else if cmp == 0 {
Equal
} else {
Greater
}
}
}