rustc_data_structures/
packed.rs

1use std::cmp::Ordering;
2use std::fmt;
3
4use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
5
6use crate::stable_hasher::{HashStable, StableHasher};
7
8/// A packed 128-bit integer. Useful for reducing the size of structures in
9/// some cases.
10#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
11#[repr(packed(8))]
12pub struct Pu128(pub u128);
13
14impl Pu128 {
15    #[inline]
16    pub fn get(self) -> u128 {
17        self.0
18    }
19}
20
21impl From<Pu128> for u128 {
22    #[inline]
23    fn from(value: Pu128) -> Self {
24        value.get()
25    }
26}
27
28impl From<u128> for Pu128 {
29    #[inline]
30    fn from(value: u128) -> Self {
31        Self(value)
32    }
33}
34
35impl PartialEq<u128> for Pu128 {
36    #[inline]
37    fn eq(&self, other: &u128) -> bool {
38        ({ self.0 }) == *other
39    }
40}
41
42impl PartialOrd<u128> for Pu128 {
43    #[inline]
44    fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
45        { self.0 }.partial_cmp(other)
46    }
47}
48
49impl fmt::Display for Pu128 {
50    #[inline]
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        { self.0 }.fmt(f)
53    }
54}
55
56impl fmt::UpperHex for Pu128 {
57    #[inline]
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        { self.0 }.fmt(f)
60    }
61}
62
63impl<CTX> HashStable<CTX> for Pu128 {
64    #[inline]
65    fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
66        { self.0 }.hash_stable(ctx, hasher)
67    }
68}
69
70impl<S: Encoder> Encodable<S> for Pu128 {
71    #[inline]
72    fn encode(&self, s: &mut S) {
73        { self.0 }.encode(s);
74    }
75}
76
77impl<D: Decoder> Decodable<D> for Pu128 {
78    #[inline]
79    fn decode(d: &mut D) -> Self {
80        Self(u128::decode(d))
81    }
82}