Skip to content

Commit 162ba89

Browse files
committed
auto merge of #10359 : tomassedovic/rust/typeid-iterbytes, r=pcwalton
I'm not sure this is something you're interested in, but I was playing around the Any trait a bit and I wanted to try it as a key in a HashMap. To do that, TypeId needs to implement IterBytes.
2 parents 785f9b8 + 0865b4d commit 162ba89

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/libstd/any.rs

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use cast::transmute;
1515
use cmp::Eq;
1616
use option::{Option, Some, None};
17+
use to_bytes::{IterBytes, Cb};
1718
use to_str::ToStr;
1819
use unstable::intrinsics;
1920
use util::Void;
@@ -42,6 +43,12 @@ impl Eq for TypeId {
4243
}
4344
}
4445

46+
impl IterBytes for TypeId {
47+
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
48+
self.t.iter_bytes(lsb0, f)
49+
}
50+
}
51+
4552
///////////////////////////////////////////////////////////////////////////////
4653
// Any trait
4754
///////////////////////////////////////////////////////////////////////////////
@@ -175,6 +182,7 @@ mod tests {
175182
use super::*;
176183
use super::AnyRefExt;
177184
use option::{Some, None};
185+
use hash::Hash;
178186

179187
#[deriving(Eq)]
180188
struct Test;
@@ -197,6 +205,13 @@ mod tests {
197205
assert_eq!(c, f);
198206
}
199207

208+
#[test]
209+
fn type_id_hash() {
210+
let (a, b) = (TypeId::of::<uint>(), TypeId::of::<uint>::());
211+
212+
assert_eq!(a.hash(), b.hash());
213+
}
214+
200215
#[test]
201216
fn any_as_void_ptr() {
202217
let (a, b, c) = (~5u as ~Any, ~TEST as ~Any, ~Test as ~Any);

0 commit comments

Comments
 (0)