Remove IterDelimited.

itertools has `with_position` which does the same thing.
This commit is contained in:
Nicholas Nethercote 2023-11-14 09:59:30 +11:00
parent 3eadc6844b
commit ce363bb6e6
6 changed files with 13 additions and 52 deletions

View File

@ -3471,6 +3471,7 @@ dependencies = [
name = "rustc_ast_pretty"
version = "0.0.0"
dependencies = [
"itertools",
"rustc_ast",
"rustc_span",
"thin-vec",

View File

@ -5,6 +5,7 @@ edition = "2021"
[dependencies]
# tidy-alphabetical-start
itertools = "0.11"
rustc_ast = { path = "../rustc_ast" }
rustc_span = { path = "../rustc_span" }
thin-vec = "0.2.12"

View File

@ -1,4 +1,3 @@
mod delimited;
mod expr;
mod item;
@ -23,8 +22,6 @@ use rustc_span::{BytePos, FileName, Span, DUMMY_SP};
use std::borrow::Cow;
use thin_vec::ThinVec;
pub use self::delimited::IterDelimited;
pub enum MacHeader<'a> {
Path(&'a ast::Path),
Keyword(&'static str),

View File

@ -1,41 +0,0 @@
use std::iter::Peekable;
use std::mem;
use std::ops::Deref;
pub struct Delimited<I: Iterator> {
is_first: bool,
iter: Peekable<I>,
}
pub trait IterDelimited: Iterator + Sized {
fn delimited(self) -> Delimited<Self> {
Delimited { is_first: true, iter: self.peekable() }
}
}
impl<I: Iterator> IterDelimited for I {}
pub struct IteratorItem<T> {
value: T,
pub is_first: bool,
pub is_last: bool,
}
impl<I: Iterator> Iterator for Delimited<I> {
type Item = IteratorItem<I::Item>;
fn next(&mut self) -> Option<Self::Item> {
let value = self.iter.next()?;
let is_first = mem::replace(&mut self.is_first, false);
let is_last = self.iter.peek().is_none();
Some(IteratorItem { value, is_first, is_last })
}
}
impl<T> Deref for IteratorItem<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}

View File

@ -1,6 +1,6 @@
use crate::pp::Breaks::Inconsistent;
use crate::pprust::state::{AnnNode, IterDelimited, PrintState, State, INDENT_UNIT};
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
use itertools::{Itertools, Position};
use rustc_ast::ptr::P;
use rustc_ast::token;
use rustc_ast::util::literal::escape_byte_str_symbol;
@ -149,10 +149,12 @@ impl<'a> State<'a> {
return;
}
self.cbox(0);
for field in fields.iter().delimited() {
for (pos, field) in fields.iter().with_position() {
let is_first = matches!(pos, Position::First | Position::Only);
let is_last = matches!(pos, Position::Last | Position::Only);
self.maybe_print_comment(field.span.hi());
self.print_outer_attributes(&field.attrs);
if field.is_first {
if is_first {
self.space_if_not_bol();
}
if !field.is_shorthand {
@ -160,7 +162,7 @@ impl<'a> State<'a> {
self.word_nbsp(":");
}
self.print_expr(&field.expr);
if !field.is_last || has_rest {
if !is_last || has_rest {
self.word_space(",");
} else {
self.trailing_comma_or_space();

View File

@ -1,8 +1,8 @@
use crate::pp::Breaks::Inconsistent;
use crate::pprust::state::delimited::IterDelimited;
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
use ast::StaticItem;
use itertools::{Itertools, Position};
use rustc_ast as ast;
use rustc_ast::GenericBound;
use rustc_ast::ModKind;
@ -712,9 +712,10 @@ impl<'a> State<'a> {
self.word("{");
self.zerobreak();
self.ibox(0);
for use_tree in items.iter().delimited() {
for (pos, use_tree) in items.iter().with_position() {
let is_last = matches!(pos, Position::Last | Position::Only);
self.print_use_tree(&use_tree.0);
if !use_tree.is_last {
if !is_last {
self.word(",");
if let ast::UseTreeKind::Nested(_) = use_tree.0.kind {
self.hardbreak();