Skip to content

Commit

Permalink
rustdoc: Make fold require Clone instead of Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Nov 27, 2012
1 parent b21e9d5 commit 19f5f91
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 41 deletions.
4 changes: 4 additions & 0 deletions src/librustdoc/astsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub enum Srv = {
ch: comm::Chan<Msg>
};

impl Srv: Clone {
fn clone(&self) -> Srv { copy *self }
}

pub fn from_str<T>(source: ~str, owner: SrvOwner<T>) -> T {
run(owner, source, parse::from_str_sess)
}
Expand Down
4 changes: 4 additions & 0 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub type Config = {
pandoc_cmd: Option<~str>
};

impl Config: Clone {
fn clone(&self) -> Config { copy *self }
}

fn opt_output_dir() -> ~str { ~"output-dir" }
fn opt_output_format() -> ~str { ~"output-format" }
fn opt_output_style() -> ~str { ~"output-style" }
Expand Down
52 changes: 36 additions & 16 deletions src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
pub enum Fold<T> = Fold_<T>;

impl<T: Clone> Fold<T>: Clone {
fn clone(&self) -> Fold<T> {
Fold({
ctxt: self.ctxt.clone(),
fold_doc: copy self.fold_doc,
fold_crate: copy self.fold_crate,
fold_item: copy self.fold_item,
fold_mod: copy self.fold_mod,
fold_nmod: copy self.fold_nmod,
fold_fn: copy self.fold_fn,
fold_const: copy self.fold_const,
fold_enum: copy self.fold_enum,
fold_trait: copy self.fold_trait,
fold_impl: copy self.fold_impl,
fold_type: copy self.fold_type,
fold_struct: copy self.fold_struct
})
}
}

