Skip to content

Commit b50684a

Browse files
committed
Extract function util::c_cmp_to_ordering
1 parent 50132f5 commit b50684a

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

src/oid.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use libc;
77

88
use {raw, Error, ObjectType, IntoCString};
99

10-
use util::Binding;
10+
use util::{c_cmp_to_ordering, Binding};
1111

1212
/// Unique identity of any object (commit, tree, blob, tag).
1313
#[derive(Copy, Clone)]
@@ -156,11 +156,7 @@ impl PartialOrd for Oid {
156156

157157
impl Ord for Oid {
158158
fn cmp(&self, other: &Oid) -> Ordering {
159-
match unsafe { raw::git_oid_cmp(&self.raw, &other.raw) } {
160-
0 => Ordering::Equal,
161-
n if n < 0 => Ordering::Less,
162-
_ => Ordering::Greater,
163-
}
159+
c_cmp_to_ordering(unsafe { raw::git_oid_cmp(&self.raw, &other.raw) })
164160
}
165161
}
166162

src/reference.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::str;
77

88
use {raw, Error, Oid, Repository, ReferenceType, Object, ObjectType, Blob, Commit, Tree, Tag};
99
use object::CastOrPanic;
10-
use util::Binding;
10+
use util::{c_cmp_to_ordering, Binding};
1111

1212
struct Refdb<'repo>(&'repo Repository);
1313

@@ -246,11 +246,7 @@ impl<'repo> PartialOrd for Reference<'repo> {
246246

247247
impl<'repo> Ord for Reference<'repo> {
248248
fn cmp(&self, other: &Reference<'repo>) -> Ordering {
249-
match unsafe { raw::git_reference_cmp(&*self.raw, &*other.raw) } {
250-
0 => Ordering::Equal,
251-
n if n < 0 => Ordering::Less,
252-
_ => Ordering::Greater,
253-
}
249+
c_cmp_to_ordering(unsafe { raw::git_reference_cmp(&*self.raw, &*other.raw) })
254250
}
255251
}
256252

src/tree.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::str;
99
use libc::{self, c_int, c_char, c_void};
1010

1111
use {panic, raw, Oid, Repository, Error, Object, ObjectType};
12-
use util::{Binding, IntoCString};
12+
use util::{c_cmp_to_ordering, Binding, IntoCString};
1313

1414
/// A structure to represent a git [tree][1]
1515
///
@@ -351,11 +351,7 @@ impl<'a> PartialOrd for TreeEntry<'a> {
351351
}
352352
impl<'a> Ord for TreeEntry<'a> {
353353
fn cmp(&self, other: &TreeEntry<'a>) -> Ordering {
354-
match unsafe { raw::git_tree_entry_cmp(&*self.raw(), &*other.raw()) } {
355-
0 => Ordering::Equal,
356-
n if n < 0 => Ordering::Less,
357-
_ => Ordering::Greater,
358-
}
354+
c_cmp_to_ordering(unsafe { raw::git_tree_entry_cmp(&*self.raw(), &*other.raw()) })
359355
}
360356
}
361357

src/util.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use std::cmp::Ordering;
12
use std::ffi::{CString, OsStr, OsString};
23
use std::iter::IntoIterator;
34
use std::path::{Path, PathBuf};
4-
use libc::{c_char, size_t};
5+
use libc::{c_char, c_int, size_t};
56

67
use {raw, Error};
78

@@ -150,3 +151,11 @@ pub fn into_opt_c_string<S>(opt_s: Option<S>) -> Result<Option<CString>, Error>
150151
Some(s) => Ok(Some(try!(s.into_c_string()))),
151152
}
152153
}
154+
155+
pub fn c_cmp_to_ordering(cmp: c_int) -> Ordering {
156+
match cmp {
157+
0 => Ordering::Equal,
158+
n if n < 0 => Ordering::Less,
159+
_ => Ordering::Greater,
160+
}
161+
}

0 commit comments

Comments
 (0)