Skip to content

Commit cb7d863

Browse files
committed
Add Pu128 = #[repr(packed(8))] u128
1 parent 0547c41 commit cb7d863

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub mod aligned;
9393
pub mod frozen;
9494
mod hashes;
9595
pub mod owned_slice;
96+
pub mod packed;
9697
pub mod sso;
9798
pub mod steal;
9899
pub mod tagged_ptr;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use crate::stable_hasher::{HashStable, StableHasher};
2+
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
3+
use std::cmp::Ordering;
4+
use std::fmt;
5+
6+
#[repr(packed(8))]
7+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
8+
pub struct Pu128(pub u128);
9+
10+
impl Pu128 {
11+
#[inline]
12+
pub fn get(self) -> u128 {
13+
self.0
14+
}
15+
}
16+
17+
impl From<u128> for Pu128 {
18+
#[inline]
19+
fn from(value: u128) -> Self {
20+
Self(value)
21+
}
22+
}
23+
24+
impl PartialEq<u128> for Pu128 {
25+
#[inline]
26+
fn eq(&self, other: &u128) -> bool {
27+
({ self.0 }) == *other
28+
}
29+
}
30+
31+
impl PartialOrd<u128> for Pu128 {
32+
#[inline]
33+
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
34+
{ self.0 }.partial_cmp(other)
35+
}
36+
}
37+
38+
impl fmt::Display for Pu128 {
39+
#[inline]
40+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41+
{ self.0 }.fmt(f)
42+
}
43+
}
44+
45+
impl fmt::UpperHex for Pu128 {
46+
#[inline]
47+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48+
{ self.0 }.fmt(f)
49+
}
50+
}
51+
52+
impl<CTX> HashStable<CTX> for Pu128 {
53+
#[inline]
54+
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
55+
{ self.0 }.hash_stable(ctx, hasher)
56+
}
57+
}
58+
59+
impl<S: Encoder> Encodable<S> for Pu128 {
60+
#[inline]
61+
fn encode(&self, s: &mut S) {
62+
{ self.0 }.encode(s);
63+
}
64+
}
65+
66+
impl<D: Decoder> Decodable<D> for Pu128 {
67+
#[inline]
68+
fn decode(d: &mut D) -> Self {
69+
Self(u128::decode(d))
70+
}
71+
}

0 commit comments

Comments
 (0)