Skip to content

Commit

Permalink
Merge pull request #423 from emlun/refactor-cmp
Browse files Browse the repository at this point in the history
Refactor: Extract function util::c_cmp_to_ordering
  • Loading branch information
ehuss authored May 27, 2019
2 parents 0f66776 + b50684a commit c29c88d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
35 changes: 29 additions & 6 deletions src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use libc;

use {raw, Error, ObjectType, IntoCString};

use util::Binding;
use util::{c_cmp_to_ordering, Binding};

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

impl Ord for Oid {
fn cmp(&self, other: &Oid) -> Ordering {
match unsafe { raw::git_oid_cmp(&self.raw, &other.raw) } {
0 => Ordering::Equal,
n if n < 0 => Ordering::Less,
_ => Ordering::Greater,
}
c_cmp_to_ordering(unsafe { raw::git_oid_cmp(&self.raw, &other.raw) })
}
}

Expand All @@ -181,6 +177,7 @@ mod tests {

use tempdir::TempDir;
use {ObjectType};
use super::Error;
use super::Oid;

#[test]
Expand All @@ -191,6 +188,32 @@ mod tests {
assert!(Oid::from_bytes(b"00000000000000000000").is_ok());
}

#[test]
fn comparisons() -> Result<(), Error> {
assert_eq!(Oid::from_str("decbf2b")?, Oid::from_str("decbf2b")?);
assert!(Oid::from_str("decbf2b")? <= Oid::from_str("decbf2b")?);
assert!(Oid::from_str("decbf2b")? >= Oid::from_str("decbf2b")?);
{
let o = Oid::from_str("decbf2b")?;
assert_eq!(o, o);
assert!(o <= o);
assert!(o >= o);
}
assert_eq!(
Oid::from_str("decbf2b")?,
Oid::from_str("decbf2b000000000000000000000000000000000")?
);
assert!(
Oid::from_bytes(b"00000000000000000000")? < Oid::from_bytes(b"00000000000000000001")?
);
assert!(Oid::from_bytes(b"00000000000000000000")? < Oid::from_str("decbf2b")?);
assert_eq!(
Oid::from_bytes(b"00000000000000000000")?,
Oid::from_str("3030303030303030303030303030303030303030")?
);
Ok(())
}

#[test]
fn zero_is_zero() {
assert!(Oid::zero().is_zero());
Expand Down
8 changes: 2 additions & 6 deletions src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::str;

use {raw, Error, Oid, Repository, ReferenceType, Object, ObjectType, Blob, Commit, Tree, Tag};
use object::CastOrPanic;
use util::Binding;
use util::{c_cmp_to_ordering, Binding};

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

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

impl<'repo> Ord for Reference<'repo> {
fn cmp(&self, other: &Reference<'repo>) -> Ordering {
match unsafe { raw::git_reference_cmp(&*self.raw, &*other.raw) } {
0 => Ordering::Equal,
n if n < 0 => Ordering::Less,
_ => Ordering::Greater,
}
c_cmp_to_ordering(unsafe { raw::git_reference_cmp(&*self.raw, &*other.raw) })
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::str;
use libc::{self, c_int, c_char, c_void};

use {panic, raw, Oid, Repository, Error, Object, ObjectType};
use util::{Binding, IntoCString};
use util::{c_cmp_to_ordering, Binding, IntoCString};

/// A structure to represent a git [tree][1]
///
Expand Down Expand Up @@ -351,11 +351,7 @@ impl<'a> PartialOrd for TreeEntry<'a> {
}
impl<'a> Ord for TreeEntry<'a> {
fn cmp(&self, other: &TreeEntry<'a>) -> Ordering {
match unsafe { raw::git_tree_entry_cmp(&*self.raw(), &*other.raw()) } {
0 => Ordering::Equal,
n if n < 0 => Ordering::Less,
_ => Ordering::Greater,
}
c_cmp_to_ordering(unsafe { raw::git_tree_entry_cmp(&*self.raw(), &*other.raw()) })
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::cmp::Ordering;
use std::ffi::{CString, OsStr, OsString};
use std::iter::IntoIterator;
use std::path::{Path, PathBuf};
use libc::{c_char, size_t};
use libc::{c_char, c_int, size_t};

use {raw, Error};

Expand Down Expand Up @@ -150,3 +151,11 @@ pub fn into_opt_c_string<S>(opt_s: Option<S>) -> Result<Option<CString>, Error>
Some(s) => Ok(Some(try!(s.into_c_string()))),
}
}

pub fn c_cmp_to_ordering(cmp: c_int) -> Ordering {
match cmp {
0 => Ordering::Equal,
n if n < 0 => Ordering::Less,
_ => Ordering::Greater,
}
}

0 comments on commit c29c88d

Please sign in to comment.