murmur3/lib.rs
1// Copyright (c) 2020 Stu Small
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9//! A pure rust implementation of the fast, non-cryptographic hash [murmur3](https://en.wikipedia.org/wiki/MurmurHash)
10#![deny(missing_docs)]
11
12mod murmur3_32;
13mod murmur3_x64_128;
14mod murmur3_x86_128;
15
16use std::io::{ErrorKind, Read, Result};
17
18pub use self::murmur3_32::*;
19pub use self::murmur3_x64_128::*;
20pub use self::murmur3_x86_128::*;
21
22fn copy_into_array<A, T>(slice: &[T]) -> A
23where
24 A: Default + AsMut<[T]>,
25 T: Copy,
26{
27 let mut a = A::default();
28 <A as AsMut<[T]>>::as_mut(&mut a).copy_from_slice(slice);
29 a
30}
31
32/// Try to fill buf with data from source, dealing with short reads such as
33/// caused by Chain.
34///
35/// Errors: See `std::io::Read`.
36fn read_bytes<R>(source: &mut R, buf: &mut [u8]) -> Result<usize>
37where
38 R: Read,
39{
40 let mut offset = 0;
41 loop {
42 match source.read(&mut buf[offset..]) {
43 Ok(0) => {
44 return Ok(offset);
45 }
46 Ok(n) => {
47 offset += n;
48 if offset == buf.len() {
49 return Ok(offset);
50 }
51 }
52 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
53 Err(e) => {
54 return Err(e);
55 }
56 }
57 }
58}