Skip to content

Instantly share code, notes, and snippets.

@dcolish
Created June 28, 2013 04:13
Show Gist options
  • Save dcolish/5882409 to your computer and use it in GitHub Desktop.
Save dcolish/5882409 to your computer and use it in GitHub Desktop.
fnv32.rs:51:18: 51:34 error: borrowed value does not live long enough
fnv32.rs:51 let res = zip(comp, spec).iter()
^~~~~~~~~~~~~~~~
fnv32.rs:51:18: 53:24 note: borrowed pointer must be valid for the method call at 51:18...
fnv32.rs:51 let res = zip(comp, spec).iter()
fnv32.rs:52 .transform(|&(x, y)| {hash32(&x) == (y as u32) })
fnv32.rs:53 .all(|x| x);
fnv32.rs:51:18: 52:13 note: ...but borrowed value is only valid for the method call at 51:18
fnv32.rs:51 let res = zip(comp, spec).iter()
fnv32.rs:52 .transform(|&(x, y)| {hash32(&x) == (y as u32) })
error: aborting due to previous error
// Port of FNV 32 to Rust
// ref: http://isthe.com/chongo/src/fnv/hash_32.c
use std::to_bytes::{ToBytes};
fn main() {
// let s = "a";
let m = hash32(&~"a");
println(m.to_str());
}
pub fn hash32<T: ToBytes>(input: &T) -> u32 {
let data: ~[u8] = input.to_bytes(false);
data.iter().fold(0u32, |a, b| {
(a + (a<<1) + (a<<4) + (a<<7) + (a<<8) + (a<<24)) ^ (*b as u32)
})
}
#[cfg(test)]
mod tests {
use std::vec::{zip};
use hash32;
#[test]
fn testHash32() {
let comp = ~[
~"a"
, ~"b"
, ~"c"
, ~"d"
, ~"e"
, ~"fo"
, ~"foo"
, ~"foob"
, ~"fooba"
, ~"foobar"
];
let spec = ~[
0x00000061
, 0x00000062
, 0x00000063
, 0x00000064
, 0x00000065
, 0x00000066
, 0x6600a0fd
, 0x8ffd6e28
, 0xd3f4689a
];
let res = zip(comp, spec).iter()
.transform(|&(x, y)| {hash32(&x) == (y as u32) })
.all(|x| x);
assert!(res == true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment