1use rustc_hir::def::CtorOf;
2use rustc_index::Idx;
3
4use crate::rmeta::decoder::MetaBlob;
5use crate::rmeta::remapped_def_id::RemappedRawDefId;
6use crate::rmeta::*;
7
8pub(super) trait IsDefault: Default {
9 fn is_default(&self) -> bool;
10}
11
12impl<T> IsDefault for Option<T> {
13 fn is_default(&self) -> bool {
14 self.is_none()
15 }
16}
17
18impl IsDefault for AttrFlags {
19 fn is_default(&self) -> bool {
20 self.is_empty()
21 }
22}
23
24impl IsDefault for bool {
25 fn is_default(&self) -> bool {
26 !self
27 }
28}
29
30impl IsDefault for u32 {
31 fn is_default(&self) -> bool {
32 *self == 0
33 }
34}
35
36impl IsDefault for u64 {
37 fn is_default(&self) -> bool {
38 *self == 0
39 }
40}
41
42impl<T> IsDefault for LazyArray<T> {
43 fn is_default(&self) -> bool {
44 self.num_elems == 0
45 }
46}
47
48pub(super) trait FixedSizeEncoding: IsDefault {
55 type ByteArray;
58
59 fn from_bytes(b: &Self::ByteArray) -> Self;
60 fn write_to_bytes(self, b: &mut Self::ByteArray);
61}
62
63impl FixedSizeEncoding for u64 {
64 type ByteArray = [u8; 8];
65
66 #[inline]
67 fn from_bytes(b: &[u8; 8]) -> Self {
68 Self::from_le_bytes(*b)
69 }
70
71 #[inline]
72 fn write_to_bytes(self, b: &mut [u8; 8]) {
73 *b = self.to_le_bytes();
74 }
75}
76
77macro_rules! fixed_size_enum {
78 ($ty:ty { $(($($pat:tt)*))* } $( unreachable { $(($($upat:tt)*))+ } )?) => {
79 impl FixedSizeEncoding for Option<$ty> {
80 type ByteArray = [u8;1];
81
82 #[inline]
83 fn from_bytes(b: &[u8;1]) -> Self {
84 use $ty::*;
85 if b[0] == 0 {
86 return None;
87 }
88 match b[0] - 1 {
89 $(${index()} => Some($($pat)*),)*
90 _ => panic!("Unexpected {} code: {:?}", stringify!($ty), b[0]),
91 }
92 }
93
94 #[inline]
95 fn write_to_bytes(self, b: &mut [u8;1]) {
96 use $ty::*;
97 b[0] = match self {
98 None => unreachable!(),
99 $(Some($($pat)*) => 1 + ${index()},)*
100 $(Some($($($upat)*)|+) => unreachable!(),)?
101 }
102 }
103 }
104 }
105}
106
107macro_rules! defaulted_enum {
108 ($ty:ty { $(($($pat:tt)*))* } $( unreachable { $(($($upat:tt)*))+ } )?) => {
109 impl FixedSizeEncoding for $ty {
110 type ByteArray = [u8; 1];
111
112 #[inline]
113 fn from_bytes(b: &[u8; 1]) -> Self {
114 use $ty::*;
115 let val = match b[0] {
116 $(${index()} => $($pat)*,)*
117 _ => panic!("Unexpected {} code: {:?}", stringify!($ty), b[0]),
118 };
119 debug_assert_ne!((b[0] != 0), IsDefault::is_default(&val));
122 val
123 }
124
125 #[inline]
126 fn write_to_bytes(self, b: &mut [u8; 1]) {
127 debug_assert!(!IsDefault::is_default(&self));
128 use $ty::*;
129 b[0] = match self {
130 $($($pat)* => ${index()},)*
131 $($($($upat)*)|+ => unreachable!(),)?
132 };
133 debug_assert_ne!(b[0], 0);
134 }
135 }
136 impl IsDefault for $ty {
137 fn is_default(&self) -> bool {
138 <$ty as Default>::default() == *self
139 }
140 }
141 }
142}
143
144const _: () = if !MacroKinds::DERIVE_ATTR_BANG.is_all() {
::core::panicking::panic("assertion failed: MacroKinds::DERIVE_ATTR_BANG.is_all()")
}assert!(MacroKinds::DERIVE_ATTR_BANG.is_all());
146
147impl FixedSizeEncoding for Option<DefKind> {
type ByteArray = [u8; 1];
#[inline]
fn from_bytes(b: &[u8; 1]) -> Self {
use DefKind::*;
if b[0] == 0 { return None; }
match b[0] - 1 {
0 => Some(Mod),
1 => Some(Struct),
2 => Some(Union),
3 => Some(Enum),
4 => Some(Variant),
5 => Some(Trait),
6 => Some(TyAlias),
7 => Some(ForeignTy),
8 => Some(TraitAlias),
9 => Some(AssocTy),
10 => Some(TyParam),
11 => Some(Fn),
12 => Some(Const),
13 => Some(ConstParam),
14 => Some(AssocFn),
15 => Some(AssocConst),
16 => Some(ExternCrate),
17 => Some(Use),
18 => Some(ForeignMod),
19 => Some(AnonConst),
20 => Some(OpaqueTy),
21 => Some(Field),
22 => Some(LifetimeParam),
23 => Some(GlobalAsm),
24 => Some(Impl { of_trait: false }),
25 => Some(Impl { of_trait: true }),
26 => Some(Closure),
27 =>
Some(Static {
safety: hir::Safety::Unsafe,
mutability: ast::Mutability::Not,
nested: false,
}),
28 =>
Some(Static {
safety: hir::Safety::Safe,
mutability: ast::Mutability::Not,
nested: false,
}),
29 =>
Some(Static {
safety: hir::Safety::Unsafe,
mutability: ast::Mutability::Mut,
nested: false,
}),
30 =>
Some(Static {
safety: hir::Safety::Safe,
mutability: ast::Mutability::Mut,
nested: false,
}),
31 =>
Some(Static {
safety: hir::Safety::Unsafe,
mutability: ast::Mutability::Not,
nested: true,
}),
32 =>
Some(Static {
safety: hir::Safety::Safe,
mutability: ast::Mutability::Not,
nested: true,
}),
33 =>
Some(Static {
safety: hir::Safety::Unsafe,
mutability: ast::Mutability::Mut,
nested: true,
}),
34 =>
Some(Static {
safety: hir::Safety::Safe,
mutability: ast::Mutability::Mut,
nested: true,
}),
35 => Some(Ctor(CtorOf::Struct, CtorKind::Fn)),
36 => Some(Ctor(CtorOf::Struct, CtorKind::Const)),
37 => Some(Ctor(CtorOf::Variant, CtorKind::Fn)),
38 => Some(Ctor(CtorOf::Variant, CtorKind::Const)),
39 => Some(Macro(MacroKinds::BANG)),
40 => Some(Macro(MacroKinds::ATTR)),
41 => Some(Macro(MacroKinds::DERIVE)),
42 => Some(Macro(MacroKinds::ATTR_BANG)),
43 => Some(Macro(MacroKinds::DERIVE_ATTR)),
44 => Some(Macro(MacroKinds::DERIVE_BANG)),
45 => Some(Macro(MacroKinds::DERIVE_ATTR_BANG)),
46 => Some(SyntheticCoroutineBody),
47 => Some(TestBinderConstraints),
_ => {
::core::panicking::panic_fmt(format_args!("Unexpected {0} code: {1:?}",
"DefKind", b[0]));
}
}
}
#[inline]
fn write_to_bytes(self, b: &mut [u8; 1]) {
use DefKind::*;
b[0] =
match self {
None => ::core::panicking::unreachable_default_msg(),
Some(Mod) => 1 + 0,
Some(Struct) => 1 + 1,
Some(Union) => 1 + 2,
Some(Enum) => 1 + 3,
Some(Variant) => 1 + 4,
Some(Trait) => 1 + 5,
Some(TyAlias) => 1 + 6,
Some(ForeignTy) => 1 + 7,
Some(TraitAlias) => 1 + 8,
Some(AssocTy) => 1 + 9,
Some(TyParam) => 1 + 10,
Some(Fn) => 1 + 11,
Some(Const) => 1 + 12,
Some(ConstParam) => 1 + 13,
Some(AssocFn) => 1 + 14,
Some(AssocConst) => 1 + 15,
Some(ExternCrate) => 1 + 16,
Some(Use) => 1 + 17,
Some(ForeignMod) => 1 + 18,
Some(AnonConst) => 1 + 19,
Some(OpaqueTy) => 1 + 20,
Some(Field) => 1 + 21,
Some(LifetimeParam) => 1 + 22,
Some(GlobalAsm) => 1 + 23,
Some(Impl { of_trait: false }) => 1 + 24,
Some(Impl { of_trait: true }) => 1 + 25,
Some(Closure) => 1 + 26,
Some(Static {
safety: hir::Safety::Unsafe,
mutability: ast::Mutability::Not,
nested: false }) => 1 + 27,
Some(Static {
safety: hir::Safety::Safe,
mutability: ast::Mutability::Not,
nested: false }) => 1 + 28,
Some(Static {
safety: hir::Safety::Unsafe,
mutability: ast::Mutability::Mut,
nested: false }) => 1 + 29,
Some(Static {
safety: hir::Safety::Safe,
mutability: ast::Mutability::Mut,
nested: false }) => 1 + 30,
Some(Static {
safety: hir::Safety::Unsafe,
mutability: ast::Mutability::Not,
nested: true }) => 1 + 31,
Some(Static {
safety: hir::Safety::Safe,
mutability: ast::Mutability::Not,
nested: true }) => 1 + 32,
Some(Static {
safety: hir::Safety::Unsafe,
mutability: ast::Mutability::Mut,
nested: true }) => 1 + 33,
Some(Static {
safety: hir::Safety::Safe,
mutability: ast::Mutability::Mut,
nested: true }) => 1 + 34,
Some(Ctor(CtorOf::Struct, CtorKind::Fn)) => 1 + 35,
Some(Ctor(CtorOf::Struct, CtorKind::Const)) => 1 + 36,
Some(Ctor(CtorOf::Variant, CtorKind::Fn)) => 1 + 37,
Some(Ctor(CtorOf::Variant, CtorKind::Const)) => 1 + 38,
Some(Macro(MacroKinds::BANG)) => 1 + 39,
Some(Macro(MacroKinds::ATTR)) => 1 + 40,
Some(Macro(MacroKinds::DERIVE)) => 1 + 41,
Some(Macro(MacroKinds::ATTR_BANG)) => 1 + 42,
Some(Macro(MacroKinds::DERIVE_ATTR)) => 1 + 43,
Some(Macro(MacroKinds::DERIVE_BANG)) => 1 + 44,
Some(Macro(MacroKinds::DERIVE_ATTR_BANG)) => 1 + 45,
Some(SyntheticCoroutineBody) => 1 + 46,
Some(TestBinderConstraints) => 1 + 47,
Some(Macro(_)) =>
::core::panicking::unreachable_default_msg(),
}
}
}fixed_size_enum! {
148 DefKind {
149 ( Mod )
150 ( Struct )
151 ( Union )
152 ( Enum )
153 ( Variant )
154 ( Trait )
155 ( TyAlias )
156 ( ForeignTy )
157 ( TraitAlias )
158 ( AssocTy )
159 ( TyParam )
160 ( Fn )
161 ( Const )
162 ( ConstParam )
163 ( AssocFn )
164 ( AssocConst )
165 ( ExternCrate )
166 ( Use )
167 ( ForeignMod )
168 ( AnonConst )
169 ( OpaqueTy )
170 ( Field )
171 ( LifetimeParam )
172 ( GlobalAsm )
173 ( Impl { of_trait: false } )
174 ( Impl { of_trait: true } )
175 ( Closure )
176 ( Static { safety: hir::Safety::Unsafe, mutability: ast::Mutability::Not, nested: false } )
177 ( Static { safety: hir::Safety::Safe, mutability: ast::Mutability::Not, nested: false } )
178 ( Static { safety: hir::Safety::Unsafe, mutability: ast::Mutability::Mut, nested: false } )
179 ( Static { safety: hir::Safety::Safe, mutability: ast::Mutability::Mut, nested: false } )
180 ( Static { safety: hir::Safety::Unsafe, mutability: ast::Mutability::Not, nested: true } )
181 ( Static { safety: hir::Safety::Safe, mutability: ast::Mutability::Not, nested: true } )
182 ( Static { safety: hir::Safety::Unsafe, mutability: ast::Mutability::Mut, nested: true } )
183 ( Static { safety: hir::Safety::Safe, mutability: ast::Mutability::Mut, nested: true } )
184 ( Ctor(CtorOf::Struct, CtorKind::Fn) )
185 ( Ctor(CtorOf::Struct, CtorKind::Const) )
186 ( Ctor(CtorOf::Variant, CtorKind::Fn) )
187 ( Ctor(CtorOf::Variant, CtorKind::Const) )
188 ( Macro(MacroKinds::BANG) )
189 ( Macro(MacroKinds::ATTR) )
190 ( Macro(MacroKinds::DERIVE) )
191 ( Macro(MacroKinds::ATTR_BANG) )
192 ( Macro(MacroKinds::DERIVE_ATTR) )
193 ( Macro(MacroKinds::DERIVE_BANG) )
194 ( Macro(MacroKinds::DERIVE_ATTR_BANG) )
195 ( SyntheticCoroutineBody )
196 ( TestBinderConstraints )
197 } unreachable {
198 ( Macro(_) )
199 }
200}
201
202impl FixedSizeEncoding for hir::Defaultness {
type ByteArray = [u8; 1];
#[inline]
fn from_bytes(b: &[u8; 1]) -> Self {
use hir::Defaultness::*;
let val =
match b[0] {
0 => Final,
1 => Default { has_value: false },
2 => Default { has_value: true },
_ => {
::core::panicking::panic_fmt(format_args!("Unexpected {0} code: {1:?}",
"hir::Defaultness", b[0]));
}
};
if true {
{
match (&(b[0] != 0), &IsDefault::is_default(&val)) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
val
}
#[inline]
fn write_to_bytes(self, b: &mut [u8; 1]) {
if true {
if !!IsDefault::is_default(&self) {
::core::panicking::panic("assertion failed: !IsDefault::is_default(&self)")
};
};
use hir::Defaultness::*;
b[0] =
match self {
Final => 0,
Default { has_value: false } => 1,
Default { has_value: true } => 2,
};
if true {
{
match (&b[0], &0) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
}
}
impl IsDefault for hir::Defaultness {
fn is_default(&self) -> bool {
<hir::Defaultness as Default>::default() == *self
}
}defaulted_enum! {
203 hir::Defaultness {
204 ( Final )
205 ( Default { has_value: false } )
206 ( Default { has_value: true } )
207 }
208}
209
210impl FixedSizeEncoding for ty::Asyncness {
type ByteArray = [u8; 1];
#[inline]
fn from_bytes(b: &[u8; 1]) -> Self {
use ty::Asyncness::*;
let val =
match b[0] {
0 => No,
1 => Yes,
_ => {
::core::panicking::panic_fmt(format_args!("Unexpected {0} code: {1:?}",
"ty::Asyncness", b[0]));
}
};
if true {
{
match (&(b[0] != 0), &IsDefault::is_default(&val)) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
val
}
#[inline]
fn write_to_bytes(self, b: &mut [u8; 1]) {
if true {
if !!IsDefault::is_default(&self) {
::core::panicking::panic("assertion failed: !IsDefault::is_default(&self)")
};
};
use ty::Asyncness::*;
b[0] = match self { No => 0, Yes => 1, };
if true {
{
match (&b[0], &0) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
}
}
impl IsDefault for ty::Asyncness {
fn is_default(&self) -> bool {
<ty::Asyncness as Default>::default() == *self
}
}defaulted_enum! {
211 ty::Asyncness {
212 ( No )
213 ( Yes )
214 }
215}
216
217impl FixedSizeEncoding for hir::Constness {
type ByteArray = [u8; 1];
#[inline]
fn from_bytes(b: &[u8; 1]) -> Self {
use hir::Constness::*;
let val =
match b[0] {
0 => Const { always: false },
1 => NotConst,
2 => Const { always: true },
_ => {
::core::panicking::panic_fmt(format_args!("Unexpected {0} code: {1:?}",
"hir::Constness", b[0]));
}
};
if true {
{
match (&(b[0] != 0), &IsDefault::is_default(&val)) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
val
}
#[inline]
fn write_to_bytes(self, b: &mut [u8; 1]) {
if true {
if !!IsDefault::is_default(&self) {
::core::panicking::panic("assertion failed: !IsDefault::is_default(&self)")
};
};
use hir::Constness::*;
b[0] =
match self {
Const { always: false } => 0,
NotConst => 1,
Const { always: true } => 2,
};
if true {
{
match (&b[0], &0) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
}
}
impl IsDefault for hir::Constness {
fn is_default(&self) -> bool {
<hir::Constness as Default>::default() == *self
}
}defaulted_enum! {
218 hir::Constness {
219 ( Const { always: false } )
220 ( NotConst )
221 ( Const { always: true } )
222 }
223}
224
225impl FixedSizeEncoding for hir::Safety {
type ByteArray = [u8; 1];
#[inline]
fn from_bytes(b: &[u8; 1]) -> Self {
use hir::Safety::*;
let val =
match b[0] {
0 => Unsafe,
1 => Safe,
_ => {
::core::panicking::panic_fmt(format_args!("Unexpected {0} code: {1:?}",
"hir::Safety", b[0]));
}
};
if true {
{
match (&(b[0] != 0), &IsDefault::is_default(&val)) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
val
}
#[inline]
fn write_to_bytes(self, b: &mut [u8; 1]) {
if true {
if !!IsDefault::is_default(&self) {
::core::panicking::panic("assertion failed: !IsDefault::is_default(&self)")
};
};
use hir::Safety::*;
b[0] = match self { Unsafe => 0, Safe => 1, };
if true {
{
match (&b[0], &0) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
}
}
impl IsDefault for hir::Safety {
fn is_default(&self) -> bool {
<hir::Safety as Default>::default() == *self
}
}defaulted_enum! {
226 hir::Safety {
227 ( Unsafe )
228 ( Safe )
229 }
230}
231
232impl FixedSizeEncoding for Option<hir::CoroutineKind> {
type ByteArray = [u8; 1];
#[inline]
fn from_bytes(b: &[u8; 1]) -> Self {
use hir::CoroutineKind::*;
if b[0] == 0 { return None; }
match b[0] - 1 {
0 => Some(Coroutine(hir::Movability::Movable)),
1 => Some(Coroutine(hir::Movability::Static)),
2 =>
Some(Desugared(hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Block)),
3 =>
Some(Desugared(hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Fn)),
4 =>
Some(Desugared(hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Closure)),
5 =>
Some(Desugared(hir::CoroutineDesugaring::Async,
hir::CoroutineSource::Block)),
6 =>
Some(Desugared(hir::CoroutineDesugaring::Async,
hir::CoroutineSource::Fn)),
7 =>
Some(Desugared(hir::CoroutineDesugaring::Async,
hir::CoroutineSource::Closure)),
8 =>
Some(Desugared(hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Block)),
9 =>
Some(Desugared(hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Fn)),
10 =>
Some(Desugared(hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Closure)),
_ => {
::core::panicking::panic_fmt(format_args!("Unexpected {0} code: {1:?}",
"hir::CoroutineKind", b[0]));
}
}
}
#[inline]
fn write_to_bytes(self, b: &mut [u8; 1]) {
use hir::CoroutineKind::*;
b[0] =
match self {
None => ::core::panicking::unreachable_default_msg(),
Some(Coroutine(hir::Movability::Movable)) => 1 + 0,
Some(Coroutine(hir::Movability::Static)) => 1 + 1,
Some(Desugared(hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Block)) => 1 + 2,
Some(Desugared(hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Fn)) => 1 + 3,
Some(Desugared(hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Closure)) => 1 + 4,
Some(Desugared(hir::CoroutineDesugaring::Async,
hir::CoroutineSource::Block)) => 1 + 5,
Some(Desugared(hir::CoroutineDesugaring::Async,
hir::CoroutineSource::Fn)) => 1 + 6,
Some(Desugared(hir::CoroutineDesugaring::Async,
hir::CoroutineSource::Closure)) => 1 + 7,
Some(Desugared(hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Block)) => 1 + 8,
Some(Desugared(hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Fn)) => 1 + 9,
Some(Desugared(hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Closure)) => 1 + 10,
}
}
}fixed_size_enum! {
233 hir::CoroutineKind {
234 ( Coroutine(hir::Movability::Movable) )
235 ( Coroutine(hir::Movability::Static) )
236 ( Desugared(hir::CoroutineDesugaring::Gen, hir::CoroutineSource::Block) )
237 ( Desugared(hir::CoroutineDesugaring::Gen, hir::CoroutineSource::Fn) )
238 ( Desugared(hir::CoroutineDesugaring::Gen, hir::CoroutineSource::Closure) )
239 ( Desugared(hir::CoroutineDesugaring::Async, hir::CoroutineSource::Block) )
240 ( Desugared(hir::CoroutineDesugaring::Async, hir::CoroutineSource::Fn) )
241 ( Desugared(hir::CoroutineDesugaring::Async, hir::CoroutineSource::Closure) )
242 ( Desugared(hir::CoroutineDesugaring::AsyncGen, hir::CoroutineSource::Block) )
243 ( Desugared(hir::CoroutineDesugaring::AsyncGen, hir::CoroutineSource::Fn) )
244 ( Desugared(hir::CoroutineDesugaring::AsyncGen, hir::CoroutineSource::Closure) )
245 }
246}
247
248impl FixedSizeEncoding for Option<MacroKind> {
type ByteArray = [u8; 1];
#[inline]
fn from_bytes(b: &[u8; 1]) -> Self {
use MacroKind::*;
if b[0] == 0 { return None; }
match b[0] - 1 {
0 => Some(Attr),
1 => Some(Bang),
2 => Some(Derive),
_ => {
::core::panicking::panic_fmt(format_args!("Unexpected {0} code: {1:?}",
"MacroKind", b[0]));
}
}
}
#[inline]
fn write_to_bytes(self, b: &mut [u8; 1]) {
use MacroKind::*;
b[0] =
match self {
None => ::core::panicking::unreachable_default_msg(),
Some(Attr) => 1 + 0,
Some(Bang) => 1 + 1,
Some(Derive) => 1 + 2,
}
}
}fixed_size_enum! {
249 MacroKind {
250 ( Attr )
251 ( Bang )
252 ( Derive )
253 }
254}
255
256impl FixedSizeEncoding for Option<RemappedRawDefId> {
258 type ByteArray = [u8; 8];
259
260 #[inline]
261 fn from_bytes(encoded: &[u8; 8]) -> Self {
262 let (index, krate) = decode_interleaved(encoded);
263 let krate = u32::from_le_bytes(krate);
264 if krate == 0 {
265 return None;
266 }
267 let index = u32::from_le_bytes(index);
268
269 Some(RemappedRawDefId::new_unchecked(krate - 1, index))
270 }
271
272 #[inline]
273 fn write_to_bytes(self, dest: &mut [u8; 8]) {
274 match self {
275 None => ::core::panicking::unreachable_default_msg()unreachable!(),
276 Some(id) => {
277 let (krate, index) = id.decompose();
278 if true {
if !(krate < u32::MAX) {
::core::panicking::panic("assertion failed: krate < u32::MAX")
};
};debug_assert!(krate < u32::MAX);
279 let krate = (krate + 1).to_le_bytes();
281 let index = index.to_le_bytes();
282
283 encode_interleaved(index, krate, dest);
286 }
287 }
288 }
289}
290
291impl FixedSizeEncoding for AttrFlags {
292 type ByteArray = [u8; 1];
293
294 #[inline]
295 fn from_bytes(b: &[u8; 1]) -> Self {
296 AttrFlags::from_bits_truncate(b[0])
297 }
298
299 #[inline]
300 fn write_to_bytes(self, b: &mut [u8; 1]) {
301 if true {
if !!self.is_default() {
::core::panicking::panic("assertion failed: !self.is_default()")
};
};debug_assert!(!self.is_default());
302 b[0] = self.bits();
303 }
304}
305
306impl FixedSizeEncoding for bool {
307 type ByteArray = [u8; 1];
308
309 #[inline]
310 fn from_bytes(b: &[u8; 1]) -> Self {
311 b[0] != 0
312 }
313
314 #[inline]
315 fn write_to_bytes(self, b: &mut [u8; 1]) {
316 if true {
if !!self.is_default() {
::core::panicking::panic("assertion failed: !self.is_default()")
};
};debug_assert!(!self.is_default());
317 b[0] = self as u8
318 }
319}
320
321impl<T> FixedSizeEncoding for Option<LazyValue<T>> {
325 type ByteArray = [u8; 8];
326
327 #[inline]
328 fn from_bytes(b: &[u8; 8]) -> Self {
329 let position = NonZero::new(u64::from_bytes(b) as usize)?;
330 Some(LazyValue::from_position(position))
331 }
332
333 #[inline]
334 fn write_to_bytes(self, b: &mut [u8; 8]) {
335 match self {
336 None => ::core::panicking::unreachable_default_msg()unreachable!(),
337 Some(lazy) => {
338 let position = lazy.position.get();
339 let position: u64 = position.try_into().unwrap();
340 position.write_to_bytes(b)
341 }
342 }
343 }
344}
345
346impl<T> LazyArray<T> {
347 #[inline]
348 fn write_to_bytes_impl(self, dest: &mut [u8; 16]) {
349 let position = (self.position.get() as u64).to_le_bytes();
350 let len = (self.num_elems as u64).to_le_bytes();
351
352 encode_interleaved(position, len, dest)
353 }
354
355 fn from_bytes_impl(position: &[u8; 8], meta: &[u8; 8]) -> Option<LazyArray<T>> {
356 let position = NonZero::new(u64::from_bytes(position) as usize)?;
357 let len = u64::from_bytes(meta) as usize;
358 Some(LazyArray::from_position_and_num_elems(position, len))
359 }
360}
361
362#[inline]
365fn decode_interleaved<const N: usize, const M: usize>(encoded: &[u8; N]) -> ([u8; M], [u8; M]) {
366 {
match (&(M * 2), &N) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(M * 2, N);
367 let mut first = [0u8; M];
368 let mut second = [0u8; M];
369 for i in 0..M {
370 first[i] = encoded[2 * i];
371 second[i] = encoded[2 * i + 1];
372 }
373 (first, second)
374}
375
376#[inline]
384fn encode_interleaved<const N: usize, const M: usize>(a: [u8; M], b: [u8; M], dest: &mut [u8; N]) {
385 {
match (&(M * 2), &N) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(M * 2, N);
386 for i in 0..M {
387 dest[2 * i] = a[i];
388 dest[2 * i + 1] = b[i];
389 }
390}
391
392impl<T> FixedSizeEncoding for LazyArray<T> {
393 type ByteArray = [u8; 16];
394
395 #[inline]
396 fn from_bytes(b: &[u8; 16]) -> Self {
397 let (position, meta) = decode_interleaved(b);
398
399 if meta == [0; 8] {
400 return Default::default();
401 }
402 LazyArray::from_bytes_impl(&position, &meta).unwrap()
403 }
404
405 #[inline]
406 fn write_to_bytes(self, b: &mut [u8; 16]) {
407 if !!self.is_default() {
::core::panicking::panic("assertion failed: !self.is_default()")
};assert!(!self.is_default());
408 self.write_to_bytes_impl(b)
409 }
410}
411
412impl<T> FixedSizeEncoding for Option<LazyArray<T>> {
413 type ByteArray = [u8; 16];
414
415 #[inline]
416 fn from_bytes(b: &[u8; 16]) -> Self {
417 let (position, meta) = decode_interleaved(b);
418
419 LazyArray::from_bytes_impl(&position, &meta)
420 }
421
422 #[inline]
423 fn write_to_bytes(self, b: &mut [u8; 16]) {
424 match self {
425 None => ::core::panicking::unreachable_default_msg()unreachable!(),
426 Some(lazy) => lazy.write_to_bytes_impl(b),
427 }
428 }
429}
430
431pub(super) struct TableBuilder<IdxEncode: Idx, IdxDecode: Idx, T: FixedSizeEncoding> {
433 width: usize,
434 blocks: IndexVec<IdxEncode, T::ByteArray>,
435 _marker: PhantomData<(IdxDecode, T)>,
436}
437
438impl<Ie: Idx, Id: Idx, T: FixedSizeEncoding> Default for TableBuilder<Ie, Id, T> {
439 fn default() -> Self {
440 TableBuilder { width: 0, blocks: Default::default(), _marker: PhantomData }
441 }
442}
443
444impl<Ie: Idx, Id: Idx, const N: usize, T> TableBuilder<Ie, Id, Option<T>>
445where
446 Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>,
447{
448 pub(crate) fn set_some(&mut self, i: Ie, value: T) {
449 self.set(i, Some(value))
450 }
451}
452
453impl<Ie: Idx, Id: Idx, const N: usize, T: FixedSizeEncoding<ByteArray = [u8; N]>>
454 TableBuilder<Ie, Id, T>
455{
456 pub(crate) fn set(&mut self, i: Ie, value: T) {
462 #[cfg(debug_assertions)]
463 {
464 if true {
if !T::from_bytes(&[0; N]).is_default() {
{
::core::panicking::panic_fmt(format_args!("expected all-zeroes to decode to the default value, as per the invariant of FixedSizeEncoding"));
}
};
};debug_assert!(
465 T::from_bytes(&[0; N]).is_default(),
466 "expected all-zeroes to decode to the default value, as per the invariant of FixedSizeEncoding"
467 );
468 }
469 if !value.is_default() {
470 let block = self.blocks.ensure_contains_elem(i, || [0; N]);
476 value.write_to_bytes(block);
477 if self.width != N {
478 let width = N - trailing_zeros(block);
479 self.width = self.width.max(width);
480 }
481 }
482 }
483
484 pub(crate) fn encode(&self, buf: &mut FileEncoder<'_>) -> LazyTable<Ie, Id, T> {
485 let pos = buf.position();
486
487 let width = self.width;
488 for block in &self.blocks {
489 buf.write_with(|dest| {
490 *dest = *block;
491 width
492 });
493 }
494
495 LazyTable::from_position_and_encoded_size(
496 NonZero::new(pos).unwrap(),
497 width,
498 self.blocks.len(),
499 )
500 }
501}
502
503fn trailing_zeros(x: &[u8]) -> usize {
504 x.iter().rev().take_while(|b| **b == 0).count()
505}
506
507impl<
508 Ie: Idx,
509 Id: Idx,
510 const N: usize,
511 T: FixedSizeEncoding<ByteArray = [u8; N]> + ParameterizedOverTcx,
512> LazyTable<Ie, Id, T>
513where
514 for<'tcx> T::Value<'tcx>: FixedSizeEncoding<ByteArray = [u8; N]>,
515{
516 pub(super) fn get<'a, 'tcx, M: MetaBlob<'a>>(&self, metadata: M, i: Id) -> T::Value<'tcx> {
518 if i.index() >= self.len {
520 return Default::default();
521 }
522
523 let width = self.width;
524 let start = self.position.get() + (width * i.index());
525 let end = start + width;
526 let bytes = &metadata.blob()[start..end];
527
528 if let Ok(fixed) = bytes.try_into() {
529 FixedSizeEncoding::from_bytes(fixed)
530 } else {
531 let mut fixed = [0u8; N];
532 fixed[..width].copy_from_slice(bytes);
533 FixedSizeEncoding::from_bytes(&fixed)
534 }
535 }
536
537 pub(super) fn size(&self) -> usize {
539 self.len
540 }
541}