Remove IterDelimited
.
itertools has `with_position` which does the same thing.
This commit is contained in:
parent
3eadc6844b
commit
ce363bb6e6
@ -3471,6 +3471,7 @@ dependencies = [
|
|||||||
name = "rustc_ast_pretty"
|
name = "rustc_ast_pretty"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
"rustc_ast",
|
"rustc_ast",
|
||||||
"rustc_span",
|
"rustc_span",
|
||||||
"thin-vec",
|
"thin-vec",
|
||||||
|
@ -5,6 +5,7 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# tidy-alphabetical-start
|
# tidy-alphabetical-start
|
||||||
|
itertools = "0.11"
|
||||||
rustc_ast = { path = "../rustc_ast" }
|
rustc_ast = { path = "../rustc_ast" }
|
||||||
rustc_span = { path = "../rustc_span" }
|
rustc_span = { path = "../rustc_span" }
|
||||||
thin-vec = "0.2.12"
|
thin-vec = "0.2.12"
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
mod delimited;
|
|
||||||
mod expr;
|
mod expr;
|
||||||
mod item;
|
mod item;
|
||||||
|
|
||||||
@ -23,8 +22,6 @@ use rustc_span::{BytePos, FileName, Span, DUMMY_SP};
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use thin_vec::ThinVec;
|
use thin_vec::ThinVec;
|
||||||
|
|
||||||
pub use self::delimited::IterDelimited;
|
|
||||||
|
|
||||||
pub enum MacHeader<'a> {
|
pub enum MacHeader<'a> {
|
||||||
Path(&'a ast::Path),
|
Path(&'a ast::Path),
|
||||||
Keyword(&'static str),
|
Keyword(&'static str),
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
use crate::pp::Breaks::Inconsistent;
|
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::ptr::P;
|
||||||
use rustc_ast::token;
|
use rustc_ast::token;
|
||||||
use rustc_ast::util::literal::escape_byte_str_symbol;
|
use rustc_ast::util::literal::escape_byte_str_symbol;
|
||||||
@ -149,10 +149,12 @@ impl<'a> State<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.cbox(0);
|
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.maybe_print_comment(field.span.hi());
|
||||||
self.print_outer_attributes(&field.attrs);
|
self.print_outer_attributes(&field.attrs);
|
||||||
if field.is_first {
|
if is_first {
|
||||||
self.space_if_not_bol();
|
self.space_if_not_bol();
|
||||||
}
|
}
|
||||||
if !field.is_shorthand {
|
if !field.is_shorthand {
|
||||||
@ -160,7 +162,7 @@ impl<'a> State<'a> {
|
|||||||
self.word_nbsp(":");
|
self.word_nbsp(":");
|
||||||
}
|
}
|
||||||
self.print_expr(&field.expr);
|
self.print_expr(&field.expr);
|
||||||
if !field.is_last || has_rest {
|
if !is_last || has_rest {
|
||||||
self.word_space(",");
|
self.word_space(",");
|
||||||
} else {
|
} else {
|
||||||
self.trailing_comma_or_space();
|
self.trailing_comma_or_space();
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use crate::pp::Breaks::Inconsistent;
|
use crate::pp::Breaks::Inconsistent;
|
||||||
use crate::pprust::state::delimited::IterDelimited;
|
|
||||||
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
|
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
|
||||||
|
|
||||||
use ast::StaticItem;
|
use ast::StaticItem;
|
||||||
|
use itertools::{Itertools, Position};
|
||||||
use rustc_ast as ast;
|
use rustc_ast as ast;
|
||||||
use rustc_ast::GenericBound;
|
use rustc_ast::GenericBound;
|
||||||
use rustc_ast::ModKind;
|
use rustc_ast::ModKind;
|
||||||
@ -712,9 +712,10 @@ impl<'a> State<'a> {
|
|||||||
self.word("{");
|
self.word("{");
|
||||||
self.zerobreak();
|
self.zerobreak();
|
||||||
self.ibox(0);
|
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);
|
self.print_use_tree(&use_tree.0);
|
||||||
if !use_tree.is_last {
|
if !is_last {
|
||||||
self.word(",");
|
self.word(",");
|
||||||
if let ast::UseTreeKind::Nested(_) = use_tree.0.kind {
|
if let ast::UseTreeKind::Nested(_) = use_tree.0.kind {
|
||||||
self.hardbreak();
|
self.hardbreak();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user