1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use std::iter::Extendable;
13 use std::mem::{replace, swap};
16 fn fold_item(&mut self, item: Item) -> Option<Item> {
17 self.fold_item_recur(item)
21 fn fold_item_recur(&mut self, item: Item) -> Option<Item> {
22 let Item { attrs, name, source, visibility, id, inner } = item;
24 let inner = match inner {
25 StructItem(mut i) => {
26 let mut foo = ~[]; swap(&mut foo, &mut i.fields);
27 let num_fields = foo.len();
28 i.fields.extend(&mut foo.move_iter().filter_map(|x| self.fold_item(x)));
29 i.fields_stripped |= num_fields != i.fields.len();
33 ModuleItem(self.fold_mod(i))
36 let mut foo = ~[]; swap(&mut foo, &mut i.variants);
37 let num_variants = foo.len();
38 i.variants.extend(&mut foo.move_iter().filter_map(|x| self.fold_item(x)));
39 i.variants_stripped |= num_variants != i.variants.len();
43 fn vtrm<T: DocFolder>(this: &mut T, trm: TraitMethod) -> Option<TraitMethod> {
46 match this.fold_item(it) {
47 Some(x) => return Some(Required(x)),
52 match this.fold_item(it) {
53 Some(x) => return Some(Provided(x)),
59 let mut foo = ~[]; swap(&mut foo, &mut i.methods);
60 i.methods.extend(&mut foo.move_iter().filter_map(|x| vtrm(self, x)));
64 let mut foo = ~[]; swap(&mut foo, &mut i.methods);
65 i.methods.extend(&mut foo.move_iter().filter_map(|x| self.fold_item(x)));
69 let i2 = i.clone(); // this clone is small
71 StructVariant(mut j) => {
72 let mut foo = ~[]; swap(&mut foo, &mut j.fields);
73 let num_fields = foo.len();
74 let c = |x| self.fold_item(x);
75 j.fields.extend(&mut foo.move_iter().filter_map(c));
76 j.fields_stripped |= num_fields != j.fields.len();
77 VariantItem(Variant {kind: StructVariant(j), ..i2})
85 Some(Item { attrs: attrs, name: name, source: source, inner: inner,
86 visibility: visibility, id: id })
89 fn fold_mod(&mut self, m: Module) -> Module {
92 items: m.items.move_iter().filter_map(|i| self.fold_item(i)).collect()
96 fn fold_crate(&mut self, mut c: Crate) -> Crate {
97 c.module = match replace(&mut c.module, None) {
98 Some(module) => self.fold_item(module), None => None