endian_hasher/lib.rs
1// Copyright 2016 Jeffrey Burdges.
2
3/*!
4These simple wrapper types for `Hasher` change the endianness
5used when hashing primitive numeric types.
6
7# Examples
8
9```
10use std::collections::hash_map::DefaultHasher;
11use std::hash::Hasher;
12use endian_hasher::*;
13
14assert_eq!( {
15 let mut h1 = HasherToLE(DefaultHasher::new());
16 h1.write_i16(-3);
17 h1.finish()
18}, {
19 let mut h0 = DefaultHasher::new();
20 h0.write_i16( i16::to_le(-3) );
21 h0.finish()
22} );
23
24assert_eq!( {
25 let mut h1 = HasherToBE(DefaultHasher::new());
26 h1.write_u32(79);
27 h1.finish()
28}, {
29 let mut h0 = DefaultHasher::new();
30 h0.write_u32( 79u32.to_be() );
31 h0.finish()
32} );
33
34assert_eq!( {
35 let mut h1 = HasherSwapBytes(DefaultHasher::new());
36 h1.write_u64(0x12345678);
37 h1.finish()
38}, {
39 let mut h0 = DefaultHasher::new();
40 h0.write_u64( u64::swap_bytes(0x12345678) );
41 h0.finish()
42} );
43```
44*/
45
46use std::hash::{Hasher}; // Hash
47
48#[doc(hidden)]
49macro_rules! conv_hasher{
50 ($i:ident,$t:ident) =>{
51
52// #[doc="Apply a primitive numeric type's `stringify!($t)` method before hashing."]
53// pub struct $i<H: Hasher>(pub H);
54
55impl<H: Hasher> Hasher for $i<H> {
56 fn finish(&self) -> u64 { self.0.finish() }
57 fn write(&mut self, bytes: &[u8]) { self.0.write(bytes) }
58
59 fn write_u8(&mut self, n: u8) { self.0.write_u8( u8::$t(n) ) }
60 fn write_u16(&mut self, n: u16) { self.0.write_u16( u16::$t(n) ) }
61 fn write_u32(&mut self, n: u32) { self.0.write_u32( u32::$t(n) ) }
62 fn write_u64(&mut self, n: u64) { self.0.write_u64( u64::$t(n) ) }
63 fn write_usize(&mut self, n: usize) { self.0.write_usize( usize::$t(n)) }
64 fn write_i8(&mut self, n: i8) { self.0.write_i8( i8::$t(n) ) }
65 fn write_i16(&mut self, n: i16) { self.0.write_i16( i16::$t(n) ) }
66 fn write_i32(&mut self, n: i32) { self.0.write_i32( i32::$t(n) ) }
67 fn write_i64(&mut self, n: i64) { self.0.write_i64( i64::$t(n) ) }
68 fn write_isize(&mut self, n: isize) { self.0.write_isize( isize::$t(n) ) }
69}
70
71 }
72} // macro_rules! conv_hasher
73
74/// Apply a primitive numeric type's `to_be` method before hashing.
75pub struct HasherToBE<H: Hasher>(pub H);
76conv_hasher!(HasherToBE, to_be );
77
78/// Apply a primitive numeric type's `to_le` method before hashing.
79pub struct HasherToLE<H: Hasher>(pub H);
80conv_hasher!(HasherToLE, to_le );
81
82/// Apply a primitive numeric type's `from_be` method before hashing.
83pub struct HasherFromBE<H: Hasher>(pub H);
84conv_hasher!(HasherFromBE, from_be );
85
86/// Apply a primitive numeric type's `from_le` method before hashing.
87pub struct HasherFromLE<H: Hasher>(pub H);
88conv_hasher!(HasherFromLE, from_le );
89
90/// Apply a primitive numeric type's `swap_bytes` method before hashing.
91pub struct HasherSwapBytes<H: Hasher>(pub H);
92conv_hasher!(HasherSwapBytes, swap_bytes );
93
94
95/*
96#[cfg(test)]
97mod tests {
98#![allow(deprecated)]
99
100use super::*;
101use std::hash::{SipHasher24, Hasher};
102
103#[test]
104fn it_works() {
105
106}
107
108} // mod tests
109*/
110