]> git.proxmox.com Git - rustc.git/blob - src/librustc_incremental/ich/fingerprint.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / librustc_incremental / ich / fingerprint.rs
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.
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 use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
12
13 const FINGERPRINT_LENGTH: usize = 16;
14
15 #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
16 pub struct Fingerprint(pub [u8; FINGERPRINT_LENGTH]);
17
18 impl Fingerprint {
19 #[inline]
20 pub fn zero() -> Fingerprint {
21 Fingerprint([0; FINGERPRINT_LENGTH])
22 }
23
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;
34 result
35 }
36
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)
46 }
47 }
48
49 impl Encodable for Fingerprint {
50 #[inline]
51 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
52 for &byte in &self.0[..] {
53 s.emit_u8(byte)?;
54 }
55 Ok(())
56 }
57 }
58
59 impl Decodable for Fingerprint {
60 #[inline]
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[..] {
64 *byte = d.read_u8()?;
65 }
66 Ok(result)
67 }
68 }
69
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() {
73 if i > 0 {
74 write!(formatter, "::")?;
75 }
76
77 write!(formatter, "{}", self.0[i])?;
78 }
79 Ok(())
80 }
81 }