|
| 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +trait Hash<H> { |
| 12 | + fn hash2(&self, hasher: &H) -> u64; |
| 13 | +} |
| 14 | + |
| 15 | +trait Stream { |
| 16 | + fn input(&mut self, bytes: &[u8]); |
| 17 | + fn result(&self) -> u64; |
| 18 | +} |
| 19 | + |
| 20 | +trait StreamHasher<S: Stream> { |
| 21 | + fn stream(&self) -> S; |
| 22 | +} |
| 23 | + |
| 24 | +////////////////////////////////////////////////////////////////////////////// |
| 25 | + |
| 26 | +trait StreamHash<S: Stream, H: StreamHasher<S>>: Hash<H> { |
| 27 | + fn input_stream(&self, stream: &mut S); |
| 28 | +} |
| 29 | + |
| 30 | +impl<S: Stream, H: StreamHasher<S>> Hash<H> for u8 { |
| 31 | + fn hash2(&self, hasher: &H) -> u64 { |
| 32 | + let mut stream = hasher.stream(); |
| 33 | + self.input_stream(&mut stream); |
| 34 | + stream.result() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<S: Stream, H: StreamHasher<S>> StreamHash<S, H> for u8 { |
| 39 | + fn input_stream(&self, stream: &mut S) { |
| 40 | + stream.input([*self]); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fn main() {} |
0 commit comments