"Fossies" - the Fresh Open Source Software Archive

Member "rustc-1.60.0-src/src/librustdoc/fold.rs" (4 Apr 2022, 4513 Bytes) of package /linux/misc/rustc-1.60.0-src.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "fold.rs": 1.57.0_vs_1.58.0.

    1 use crate::clean::*;
    2 
    3 crate fn strip_item(mut item: Item) -> Item {
    4     if !matches!(*item.kind, StrippedItem(..)) {
    5         item.kind = box StrippedItem(item.kind);
    6     }
    7     item
    8 }
    9 
   10 crate trait DocFolder: Sized {
   11     fn fold_item(&mut self, item: Item) -> Option<Item> {
   12         Some(self.fold_item_recur(item))
   13     }
   14 
   15     /// don't override!
   16     fn fold_inner_recur(&mut self, kind: ItemKind) -> ItemKind {
   17         match kind {
   18             StrippedItem(..) => unreachable!(),
   19             ModuleItem(i) => ModuleItem(self.fold_mod(i)),
   20             StructItem(mut i) => {
   21                 let num_fields = i.fields.len();
   22                 i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
   23                 if !i.fields_stripped {
   24                     i.fields_stripped =
   25                         num_fields != i.fields.len() || i.fields.iter().any(|f| f.is_stripped());
   26                 }
   27                 StructItem(i)
   28             }
   29             UnionItem(mut i) => {
   30                 let num_fields = i.fields.len();
   31                 i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
   32                 if !i.fields_stripped {
   33                     i.fields_stripped =
   34                         num_fields != i.fields.len() || i.fields.iter().any(|f| f.is_stripped());
   35                 }
   36                 UnionItem(i)
   37             }
   38             EnumItem(mut i) => {
   39                 let num_variants = i.variants.len();
   40                 i.variants = i.variants.into_iter().filter_map(|x| self.fold_item(x)).collect();
   41                 if !i.variants_stripped {
   42                     i.variants_stripped = num_variants != i.variants.len()
   43                         || i.variants.iter().any(|f| f.is_stripped());
   44                 }
   45                 EnumItem(i)
   46             }
   47             TraitItem(mut i) => {
   48                 i.items = i.items.into_iter().filter_map(|x| self.fold_item(x)).collect();
   49                 TraitItem(i)
   50             }
   51             ImplItem(mut i) => {
   52                 i.items = i.items.into_iter().filter_map(|x| self.fold_item(x)).collect();
   53                 ImplItem(i)
   54             }
   55             VariantItem(i) => match i {
   56                 Variant::Struct(mut j) => {
   57                     let num_fields = j.fields.len();
   58                     j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
   59                     if !j.fields_stripped {
   60                         j.fields_stripped = num_fields != j.fields.len()
   61                             || j.fields.iter().any(|f| f.is_stripped());
   62                     }
   63                     VariantItem(Variant::Struct(j))
   64                 }
   65                 Variant::Tuple(fields) => {
   66                     let fields = fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
   67                     VariantItem(Variant::Tuple(fields))
   68                 }
   69                 Variant::CLike => VariantItem(Variant::CLike),
   70             },
   71             ExternCrateItem { src: _ }
   72             | ImportItem(_)
   73             | FunctionItem(_)
   74             | TypedefItem(_, _)
   75             | OpaqueTyItem(_)
   76             | StaticItem(_)
   77             | ConstantItem(_)
   78             | TraitAliasItem(_)
   79             | TyMethodItem(_)
   80             | MethodItem(_, _)
   81             | StructFieldItem(_)
   82             | ForeignFunctionItem(_)
   83             | ForeignStaticItem(_)
   84             | ForeignTypeItem
   85             | MacroItem(_)
   86             | ProcMacroItem(_)
   87             | PrimitiveItem(_)
   88             | AssocConstItem(_, _)
   89             | AssocTypeItem(_, _)
   90             | KeywordItem(_) => kind,
   91         }
   92     }
   93 
   94     /// don't override!
   95     fn fold_item_recur(&mut self, mut item: Item) -> Item {
   96         item.kind = box match *item.kind {
   97             StrippedItem(box i) => StrippedItem(box self.fold_inner_recur(i)),
   98             _ => self.fold_inner_recur(*item.kind),
   99         };
  100         item
  101     }
  102 
  103     fn fold_mod(&mut self, m: Module) -> Module {
  104         Module {
  105             span: m.span,
  106             items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect(),
  107         }
  108     }
  109 
  110     fn fold_crate(&mut self, mut c: Crate) -> Crate {
  111         c.module = self.fold_item(c.module).unwrap();
  112 
  113         let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
  114         for (k, mut v) in external_traits {
  115             v.trait_.items = v.trait_.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
  116             c.external_traits.borrow_mut().insert(k, v);
  117         }
  118 
  119         c
  120     }
  121 }