Skip to main content

xdrs/
int128.rs

1//! Implement `ReadXDR` and `WriteXDR` for `i128` and `u128` using encodings `(i64,u64)` and
2//! `(u64,u64)`, respectively.
3
4const _: () = {
5    use crate::read_xdr::*;
6    use crate::serialize::reader::*;
7
8    impl<'r> ReadXDR<'r> for i128 {
9        #[inline]
10        fn read_xdr<R: Reader<'r>>(r: &mut R) -> ReadResult<Self> {
11            let highbits: i64 = read_xdr(r)?;
12            let lowbits: u64 = read_xdr(r)?;
13            let i: i128 = ((highbits as i128) << 64) | (lowbits as i128);
14
15            Ok(i)
16        }
17    }
18
19    impl<'r> ReadXDR<'r> for u128 {
20        #[inline]
21        fn read_xdr<R: Reader<'r>>(r: &mut R) -> ReadResult<Self> {
22            let highbits: u64 = read_xdr(r)?;
23            let lowbits: u64 = read_xdr(r)?;
24            let u: u128 = ((highbits as u128) << 64) | (lowbits as u128);
25
26            Ok(u)
27        }
28    }
29};
30
31const _: () = {
32    use crate::serialize::writer::*;
33    use crate::serialize::writer_unchecked::*;
34    use crate::write_xdr::*;
35
36    impl WriteXDR for i128 {
37        #[inline]
38        fn write_xdr<F, W: Writer<F>>(&self, w: &mut W) -> WriteResult<()> {
39            let highbits = (*self >> 64) as i64;
40            let lowbits = (*self & 0xffffffffffffffff) as u64;
41
42            write_xdr(&highbits, w)?;
43            write_xdr(&lowbits, w)
44        }
45
46        #[inline]
47        unsafe fn write_xdr_unchecked<F, W: WriterUnchecked<F>>(&self, w: &mut W) {
48            let highbits = (*self >> 64) as i64;
49            let lowbits = (*self & 0xffffffffffffffff) as u64;
50
51            // SAFETY: the caller guarantees space for both halves.
52            unsafe {
53                write_xdr_unchecked(&highbits, w);
54                write_xdr_unchecked(&lowbits, w);
55            }
56        }
57    }
58
59    impl WriteXDR for u128 {
60        #[inline]
61        fn write_xdr<F, W: Writer<F>>(&self, w: &mut W) -> WriteResult<()> {
62            let highbits = (*self >> 64) as u64;
63            let lowbits = (*self & 0xffffffffffffffff) as u64;
64
65            write_xdr(&highbits, w)?;
66            write_xdr(&lowbits, w)
67        }
68
69        #[inline]
70        unsafe fn write_xdr_unchecked<F, W: WriterUnchecked<F>>(&self, w: &mut W) {
71            let highbits = (*self >> 64) as u64;
72            let lowbits = (*self & 0xffffffffffffffff) as u64;
73
74            // SAFETY: the caller guarantees space for both halves.
75            unsafe {
76                write_xdr_unchecked(&highbits, w);
77                write_xdr_unchecked(&lowbits, w);
78            }
79        }
80    }
81};