Factor out foldable better to reduce code duplication.  There is now a "double
dispatch" pattern that lets you easily write new folders and then apply them to
various structures, reusing the "folding" effort.
auto merge of #14158 : nikomatsakis/rust/issue-5527-refactor-foldable, r=pcwalton

Factor out foldable better to reduce code duplication. 

r? @pcwalton (probably want to let travis run...)