Split Metadata trait into MetaBlob and MetaDecoder.

`Metadata` has two methods, `blob` and `decoder`, which are not used
together. Splitting the trait in two will allow some cleanups in
subsequent commits.
This commit is contained in:
Nicholas Nethercote
2026-04-21 20:56:14 +10:00
parent c28e303778
commit 2f864a9d03
2 changed files with 20 additions and 16 deletions
+18 -14
View File
@@ -221,7 +221,7 @@ fn get_lazy_state(&self) -> LazyState {
/// This is the decode context used when crate metadata was already read.
/// Decoding of some types, like `Span` require some information to already been read.
/// Can be constructed from a [`TyCtxt`] and [`CrateMetadataRef`] (see the [`Metadata`] trait)
/// Can be constructed from a [`TyCtxt`] and [`CrateMetadataRef`] (see the [`MetaDecoder`] trait)
pub(super) struct MetadataDecodeContext<'a, 'tcx> {
blob_decoder: BlobDecodeContext<'a>,
cdata: CrateMetadataRef<'a>,
@@ -255,19 +255,24 @@ fn deref(&self) -> &Self::Target {
}
}
pub(super) trait Metadata<'a>: Copy {
pub(super) trait MetaBlob<'a>: Copy {
fn blob(self) -> &'a MetadataBlob;
}
pub(super) trait MetaDecoder: Copy {
type Context: BlobDecoder + LazyDecoder;
fn blob(self) -> &'a MetadataBlob;
fn decoder(self, pos: usize) -> Self::Context;
}
impl<'a> Metadata<'a> for &'a MetadataBlob {
type Context = BlobDecodeContext<'a>;
impl<'a> MetaBlob<'a> for &'a MetadataBlob {
fn blob(self) -> &'a MetadataBlob {
self
}
}
impl<'a> MetaDecoder for &'a MetadataBlob {
type Context = BlobDecodeContext<'a>;
fn decoder(self, pos: usize) -> Self::Context {
BlobDecodeContext {
@@ -285,12 +290,14 @@ fn decoder(self, pos: usize) -> Self::Context {
}
}
impl<'a, 'tcx> Metadata<'a> for (CrateMetadataRef<'a>, TyCtxt<'tcx>) {
type Context = MetadataDecodeContext<'a, 'tcx>;
impl<'a, 'tcx> MetaBlob<'a> for (CrateMetadataRef<'a>, TyCtxt<'tcx>) {
fn blob(self) -> &'a MetadataBlob {
&self.0.cdata.blob
}
}
impl<'a, 'tcx> MetaDecoder for (CrateMetadataRef<'a>, TyCtxt<'tcx>) {
type Context = MetadataDecodeContext<'a, 'tcx>;
fn decoder(self, pos: usize) -> MetadataDecodeContext<'a, 'tcx> {
MetadataDecodeContext {
@@ -304,7 +311,7 @@ fn decoder(self, pos: usize) -> MetadataDecodeContext<'a, 'tcx> {
impl<T: ParameterizedOverTcx> LazyValue<T> {
#[inline]
fn decode<'a, 'tcx, M: Metadata<'a>>(self, metadata: M) -> T::Value<'tcx>
fn decode<'tcx, M: MetaDecoder>(self, metadata: M) -> T::Value<'tcx>
where
T::Value<'tcx>: Decodable<M::Context>,
{
@@ -344,10 +351,7 @@ unsafe impl<D: Decoder, T: Decodable<D>> TrustedLen for DecodeIterator<T, D> {}
impl<T: ParameterizedOverTcx> LazyArray<T> {
#[inline]
fn decode<'a, 'tcx, M: Metadata<'a>>(
self,
metadata: M,
) -> DecodeIterator<T::Value<'tcx>, M::Context>
fn decode<'tcx, M: MetaDecoder>(self, metadata: M) -> DecodeIterator<T::Value<'tcx>, M::Context>
where
T::Value<'tcx>: Decodable<M::Context>,
{
+2 -2
View File
@@ -1,7 +1,7 @@
use rustc_hir::def::CtorOf;
use rustc_index::Idx;
use crate::rmeta::decoder::Metadata;
use crate::rmeta::decoder::MetaBlob;
use crate::rmeta::*;
pub(super) trait IsDefault: Default {
@@ -515,7 +515,7 @@ fn trailing_zeros(x: &[u8]) -> usize {
for<'tcx> T::Value<'tcx>: FixedSizeEncoding<ByteArray = [u8; N]>,
{
/// Given the metadata, extract out the value at a particular index (if any).
pub(super) fn get<'a, 'tcx, M: Metadata<'a>>(&self, metadata: M, i: I) -> T::Value<'tcx> {
pub(super) fn get<'a, 'tcx, M: MetaBlob<'a>>(&self, metadata: M, i: I) -> T::Value<'tcx> {
// Access past the end of the table returns a Default
if i.index() >= self.len {
return Default::default();