rustdoc: Introduce new DynTrait
type for better representation of trait objects
This commit is contained in:
parent
1f65f56461
commit
4ea27484c9
9 changed files with 117 additions and 116 deletions
|
@ -646,18 +646,24 @@ fn primitive_link(
|
|||
|
||||
/// Helper to render type parameters
|
||||
fn tybounds<'a, 'tcx: 'a>(
|
||||
param_names: Option<&'a Vec<clean::GenericBound>>,
|
||||
bounds: &'a Vec<clean::PolyTrait>,
|
||||
lt: &'a Option<clean::Lifetime>,
|
||||
cx: &'a Context<'tcx>,
|
||||
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
||||
display_fn(move |f| match param_names {
|
||||
Some(params) => {
|
||||
for param in params {
|
||||
display_fn(move |f| {
|
||||
for (i, bound) in bounds.iter().enumerate() {
|
||||
if i > 0 {
|
||||
write!(f, " + ")?;
|
||||
fmt::Display::fmt(¶m.print(cx), f)?;
|
||||
}
|
||||
Ok(())
|
||||
|
||||
fmt::Display::fmt(&bound.print(cx), f)?;
|
||||
}
|
||||
None => Ok(()),
|
||||
|
||||
if let Some(lt) = lt {
|
||||
write!(f, " + ")?;
|
||||
fmt::Display::fmt(<.print(), f)?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -694,32 +700,13 @@ fn fmt_type<'cx>(
|
|||
|
||||
match *t {
|
||||
clean::Generic(name) => write!(f, "{}", name),
|
||||
clean::ResolvedPath { did, ref param_names, ref path, is_generic } => {
|
||||
let generic_params = param_names.as_ref().map(|(_, x)| x);
|
||||
let param_names = param_names.as_ref().map(|(x, _)| x);
|
||||
|
||||
if let Some(generic_params) = generic_params {
|
||||
f.write_str("dyn ")?;
|
||||
|
||||
if !generic_params.is_empty() {
|
||||
if f.alternate() {
|
||||
write!(
|
||||
f,
|
||||
"for<{:#}> ",
|
||||
comma_sep(generic_params.iter().map(|g| g.print(cx)))
|
||||
)?;
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
"for<{}> ",
|
||||
comma_sep(generic_params.iter().map(|g| g.print(cx)))
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
clean::ResolvedPath { did, ref path, is_generic } => {
|
||||
// Paths like `T::Output` and `Self::Output` should be rendered with all segments.
|
||||
resolved_path(f, did, path, is_generic, use_absolute, cx)?;
|
||||
fmt::Display::fmt(&tybounds(param_names, cx), f)
|
||||
resolved_path(f, did, path, is_generic, use_absolute, cx)
|
||||
}
|
||||
clean::DynTrait(ref bounds, ref lt) => {
|
||||
f.write_str("dyn ")?;
|
||||
fmt::Display::fmt(&tybounds(bounds, lt, cx), f)
|
||||
}
|
||||
clean::Infer => write!(f, "_"),
|
||||
clean::Primitive(prim) => primitive_link(f, prim, &*prim.as_sym().as_str(), cx),
|
||||
|
@ -854,7 +841,9 @@ fn fmt_type<'cx>(
|
|||
}
|
||||
}
|
||||
}
|
||||
clean::ResolvedPath { param_names: Some((ref v, _)), .. } if !v.is_empty() => {
|
||||
clean::DynTrait(ref bounds, ref trait_lt)
|
||||
if bounds.len() > 1 || trait_lt.is_some() =>
|
||||
{
|
||||
write!(f, "{}{}{}(", amp, lt, m)?;
|
||||
fmt_type(&ty, f, use_absolute, cx)?;
|
||||
write!(f, ")")
|
||||
|
@ -915,7 +904,7 @@ fn fmt_type<'cx>(
|
|||
// the ugliness comes from inlining across crates where
|
||||
// everything comes in as a fully resolved QPath (hard to
|
||||
// look at).
|
||||
box clean::ResolvedPath { did, ref param_names, .. } => {
|
||||
box clean::ResolvedPath { did, .. } => {
|
||||
match href(did.into(), cx) {
|
||||
Some((ref url, _, ref path)) if !f.alternate() => {
|
||||
write!(
|
||||
|
@ -930,9 +919,6 @@ fn fmt_type<'cx>(
|
|||
}
|
||||
_ => write!(f, "{}", name)?,
|
||||
}
|
||||
|
||||
// FIXME: `param_names` are not rendered, and this seems bad?
|
||||
drop(param_names);
|
||||
Ok(())
|
||||
}
|
||||
_ => write!(f, "{}", name),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue