Pretty print mut restrictions

This commit is contained in:
CoCo-Japan-pan
2026-05-20 19:54:56 +09:00
parent c43fa7a8bb
commit a8a6aa8f59
3 changed files with 24 additions and 0 deletions
@@ -84,6 +84,10 @@ pub fn impl_restriction_to_string(r: &ast::ImplRestriction) -> String {
State::new().impl_restriction_to_string(r)
}
pub fn mut_restriction_to_string(r: &ast::MutRestriction) -> String {
State::new().mut_restriction_to_string(r)
}
pub fn meta_list_item_to_string(li: &ast::MetaItemInner) -> String {
State::new().meta_list_item_to_string(li)
}
@@ -1153,6 +1153,10 @@ fn impl_restriction_to_string(&self, r: &ast::ImplRestriction) -> String {
Self::to_string(|s| s.print_impl_restriction(r))
}
fn mut_restriction_to_string(&self, r: &ast::MutRestriction) -> String {
Self::to_string(|s| s.print_mut_restriction(r))
}
fn block_to_string(&self, blk: &ast::Block) -> String {
Self::to_string(|s| {
let (cb, ib) = s.head("");
@@ -511,6 +511,20 @@ pub(crate) fn print_impl_restriction(&mut self, impl_restriction: &ast::ImplRest
}
}
pub(crate) fn print_mut_restriction(&mut self, mut_restriction: &ast::MutRestriction) {
match &mut_restriction.kind {
ast::RestrictionKind::Restricted { path, shorthand, .. } => {
let path = Self::to_string(|s| s.print_path(path, false, 0));
if *shorthand {
self.word_nbsp(format!("mut({path})"))
} else {
self.word_nbsp(format!("mut(in {path})"))
}
}
ast::RestrictionKind::Unrestricted => {}
}
}
fn print_defaultness(&mut self, defaultness: ast::Defaultness) {
if let ast::Defaultness::Default(_) = defaultness {
self.word_nbsp("default");
@@ -537,6 +551,7 @@ fn print_struct(
s.maybe_print_comment(field.span.lo());
s.print_outer_attributes(&field.attrs);
s.print_visibility(&field.vis);
s.print_mut_restriction(&field.mut_restriction);
s.print_type(&field.ty)
});
self.pclose();
@@ -562,6 +577,7 @@ fn print_struct(
self.maybe_print_comment(field.span.lo());
self.print_outer_attributes(&field.attrs);
self.print_visibility(&field.vis);
self.print_mut_restriction(&field.mut_restriction);
self.print_ident(field.ident.unwrap());
self.word_nbsp(":");
self.print_type(&field.ty);