1 // Copyright 2016 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.
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.
11 use rustc_serialize
::{Encodable, Decodable, Encoder, Decoder}
;
13 const FINGERPRINT_LENGTH
: usize = 16;
15 #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
16 pub struct Fingerprint(pub [u8; FINGERPRINT_LENGTH
]);
20 pub fn zero() -> Fingerprint
{
21 Fingerprint([0; FINGERPRINT_LENGTH
])
24 pub fn from_smaller_hash(hash
: u64) -> Fingerprint
{
25 let mut result
= Fingerprint
::zero();
26 result
.0[0] = (hash
>> 0) as u8;
27 result
.0[1] = (hash
>> 8) as u8;
28 result
.0[2] = (hash
>> 16) as u8;
29 result
.0[3] = (hash
>> 24) as u8;
30 result
.0[4] = (hash
>> 32) as u8;
31 result
.0[5] = (hash
>> 40) as u8;
32 result
.0[6] = (hash
>> 48) as u8;
33 result
.0[7] = (hash
>> 56) as u8;
37 pub fn to_smaller_hash(&self) -> u64 {
38 ((self.0[0] as u64) << 0) |
39 ((self.0[1] as u64) << 8) |
40 ((self.0[2] as u64) << 16) |
41 ((self.0[3] as u64) << 24) |
42 ((self.0[4] as u64) << 32) |
43 ((self.0[5] as u64) << 40) |
44 ((self.0[6] as u64) << 48) |
45 ((self.0[7] as u64) << 56)
49 impl Encodable
for Fingerprint
{
51 fn encode
<S
: Encoder
>(&self, s
: &mut S
) -> Result
<(), S
::Error
> {
52 for &byte
in &self.0[..] {
59 impl Decodable
for Fingerprint
{
61 fn decode
<D
: Decoder
>(d
: &mut D
) -> Result
<Fingerprint
, D
::Error
> {
62 let mut result
= Fingerprint([0u8; FINGERPRINT_LENGTH
]);
63 for byte
in &mut result
.0[..] {
70 impl ::std
::fmt
::Display
for Fingerprint
{
71 fn fmt(&self, formatter
: &mut ::std
::fmt
::Formatter
) -> Result
<(), ::std
::fmt
::Error
> {
72 for i
in 0 .. self.0.len() {
74 write
!(formatter
, "::")?
;
77 write
!(formatter
, "{}", self.0[i
])?
;