rustc_metadata: Support encoding/decoding LazyArray
without an Option
This commit is contained in:
parent
8cc5aa561c
commit
eb5f2d3980
@ -654,7 +654,7 @@ fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
|
|||||||
impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyArray<T> {
|
impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyArray<T> {
|
||||||
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
|
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
|
||||||
let len = decoder.read_usize();
|
let len = decoder.read_usize();
|
||||||
if len == 0 { LazyArray::empty() } else { decoder.read_lazy_array(len) }
|
if len == 0 { LazyArray::default() } else { decoder.read_lazy_array(len) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -864,7 +864,7 @@ fn get_variant(self, kind: &DefKind, index: DefIndex, parent_did: DefId) -> ty::
|
|||||||
.tables
|
.tables
|
||||||
.children
|
.children
|
||||||
.get(self, index)
|
.get(self, index)
|
||||||
.unwrap_or_else(LazyArray::empty)
|
.unwrap_or_else(LazyArray::default)
|
||||||
.decode(self)
|
.decode(self)
|
||||||
.map(|index| ty::FieldDef {
|
.map(|index| ty::FieldDef {
|
||||||
did: self.local_def_id(index),
|
did: self.local_def_id(index),
|
||||||
@ -896,7 +896,7 @@ fn get_adt_def(self, item_id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::AdtDef<'tcx> {
|
|||||||
.tables
|
.tables
|
||||||
.children
|
.children
|
||||||
.get(self, item_id)
|
.get(self, item_id)
|
||||||
.unwrap_or_else(LazyArray::empty)
|
.unwrap_or_else(LazyArray::default)
|
||||||
.decode(self)
|
.decode(self)
|
||||||
.filter_map(|index| {
|
.filter_map(|index| {
|
||||||
let kind = self.def_kind(index);
|
let kind = self.def_kind(index);
|
||||||
@ -1045,7 +1045,7 @@ fn get_fn_has_self_parameter(self, id: DefIndex, sess: &'a Session) -> bool {
|
|||||||
.tables
|
.tables
|
||||||
.fn_arg_names
|
.fn_arg_names
|
||||||
.get(self, id)
|
.get(self, id)
|
||||||
.unwrap_or_else(LazyArray::empty)
|
.unwrap_or_else(LazyArray::default)
|
||||||
.decode((self, sess))
|
.decode((self, sess))
|
||||||
.nth(0)
|
.nth(0)
|
||||||
.map_or(false, |ident| ident.name == kw::SelfLower)
|
.map_or(false, |ident| ident.name == kw::SelfLower)
|
||||||
@ -1060,7 +1060,7 @@ fn get_associated_item_def_ids(
|
|||||||
.tables
|
.tables
|
||||||
.children
|
.children
|
||||||
.get(self, id)
|
.get(self, id)
|
||||||
.unwrap_or_else(LazyArray::empty)
|
.unwrap_or_else(LazyArray::default)
|
||||||
.decode((self, sess))
|
.decode((self, sess))
|
||||||
.map(move |child_index| self.local_def_id(child_index))
|
.map(move |child_index| self.local_def_id(child_index))
|
||||||
}
|
}
|
||||||
@ -1131,7 +1131,7 @@ fn get_struct_field_names(
|
|||||||
.tables
|
.tables
|
||||||
.children
|
.children
|
||||||
.get(self, id)
|
.get(self, id)
|
||||||
.unwrap_or_else(LazyArray::empty)
|
.unwrap_or_else(LazyArray::default)
|
||||||
.decode(self)
|
.decode(self)
|
||||||
.map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
|
.map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
|
||||||
}
|
}
|
||||||
@ -1144,7 +1144,7 @@ fn get_struct_field_visibilities(
|
|||||||
.tables
|
.tables
|
||||||
.children
|
.children
|
||||||
.get(self, id)
|
.get(self, id)
|
||||||
.unwrap_or_else(LazyArray::empty)
|
.unwrap_or_else(LazyArray::default)
|
||||||
.decode(self)
|
.decode(self)
|
||||||
.map(move |field_index| self.get_visibility(field_index))
|
.map(move |field_index| self.get_visibility(field_index))
|
||||||
}
|
}
|
||||||
@ -1159,7 +1159,7 @@ fn get_inherent_implementations_for_type(
|
|||||||
.tables
|
.tables
|
||||||
.inherent_impls
|
.inherent_impls
|
||||||
.get(self, id)
|
.get(self, id)
|
||||||
.unwrap_or_else(LazyArray::empty)
|
.unwrap_or_else(LazyArray::default)
|
||||||
.decode(self)
|
.decode(self)
|
||||||
.map(|index| self.local_def_id(index)),
|
.map(|index| self.local_def_id(index)),
|
||||||
)
|
)
|
||||||
@ -1174,7 +1174,7 @@ fn get_inherent_impls(self) -> impl Iterator<Item = (DefId, DefId)> + 'a {
|
|||||||
.tables
|
.tables
|
||||||
.inherent_impls
|
.inherent_impls
|
||||||
.get(self, ty_index)
|
.get(self, ty_index)
|
||||||
.unwrap_or_else(LazyArray::empty)
|
.unwrap_or_else(LazyArray::default)
|
||||||
.decode(self)
|
.decode(self)
|
||||||
.map(move |impl_index| (ty_def_id, self.local_def_id(impl_index)))
|
.map(move |impl_index| (ty_def_id, self.local_def_id(impl_index)))
|
||||||
})
|
})
|
||||||
|
@ -76,13 +76,13 @@ pub(super) struct EncodeContext<'a, 'tcx> {
|
|||||||
symbol_table: FxHashMap<Symbol, usize>,
|
symbol_table: FxHashMap<Symbol, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If the current crate is a proc-macro, returns early with `LazyArray::empty()`.
|
/// If the current crate is a proc-macro, returns early with `LazyArray::default()`.
|
||||||
/// This is useful for skipping the encoding of things that aren't needed
|
/// This is useful for skipping the encoding of things that aren't needed
|
||||||
/// for proc-macro crates.
|
/// for proc-macro crates.
|
||||||
macro_rules! empty_proc_macro {
|
macro_rules! empty_proc_macro {
|
||||||
($self:ident) => {
|
($self:ident) => {
|
||||||
if $self.is_proc_macro {
|
if $self.is_proc_macro {
|
||||||
return LazyArray::empty();
|
return LazyArray::default();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1961,7 +1961,7 @@ fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePrefere
|
|||||||
Linkage::Static => Some(LinkagePreference::RequireStatic),
|
Linkage::Static => Some(LinkagePreference::RequireStatic),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
LazyArray::empty()
|
LazyArray::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) {
|
fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) {
|
||||||
|
@ -115,14 +115,16 @@ impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
|
|||||||
type Value<'tcx> = LazyArray<T::Value<'tcx>>;
|
type Value<'tcx> = LazyArray<T::Value<'tcx>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Default for LazyArray<T> {
|
||||||
|
fn default() -> LazyArray<T> {
|
||||||
|
LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> LazyArray<T> {
|
impl<T> LazyArray<T> {
|
||||||
fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> {
|
fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> {
|
||||||
LazyArray { position, num_elems, _marker: PhantomData }
|
LazyArray { position, num_elems, _marker: PhantomData }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn empty() -> LazyArray<T> {
|
|
||||||
LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A list of lazily-decoded values, with the added capability of random access.
|
/// A list of lazily-decoded values, with the added capability of random access.
|
||||||
|
@ -38,6 +38,12 @@ fn is_default(&self) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> IsDefault for LazyArray<T> {
|
||||||
|
fn is_default(&self) -> bool {
|
||||||
|
self.num_elems == 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Helper trait, for encoding to, and decoding from, a fixed number of bytes.
|
/// Helper trait, for encoding to, and decoding from, a fixed number of bytes.
|
||||||
/// Used mainly for Lazy positions and lengths.
|
/// Used mainly for Lazy positions and lengths.
|
||||||
/// Unchecked invariant: `Self::default()` should encode as `[0; BYTE_LEN]`,
|
/// Unchecked invariant: `Self::default()` should encode as `[0; BYTE_LEN]`,
|
||||||
@ -286,32 +292,60 @@ impl<T> FixedSizeEncoding for Option<LazyValue<T>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> LazyArray<T> {
|
||||||
|
#[inline]
|
||||||
|
fn write_to_bytes_impl(self, b: &mut [u8; 8]) {
|
||||||
|
let ([position_bytes, meta_bytes],[])= b.as_chunks_mut::<4>() else { panic!() };
|
||||||
|
|
||||||
|
let position = self.position.get();
|
||||||
|
let position: u32 = position.try_into().unwrap();
|
||||||
|
position.write_to_bytes(position_bytes);
|
||||||
|
|
||||||
|
let len = self.num_elems;
|
||||||
|
let len: u32 = len.try_into().unwrap();
|
||||||
|
len.write_to_bytes(meta_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_bytes_impl(position_bytes: &[u8; 4], meta_bytes: &[u8; 4]) -> Option<LazyArray<T>> {
|
||||||
|
let position = NonZeroUsize::new(u32::from_bytes(position_bytes) as usize)?;
|
||||||
|
let len = u32::from_bytes(meta_bytes) as usize;
|
||||||
|
Some(LazyArray::from_position_and_num_elems(position, len))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> FixedSizeEncoding for LazyArray<T> {
|
||||||
|
type ByteArray = [u8; 8];
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn from_bytes(b: &[u8; 8]) -> Self {
|
||||||
|
let ([position_bytes, meta_bytes],[])= b.as_chunks::<4>() else { panic!() };
|
||||||
|
if *meta_bytes == [0; 4] {
|
||||||
|
return Default::default();
|
||||||
|
}
|
||||||
|
LazyArray::from_bytes_impl(position_bytes, meta_bytes).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn write_to_bytes(self, b: &mut [u8; 8]) {
|
||||||
|
assert!(!self.is_default());
|
||||||
|
self.write_to_bytes_impl(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> FixedSizeEncoding for Option<LazyArray<T>> {
|
impl<T> FixedSizeEncoding for Option<LazyArray<T>> {
|
||||||
type ByteArray = [u8; 8];
|
type ByteArray = [u8; 8];
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_bytes(b: &[u8; 8]) -> Self {
|
fn from_bytes(b: &[u8; 8]) -> Self {
|
||||||
let ([position_bytes, meta_bytes],[])= b.as_chunks::<4>() else { panic!() };
|
let ([position_bytes, meta_bytes],[])= b.as_chunks::<4>() else { panic!() };
|
||||||
let position = NonZeroUsize::new(u32::from_bytes(position_bytes) as usize)?;
|
LazyArray::from_bytes_impl(position_bytes, meta_bytes)
|
||||||
let len = u32::from_bytes(meta_bytes) as usize;
|
|
||||||
Some(LazyArray::from_position_and_num_elems(position, len))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn write_to_bytes(self, b: &mut [u8; 8]) {
|
fn write_to_bytes(self, b: &mut [u8; 8]) {
|
||||||
match self {
|
match self {
|
||||||
None => unreachable!(),
|
None => unreachable!(),
|
||||||
Some(lazy) => {
|
Some(lazy) => lazy.write_to_bytes_impl(b),
|
||||||
let ([position_bytes, meta_bytes],[])= b.as_chunks_mut::<4>() else { panic!() };
|
|
||||||
|
|
||||||
let position = lazy.position.get();
|
|
||||||
let position: u32 = position.try_into().unwrap();
|
|
||||||
position.write_to_bytes(position_bytes);
|
|
||||||
|
|
||||||
let len = lazy.num_elems;
|
|
||||||
let len: u32 = len.try_into().unwrap();
|
|
||||||
len.write_to_bytes(meta_bytes);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user