Skip to content

Commit

Permalink
Move path printing to a pub(crate) function
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Mar 27, 2022
1 parent d545093 commit 390e0c1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/expr.rs
Expand Up @@ -3369,7 +3369,7 @@ pub(crate) mod printing {
impl ToTokens for ExprPath {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
private::print_path(tokens, &self.qself, &self.path);
path::printing::print_path(tokens, &self.qself, &self.path);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pat.rs
Expand Up @@ -827,7 +827,7 @@ mod printing {
impl ToTokens for PatPath {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(self.attrs.outer());
private::print_path(tokens, &self.qself, &self.path);
path::printing::print_path(tokens, &self.qself, &self.path);
}
}

Expand Down
62 changes: 30 additions & 32 deletions src/path.rs
Expand Up @@ -684,7 +684,7 @@ pub mod parsing {
}

#[cfg(feature = "printing")]
mod printing {
pub(crate) mod printing {
use super::*;
use crate::print::TokensOrDefault;
use proc_macro2::TokenStream;
Expand Down Expand Up @@ -835,39 +835,37 @@ mod printing {
}
}

impl private {
pub(crate) fn print_path(tokens: &mut TokenStream, qself: &Option<QSelf>, path: &Path) {
let qself = match qself {
Some(qself) => qself,
None => {
path.to_tokens(tokens);
return;
}
};
qself.lt_token.to_tokens(tokens);
qself.ty.to_tokens(tokens);

let pos = cmp::min(qself.position, path.segments.len());
let mut segments = path.segments.pairs();
if pos > 0 {
TokensOrDefault(&qself.as_token).to_tokens(tokens);
path.leading_colon.to_tokens(tokens);
for (i, segment) in segments.by_ref().take(pos).enumerate() {
if i + 1 == pos {
segment.value().to_tokens(tokens);
qself.gt_token.to_tokens(tokens);
segment.punct().to_tokens(tokens);
} else {
segment.to_tokens(tokens);
}
}
} else {
qself.gt_token.to_tokens(tokens);
path.leading_colon.to_tokens(tokens);
pub(crate) fn print_path(tokens: &mut TokenStream, qself: &Option<QSelf>, path: &Path) {
let qself = match qself {
Some(qself) => qself,
None => {
path.to_tokens(tokens);
return;
}
for segment in segments {
segment.to_tokens(tokens);
};
qself.lt_token.to_tokens(tokens);
qself.ty.to_tokens(tokens);

let pos = cmp::min(qself.position, path.segments.len());
let mut segments = path.segments.pairs();
if pos > 0 {
TokensOrDefault(&qself.as_token).to_tokens(tokens);
path.leading_colon.to_tokens(tokens);
for (i, segment) in segments.by_ref().take(pos).enumerate() {
if i + 1 == pos {
segment.value().to_tokens(tokens);
qself.gt_token.to_tokens(tokens);
segment.punct().to_tokens(tokens);
} else {
segment.to_tokens(tokens);
}
}
} else {
qself.gt_token.to_tokens(tokens);
path.leading_colon.to_tokens(tokens);
}
for segment in segments {
segment.to_tokens(tokens);
}
}
}
2 changes: 1 addition & 1 deletion src/ty.rs
Expand Up @@ -1163,7 +1163,7 @@ mod printing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
impl ToTokens for TypePath {
fn to_tokens(&self, tokens: &mut TokenStream) {
private::print_path(tokens, &self.qself, &self.path);
path::printing::print_path(tokens, &self.qself, &self.path);
}
}

Expand Down

0 comments on commit 390e0c1

Please sign in to comment.