type FoldDoc<T> = fn~(fold: &Fold<T>, +doc: doc::Doc) -> doc::Doc;
type FoldCrate<T> = fn~(fold: &Fold<T>, +doc: doc::CrateDoc) -> doc::CrateDoc;
type FoldItem<T> = fn~(fold: &Fold<T>, +doc: doc::ItemDoc) -> doc::ItemDoc;
Expand Down Expand Up @@ -33,7 +53,7 @@ type Fold_<T> = {

// This exists because fn types don't infer correctly as record
// initializers, but they do as function arguments
fn mk_fold<T:Copy>(
fn mk_fold<T:Clone>(
+ctxt: T,
+fold_doc: FoldDoc<T>,
+fold_crate: FoldCrate<T>,
Expand All @@ -49,7 +69,7 @@ fn mk_fold<T:Copy>(
+fold_struct: FoldStruct<T>
) -> Fold<T> {
Fold({
ctxt: ctxt,
ctxt: move ctxt,
fold_doc: move fold_doc,
fold_crate: move fold_crate,
fold_item: move fold_item,
Expand All @@ -65,9 +85,9 @@ fn mk_fold<T:Copy>(
})
}

pub fn default_any_fold<T:Send Copy>(+ctxt: T) -> Fold<T> {
pub fn default_any_fold<T:Send Clone>(+ctxt: T) -> Fold<T> {
mk_fold(
ctxt,
move ctxt,
|f, d| default_seq_fold_doc(f, d),
|f, d| default_seq_fold_crate(f, d),
|f, d| default_seq_fold_item(f, d),
Expand All @@ -83,9 +103,9 @@ pub fn default_any_fold<T:Send Copy>(+ctxt: T) -> Fold<T> {
)
}

pub fn default_seq_fold<T:Copy>(+ctxt: T) -> Fold<T> {
pub fn default_seq_fold<T:Clone>(+ctxt: T) -> Fold<T> {
mk_fold(
ctxt,
move ctxt,
|f, d| default_seq_fold_doc(f, d),
|f, d| default_seq_fold_crate(f, d),
|f, d| default_seq_fold_item(f, d),
Expand All @@ -101,9 +121,9 @@ pub fn default_seq_fold<T:Copy>(+ctxt: T) -> Fold<T> {
)
}

pub fn default_par_fold<T:Send Copy>(+ctxt: T) -> Fold<T> {
pub fn default_par_fold<T:Send Clone>(+ctxt: T) -> Fold<T> {
mk_fold(
ctxt,
move ctxt,
|f, d| default_seq_fold_doc(f, d),
|f, d| default_seq_fold_crate(f, d),
|f, d| default_seq_fold_item(f, d),
Expand Down Expand Up @@ -151,11 +171,11 @@ pub fn default_seq_fold_item<T>(
doc
}

pub fn default_any_fold_mod<T:Send Copy>(
pub fn default_any_fold_mod<T:Send Clone>(
fold: &Fold<T>,
+doc: doc::ModDoc
) -> doc::ModDoc {
let fold_copy = copy *fold;
let fold_copy = fold.clone();
doc::ModDoc_({
item: fold.fold_item(fold, doc.item),
items: par::map(doc.items, |ItemTag, move fold_copy| {
Expand All @@ -178,11 +198,11 @@ pub fn default_seq_fold_mod<T>(
})
}

pub fn default_par_fold_mod<T:Send Copy>(
pub fn default_par_fold_mod<T:Send Clone>(
fold: &Fold<T>,
+doc: doc::ModDoc
) -> doc::ModDoc {
let fold_copy = copy *fold;
let fold_copy = fold.clone();
doc::ModDoc_({
item: fold.fold_item(fold, doc.item),
items: par::map(doc.items, |ItemTag, move fold_copy| {
Expand All @@ -192,11 +212,11 @@ pub fn default_par_fold_mod<T:Send Copy>(
})
}

pub fn default_any_fold_nmod<T:Send Copy>(
pub fn default_any_fold_nmod<T:Send Clone>(
fold: &Fold<T>,
+doc: doc::NmodDoc
) -> doc::NmodDoc {
let fold_copy = copy *fold;
let fold_copy = fold.clone();
{
item: fold.fold_item(fold, doc.item),
fns: par::map(doc.fns, |FnDoc, move fold_copy| {
Expand All @@ -219,11 +239,11 @@ pub fn default_seq_fold_nmod<T>(
}
}

pub fn default_par_fold_nmod<T:Send Copy>(
pub fn default_par_fold_nmod<T:Send Clone>(
fold: &Fold<T>,
+doc: doc::NmodDoc
) -> doc::NmodDoc {
let fold_copy = copy *fold;
let fold_copy = fold.clone();
{
item: fold.fold_item(fold, doc.item),
fns: par::map(doc.fns, |FnDoc, move fold_copy| {
Expand Down
17 changes: 10 additions & 7 deletions src/librustdoc/page_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ individual modules, pages for the crate, indexes, etc.

use doc::{ItemUtils, PageUtils};
use syntax::ast;
use util::NominalOp;

pub fn mk_pass(output_style: config::OutputStyle) -> Pass {
{
Expand Down Expand Up @@ -39,6 +40,8 @@ fn run(
type PagePort = comm::Port<Option<doc::Page>>;
type PageChan = comm::Chan<Option<doc::Page>>;

type NominalPageChan = NominalOp<PageChan>;

fn make_doc_from_pages(page_port: PagePort) -> doc::Doc {
let mut pages = ~[];
loop {
Expand All @@ -59,15 +62,15 @@ fn find_pages(doc: doc::Doc, page_chan: PageChan) {
fold_crate: fold_crate,
fold_mod: fold_mod,
fold_nmod: fold_nmod,
.. *fold::default_any_fold(page_chan)
.. *fold::default_any_fold(NominalOp { op: page_chan })
});
fold.fold_doc(&fold, doc);

comm::send(page_chan, None);
}

fn fold_crate(
fold: &fold::Fold<PageChan>,
fold: &fold::Fold<NominalPageChan>,
+doc: doc::CrateDoc
) -> doc::CrateDoc {

Expand All @@ -78,13 +81,13 @@ fn fold_crate(
.. doc
});

comm::send(fold.ctxt, Some(page));
comm::send(fold.ctxt.op, Some(page));

doc
}

fn fold_mod(
fold: &fold::Fold<PageChan>,
fold: &fold::Fold<NominalPageChan>,
+doc: doc::ModDoc
) -> doc::ModDoc {

Expand All @@ -94,7 +97,7 @@ fn fold_mod(

let doc = strip_mod(doc);
let page = doc::ItemPage(doc::ModTag(doc));
comm::send(fold.ctxt, Some(page));
comm::send(fold.ctxt.op, Some(page));
}

doc
Expand All @@ -114,12 +117,12 @@ fn strip_mod(doc: doc::ModDoc) -> doc::ModDoc {
}

fn fold_nmod(
fold: &fold::Fold<PageChan>,
fold: &fold::Fold<NominalPageChan>,
+doc: doc::NmodDoc
) -> doc::NmodDoc {
let doc = fold::default_seq_fold_nmod(fold, doc);
let page = doc::ItemPage(doc::NmodTag(doc));
comm::send(fold.ctxt, Some(page));
comm::send(fold.ctxt.op, Some(page));
return doc;
}

Expand Down
10 changes: 7 additions & 3 deletions src/librustdoc/path_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ pub fn mk_pass() -> Pass {
}
}

type Ctxt = {
struct Ctxt {
srv: astsrv::Srv,
mut path: ~[~str]
};
}

impl Ctxt: Clone {
fn clone(&self) -> Ctxt { copy *self }
}

#[allow(non_implicitly_copyable_typarams)]
fn run(srv: astsrv::Srv, +doc: doc::Doc) -> doc::Doc {
let ctxt = {
let ctxt = Ctxt {
srv: srv,
mut path: ~[]
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/prune_private_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn is_visible(srv: astsrv::Srv, doc: doc::ItemDoc) -> bool {
ast_map::node_item(item, _) => {
item.vis == ast::public
}
_ => util::unreachable()
_ => core::util::unreachable()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/rustdoc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ mod page_pass;
mod sectionalize_pass;
mod escape_pass;
mod prune_private_pass;
mod util;
11 changes: 7 additions & 4 deletions src/librustdoc/sort_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

use doc::ItemUtils;
use std::sort;
use util::NominalOp;

pub type ItemLtEq = pure fn~(v1: &doc::ItemTag, v2: &doc::ItemTag) -> bool;
pub type ItemLtEqOp = pure fn~(v1: &doc::ItemTag, v2: &doc::ItemTag) -> bool;

pub fn mk_pass(name: ~str, +lteq: ItemLtEq) -> Pass {
type ItemLtEq = NominalOp<ItemLtEqOp>;

pub fn mk_pass(name: ~str, +lteq: ItemLtEqOp) -> Pass {
{
name: name,
f: fn~(move lteq, srv: astsrv::Srv, +doc: doc::Doc) -> doc::Doc {
run(srv, doc, copy lteq)
run(srv, doc, NominalOp { op: copy lteq })
}
}
}
Expand All @@ -34,7 +37,7 @@ fn fold_mod(
) -> doc::ModDoc {
let doc = fold::default_any_fold_mod(fold, doc);
doc::ModDoc_({
items: sort::merge_sort(doc.items, fold.ctxt),
items: sort::merge_sort(doc.items, fold.ctxt.op),
.. *doc
})
}
Expand Down
41 changes: 31 additions & 10 deletions src/librustdoc/text_pass.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Generic pass for performing an operation on all descriptions

use doc::ItemUtils;
use util::NominalOp;

pub fn mk_pass(name: ~str, +op: fn~(~str) -> ~str) -> Pass {
{
Expand All @@ -19,6 +20,9 @@ fn run(
+doc: doc::Doc,
+op: Op
) -> doc::Doc {
let op = NominalOp {
op: move op
};
let fold = fold::Fold({
fold_item: fold_item,
fold_enum: fold_enum,
Expand All @@ -29,11 +33,14 @@ fn run(
fold.fold_doc(&fold, doc)
}

fn maybe_apply_op(op: Op, s: Option<~str>) -> Option<~str> {
s.map(|s| op(*s) )
fn maybe_apply_op(op: NominalOp<Op>, s: Option<~str>) -> Option<~str> {
s.map(|s| op.op(*s) )
}

fn fold_item(fold: &fold::Fold<Op>, +doc: doc::ItemDoc) -> doc::ItemDoc {
fn fold_item(
fold: &fold::Fold<NominalOp<Op>>,
+doc: doc::ItemDoc
) -> doc::ItemDoc {
let doc = fold::default_seq_fold_item(fold, doc);

{
Expand All @@ -44,14 +51,19 @@ fn fold_item(fold: &fold::Fold<Op>, +doc: doc::ItemDoc) -> doc::ItemDoc {
}
}

fn apply_to_sections(op: Op, sections: ~[doc::Section]) -> ~[doc::Section] {
fn apply_to_sections(
op: NominalOp<Op>,
sections: ~[doc::Section]
) -> ~[doc::Section] {
par::map(sections, |section, copy op| {
header: op(section.header),
body: op(section.body)
header: op.op(section.header),
body: op.op(section.body)
})
}

fn fold_enum(fold: &fold::Fold<Op>, +doc: doc::EnumDoc) -> doc::EnumDoc {
fn fold_enum(
fold: &fold::Fold<NominalOp<Op>>,
+doc: doc::EnumDoc) -> doc::EnumDoc {
let doc = fold::default_seq_fold_enum(fold, doc);
let fold_copy = copy *fold;

Expand All @@ -66,7 +78,10 @@ fn fold_enum(fold: &fold::Fold<Op>, +doc: doc::EnumDoc) -> doc::EnumDoc {
}
}

fn fold_trait(fold: &fold::Fold<Op>, +doc: doc::TraitDoc) -> doc::TraitDoc {
fn fold_trait(
fold: &fold::Fold<NominalOp<Op>>,
+doc: doc::TraitDoc
) -> doc::TraitDoc {
let doc = fold::default_seq_fold_trait(fold, doc);

{
Expand All @@ -75,7 +90,10 @@ fn fold_trait(fold: &fold::Fold<Op>, +doc: doc::TraitDoc) -> doc::TraitDoc {
}
}

fn apply_to_methods(op: Op, docs: ~[doc::MethodDoc]) -> ~[doc::MethodDoc] {
fn apply_to_methods(
op: NominalOp<Op>,
docs: ~[doc::MethodDoc]
) -> ~[doc::MethodDoc] {
do par::map(docs) |doc, copy op| {
{
brief: maybe_apply_op(op, doc.brief),
Expand All @@ -86,7 +104,10 @@ fn apply_to_methods(op: Op, docs: ~[doc::MethodDoc]) -> ~[doc::MethodDoc] {
}
}

fn fold_impl(fold: &fold::Fold<Op>, +doc: doc::ImplDoc) -> doc::ImplDoc {
fn fold_impl(
fold: &fold::Fold<NominalOp<Op>>,
+doc: doc::ImplDoc
) -> doc::ImplDoc {
let doc = fold::default_seq_fold_impl(fold, doc);

{
Expand Down
Loading

0 comments on commit 19f5f91

Please sign in to comment.