Skip to content

Commit c639cf6

Browse files
committed
rustdoc: Use unboxed closure sugar
This unfortunately leaves sugaring Fn/FnMut/FnOnce on cross-crate re-exports for future work. cc rust-lang#19909
1 parent 3722528 commit c639cf6

File tree

2 files changed

+98
-58
lines changed

2 files changed

+98
-58
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,10 @@ fn external_path(cx: &DocContext, name: &str, substs: &subst::Substs) -> Path {
526526
global: false,
527527
segments: vec![PathSegment {
528528
name: name.to_string(),
529-
lifetimes: lifetimes,
530-
types: types,
529+
params: PathParameters::AngleBracketed {
530+
lifetimes: lifetimes,
531+
types: types,
532+
}
531533
}],
532534
}
533535
}
@@ -1744,31 +1746,48 @@ impl Clean<Path> for ast::Path {
17441746
}
17451747

17461748
#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)]
1747-
pub struct PathSegment {
1748-
pub name: String,
1749-
pub lifetimes: Vec<Lifetime>,
1750-
pub types: Vec<Type>,
1749+
pub enum PathParameters {
1750+
AngleBracketed {
1751+
lifetimes: Vec<Lifetime>,
1752+
types: Vec<Type>,
1753+
},
1754+
Parenthesized {
1755+
inputs: Vec<Type>,
1756+
output: Option<Type>
1757+
}
17511758
}
17521759

1753-
impl Clean<PathSegment> for ast::PathSegment {
1754-
fn clean(&self, cx: &DocContext) -> PathSegment {
1755-
let (lifetimes, types) = match self.parameters {
1760+
impl Clean<PathParameters> for ast::PathParameters {
1761+
fn clean(&self, cx: &DocContext) -> PathParameters {
1762+
match *self {
17561763
ast::AngleBracketedParameters(ref data) => {
1757-
(data.lifetimes.clean(cx), data.types.clean(cx))
1764+
PathParameters::AngleBracketed {
1765+
lifetimes: data.lifetimes.clean(cx),
1766+
types: data.types.clean(cx)
1767+
}
17581768
}
17591769

17601770
ast::ParenthesizedParameters(ref data) => {
1761-
// FIXME -- rustdoc should be taught about Foo() notation
1762-
let inputs = Tuple(data.inputs.clean(cx));
1763-
let output = data.output.as_ref().map(|t| t.clean(cx)).unwrap_or(Tuple(Vec::new()));
1764-
(Vec::new(), vec![inputs, output])
1771+
PathParameters::Parenthesized {
1772+
inputs: data.inputs.clean(cx),
1773+
output: data.output.clean(cx)
1774+
}
17651775
}
1766-
};
1776+
}
1777+
}
1778+
}
17671779

1780+
#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)]
1781+
pub struct PathSegment {
1782+
pub name: String,
1783+
pub params: PathParameters
1784+
}
1785+
1786+
impl Clean<PathSegment> for ast::PathSegment {
1787+
fn clean(&self, cx: &DocContext) -> PathSegment {
17681788
PathSegment {
17691789
name: self.identifier.clean(cx),
1770-
lifetimes: lifetimes,
1771-
types: types,
1790+
params: self.parameters.clean(cx)
17721791
}
17731792
}
17741793
}
@@ -2399,8 +2418,10 @@ fn lang_struct(cx: &DocContext, did: Option<ast::DefId>,
23992418
global: false,
24002419
segments: vec![PathSegment {
24012420
name: name.to_string(),
2402-
lifetimes: vec![],
2403-
types: vec![t.clean(cx)],
2421+
params: PathParameters::AngleBracketed {
2422+
lifetimes: vec![],
2423+
types: vec![t.clean(cx)],
2424+
}
24042425
}],
24052426
},
24062427
}

src/librustdoc/html/format.rs

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -171,37 +171,69 @@ impl fmt::Show for clean::TyParamBound {
171171
}
172172
}
173173

174-
impl fmt::Show for clean::Path {
174+
impl fmt::Show for clean::PathParameters {
175175
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
176-
if self.global {
177-
try!(f.write("::".as_bytes()))
178-
}
179-
180-
for (i, seg) in self.segments.iter().enumerate() {
181-
if i > 0 {
182-
try!(f.write("::".as_bytes()))
183-
}
184-
try!(f.write(seg.name.as_bytes()));
185-
186-
if seg.lifetimes.len() > 0 || seg.types.len() > 0 {
187-
try!(f.write("&lt;".as_bytes()));
188-
let mut comma = false;
189-
for lifetime in seg.lifetimes.iter() {
190-
if comma {
191-
try!(f.write(", ".as_bytes()));
176+
match *self {
177+
clean::PathParameters::AngleBracketed { ref lifetimes, ref types } => {
178+
if lifetimes.len() > 0 || types.len() > 0 {
179+
try!(f.write("&lt;".as_bytes()));
180+
let mut comma = false;
181+
for lifetime in lifetimes.iter() {
182+
if comma {
183+
try!(f.write(", ".as_bytes()));
184+
}
185+
comma = true;
186+
try!(write!(f, "{}", *lifetime));
192187
}
193-
comma = true;
194-
try!(write!(f, "{}", *lifetime));
188+
for ty in types.iter() {
189+
if comma {
190+
try!(f.write(", ".as_bytes()));
191+
}
192+
comma = true;
193+
try!(write!(f, "{}", *ty));
194+
}
195+
try!(f.write("&gt;".as_bytes()));
195196
}
196-
for ty in seg.types.iter() {
197+
}
198+
clean::PathParameters::Parenthesized { ref inputs, ref output } => {
199+
try!(f.write("(".as_bytes()));
200+
let mut comma = false;
201+
for ty in inputs.iter() {
197202
if comma {
198203
try!(f.write(", ".as_bytes()));
199204
}
200205
comma = true;
201206
try!(write!(f, "{}", *ty));
202207
}
203-
try!(f.write("&gt;".as_bytes()));
208+
try!(f.write(")".as_bytes()));
209+
if let Some(ref ty) = *output {
210+
try!(f.write(" -&gt; ".as_bytes()));
211+
try!(write!(f, "{}", ty));
212+
}
213+
}
214+
}
215+
Ok(())
216+
}
217+
}
218+
219+
impl fmt::Show for clean::PathSegment {
220+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
221+
try!(f.write(self.name.as_bytes()));
222+
write!(f, "{}", self.params)
223+
}
224+
}
225+
226+
impl fmt::Show for clean::Path {
227+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
228+
if self.global {
229+
try!(f.write("::".as_bytes()))
230+
}
231+
232+
for (i, seg) in self.segments.iter().enumerate() {
233+
if i > 0 {
234+
try!(f.write("::".as_bytes()))
204235
}
236+
try!(write!(f, "{}", seg));
205237
}
206238
Ok(())
207239
}
@@ -243,23 +275,8 @@ fn path<F, G>(w: &mut fmt::Formatter,
243275
G: FnOnce(&render::Cache) -> Option<(Vec<String>, ItemType)>,
244276
{
245277
// The generics will get written to both the title and link
246-
let mut generics = String::new();
247278
let last = path.segments.last().unwrap();
248-
if last.lifetimes.len() > 0 || last.types.len() > 0 {
249-
let mut counter = 0u;
250-
generics.push_str("&lt;");
251-
for lifetime in last.lifetimes.iter() {
252-
if counter > 0 { generics.push_str(", "); }
253-
counter += 1;
254-
generics.push_str(format!("{}", *lifetime).as_slice());
255-
}
256-
for ty in last.types.iter() {
257-
if counter > 0 { generics.push_str(", "); }
258-
counter += 1;
259-
generics.push_str(format!("{}", *ty).as_slice());
260-
}
261-
generics.push_str("&gt;");
262-
}
279+
let generics = format!("{}", last.params);
263280

264281
let loc = CURRENT_LOCATION_KEY.with(|l| l.borrow().clone());
265282
let cache = cache();
@@ -660,8 +677,10 @@ impl fmt::Show for clean::ViewListIdent {
660677
global: false,
661678
segments: vec!(clean::PathSegment {
662679
name: self.name.clone(),
663-
lifetimes: Vec::new(),
664-
types: Vec::new(),
680+
params: clean::PathParameters::AngleBracketed {
681+
lifetimes: Vec::new(),
682+
types: Vec::new(),
683+
}
665684
})
666685
};
667686
resolved_path(f, did, &path, false)

0 commit comments

Comments
 (0)