Skip to content

Commit 2b5e592

Browse files
Fix iterator implementation, add some inlines
1 parent 14e5816 commit 2b5e592

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

compiler/rustc_metadata/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![feature(macro_metavar_expr)]
1111
#![feature(min_specialization)]
1212
#![feature(slice_as_chunks)]
13+
#![feature(trusted_len)]
1314
#![feature(try_blocks)]
1415
#![feature(never_type)]
1516
#![recursion_limit = "256"]

compiler/rustc_metadata/src/rmeta/decoder.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP};
3838

3939
use proc_macro::bridge::client::ProcMacro;
4040
use std::io;
41+
use std::iter::TrustedLen;
4142
use std::mem;
4243
use std::num::NonZeroUsize;
4344
use std::path::Path;
@@ -277,17 +278,25 @@ struct DecodeIterator<'a, 'tcx, T> {
277278
impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> Iterator for DecodeIterator<'a, 'tcx, T> {
278279
type Item = T;
279280

281+
#[inline(always)]
280282
fn next(&mut self) -> Option<Self::Item> {
281283
self.elem_counter.next().map(|_| T::decode(&mut self.dcx))
282284
}
285+
286+
#[inline(always)]
287+
fn size_hint(&self) -> (usize, Option<usize>) {
288+
self.elem_counter.size_hint()
289+
}
283290
}
284291

285292
impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> ExactSizeIterator
286293
for DecodeIterator<'a, 'tcx, T>
287294
{
288-
fn len(&self) -> usize {
289-
self.elem_counter.len()
290-
}
295+
}
296+
297+
unsafe impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> TrustedLen
298+
for DecodeIterator<'a, 'tcx, T>
299+
{
291300
}
292301

293302
impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable<DecodeContext<'a, 'tcx>>> LazyArray<T> {
@@ -321,6 +330,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
321330
self.cdata().map_encoded_cnum_to_current(cnum)
322331
}
323332

333+
#[inline]
324334
fn read_lazy_offset_then<T>(&mut self, f: impl Fn(NonZeroUsize) -> T) -> T {
325335
let distance = self.read_usize();
326336
let position = match self.lazy_state {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+6
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,28 @@ trait ProcessQueryValue<'tcx, T> {
3232
}
3333

3434
impl<T> ProcessQueryValue<'_, Option<T>> for Option<T> {
35+
#[inline(always)]
3536
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Option<T> {
3637
self
3738
}
3839
}
3940

4041
impl<T> ProcessQueryValue<'_, T> for Option<T> {
42+
#[inline(always)]
4143
fn process_decoded(self, _tcx: TyCtxt<'_>, err: impl Fn() -> !) -> T {
4244
if let Some(value) = self { value } else { err() }
4345
}
4446
}
4547

4648
impl<'tcx, T: ArenaAllocatable<'tcx>> ProcessQueryValue<'tcx, &'tcx T> for Option<T> {
49+
#[inline(always)]
4750
fn process_decoded(self, tcx: TyCtxt<'tcx>, err: impl Fn() -> !) -> &'tcx T {
4851
if let Some(value) = self { tcx.arena.alloc(value) } else { err() }
4952
}
5053
}
5154

5255
impl<T, E> ProcessQueryValue<'_, Result<Option<T>, E>> for Option<T> {
56+
#[inline(always)]
5357
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Result<Option<T>, E> {
5458
Ok(self)
5559
}
@@ -58,12 +62,14 @@ impl<T, E> ProcessQueryValue<'_, Result<Option<T>, E>> for Option<T> {
5862
impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>> ProcessQueryValue<'tcx, &'tcx [T]>
5963
for Option<DecodeIterator<'a, 'tcx, T>>
6064
{
65+
#[inline(always)]
6166
fn process_decoded(self, tcx: TyCtxt<'tcx>, _err: impl Fn() -> !) -> &'tcx [T] {
6267
if let Some(iter) = self { tcx.arena.alloc_from_iter(iter) } else { &[] }
6368
}
6469
}
6570

6671
impl ProcessQueryValue<'_, Option<DeprecationEntry>> for Option<Deprecation> {
72+
#[inline(always)]
6773
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Option<DeprecationEntry> {
6874
self.map(DeprecationEntry::external)
6975
}

0 commit comments

Comments
 (0)