Skip to content

Commit 2e6c948

Browse files
committed
Merge pull request #37 from thijsc/object_id_debug
Show object id formatted as string in debug trait and derive Hash
2 parents c4cdfa8 + 966709d commit 2e6c948

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/oid.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl error::Error for Error {
8888
}
8989

9090
/// A wrapper around raw 12-byte ObjectId representations.
91-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
91+
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9292
pub struct ObjectId {
9393
id: [u8; 12],
9494
}
@@ -279,6 +279,12 @@ impl fmt::Display for ObjectId {
279279
}
280280
}
281281

282+
impl fmt::Debug for ObjectId {
283+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
284+
f.write_str(&format!("ObjectId({})", self.to_hex()))
285+
}
286+
}
287+
282288
impl Decodable for ObjectId {
283289
fn decode<D: Decoder>(d: &mut D) -> result::Result<Self, D::Error> {
284290
let str = try!(d.read_str());
@@ -329,3 +335,22 @@ fn count_is_big_endian() {
329335
assert_eq!(0x33u8, oid.bytes()[COUNTER_OFFSET + 2]);
330336
}
331337

338+
#[test]
339+
fn test_display() {
340+
let id = ObjectId::with_string("53e37d08776f724e42000000").unwrap();
341+
342+
assert_eq!(
343+
format!("{}", id),
344+
"53e37d08776f724e42000000"
345+
)
346+
}
347+
348+
#[test]
349+
fn test_debug() {
350+
let id = ObjectId::with_string("53e37d08776f724e42000000").unwrap();
351+
352+
assert_eq!(
353+
format!("{:?}", id),
354+
"ObjectId(53e37d08776f724e42000000)"
355+
)
356+
}

0 commit comments

Comments
 (0)