rustc_data_structures/
svh.rs
1use std::fmt;
9
10use rustc_macros::{Decodable_NoContext, Encodable_NoContext};
11
12use crate::fingerprint::Fingerprint;
13use crate::stable_hasher;
14
15#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable_NoContext, Decodable_NoContext, Hash)]
16pub struct Svh {
17 hash: Fingerprint,
18}
19
20impl Svh {
21 pub fn new(hash: Fingerprint) -> Svh {
25 Svh { hash }
26 }
27
28 pub fn as_u128(self) -> u128 {
29 self.hash.as_u128()
30 }
31
32 pub fn to_hex(self) -> String {
33 format!("{:032x}", self.hash.as_u128())
34 }
35}
36
37impl fmt::Display for Svh {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.pad(&self.to_hex())
40 }
41}
42
43impl<T> stable_hasher::HashStable<T> for Svh {
44 #[inline]
45 fn hash_stable(&self, ctx: &mut T, hasher: &mut stable_hasher::StableHasher) {
46 let Svh { hash } = *self;
47 hash.hash_stable(ctx, hasher);
48 }
49}