forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
112 lines (94 loc) · 3.48 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Support code for encoding and decoding types.
/*
Core encoding and decoding interfaces.
*/
#![doc(
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
html_playground_url = "https://play.rust-lang.org/",
test(attr(allow(unused_variables), deny(warnings)))
)]
#![feature(never_type)]
#![feature(nll)]
#![feature(associated_type_bounds)]
#![feature(min_specialization)]
#![feature(core_intrinsics)]
#![feature(maybe_uninit_slice)]
#![feature(let_else)]
#![feature(new_uninit)]
#![cfg_attr(test, feature(test))]
#![allow(rustc::internal)]
use std::convert::TryInto;
pub use self::serialize::{Decodable, Decoder, Encodable, Encoder};
mod collection_impls;
mod serialize;
pub mod json;
pub mod leb128;
pub mod opaque;
pub mod raw;
// An integer that will always encode to 8 bytes.
pub struct IntEncodedWithFixedSize(pub u64);
impl IntEncodedWithFixedSize {
pub const ENCODED_SIZE: usize = 8;
}
impl Encodable<opaque::Encoder> for IntEncodedWithFixedSize {
#[inline]
fn encode(&self, e: &mut opaque::Encoder) -> opaque::EncodeResult {
let _start_pos = e.position();
e.emit_raw_bytes(&self.0.to_le_bytes())?;
let _end_pos = e.position();
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
Ok(())
}
}
impl Encodable<opaque::FileEncoder> for IntEncodedWithFixedSize {
#[inline]
fn encode(&self, e: &mut opaque::FileEncoder) -> opaque::FileEncodeResult {
let _start_pos = e.position();
e.emit_raw_bytes(&self.0.to_le_bytes())?;
let _end_pos = e.position();
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
Ok(())
}
}
impl<'a> Decodable<opaque::Decoder<'a>> for IntEncodedWithFixedSize {
#[inline]
fn decode(decoder: &mut opaque::Decoder<'a>) -> IntEncodedWithFixedSize {
let _start_pos = decoder.position();
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
let value = u64::from_le_bytes(bytes.try_into().unwrap());
let _end_pos = decoder.position();
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
IntEncodedWithFixedSize(value)
}
}
impl Encodable<raw::Encoder> for IntEncodedWithFixedSize {
#[inline]
fn encode(&self, e: &mut raw::Encoder) -> raw::EncodeResult {
let _start_pos = e.position();
e.emit_raw_bytes(&self.0.to_le_bytes())?;
let _end_pos = e.position();
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
Ok(())
}
}
impl Encodable<raw::FileEncoder> for IntEncodedWithFixedSize {
#[inline]
fn encode(&self, e: &mut raw::FileEncoder) -> raw::FileEncodeResult {
let _start_pos = e.position();
e.emit_raw_bytes(&self.0.to_le_bytes())?;
let _end_pos = e.position();
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
Ok(())
}
}
impl<'a> Decodable<raw::Decoder<'a>> for IntEncodedWithFixedSize {
#[inline]
fn decode(decoder: &mut raw::Decoder<'a>) -> IntEncodedWithFixedSize {
let _start_pos = decoder.position();
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
let _end_pos = decoder.position();
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
let value = u64::from_le_bytes(bytes.try_into().unwrap());
IntEncodedWithFixedSize(value)
}
}