Skip to content

Commit 5ac7a03

Browse files
Put the const type and value into <code>
1 parent 081336e commit 5ac7a03

File tree

3 files changed

+88
-27
lines changed

3 files changed

+88
-27
lines changed

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ pub struct PolyTrait {
14751475
/// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
14761476
/// type out of the AST/TyCtxt given one of these, if more information is needed. Most importantly
14771477
/// it does not preserve mutability or boxes.
1478-
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1478+
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq)]
14791479
pub enum Type {
14801480
/// structs/enums/traits (most that'd be an hir::TyPath)
14811481
ResolvedPath {

src/librustdoc/html/format.rs

+70-18
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ impl<'a> fmt::Display for HRef<'a> {
560560
}
561561
}
562562

563-
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt::Result {
563+
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
564+
is_not_debug: bool) -> fmt::Result {
564565
match *t {
565566
clean::Generic(ref name) => {
566567
f.write_str(name)
@@ -571,7 +572,8 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
571572
tybounds(f, typarams)
572573
}
573574
clean::Infer => write!(f, "_"),
574-
clean::Primitive(prim) => primitive_link(f, prim, prim.as_str()),
575+
clean::Primitive(prim) if is_not_debug => primitive_link(f, prim, prim.as_str()),
576+
clean::Primitive(prim) => write!(f, "{}", prim.as_str()),
575577
clean::BareFunction(ref decl) => {
576578
if f.alternate() {
577579
write!(f, "{}{}fn{:#}{:#}",
@@ -589,26 +591,30 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
589591
}
590592
clean::Tuple(ref typs) => {
591593
match &typs[..] {
592-
&[] => primitive_link(f, PrimitiveType::Tuple, "()"),
593-
&[ref one] => {
594+
&[] if is_not_debug => primitive_link(f, PrimitiveType::Tuple, "()"),
595+
&[] => write!(f, "()"),
596+
&[ref one] if is_not_debug => {
594597
primitive_link(f, PrimitiveType::Tuple, "(")?;
595598
//carry f.alternate() into this display w/o branching manually
596599
fmt::Display::fmt(one, f)?;
597600
primitive_link(f, PrimitiveType::Tuple, ",)")
598601
}
599-
many => {
602+
&[ref one] => write!(f, "({},)", one),
603+
many if is_not_debug => {
600604
primitive_link(f, PrimitiveType::Tuple, "(")?;
601605
fmt::Display::fmt(&CommaSep(&many), f)?;
602606
primitive_link(f, PrimitiveType::Tuple, ")")
603607
}
608+
many => write!(f, "({})", &CommaSep(&many)),
604609
}
605610
}
606-
clean::Vector(ref t) => {
611+
clean::Vector(ref t) if is_not_debug => {
607612
primitive_link(f, PrimitiveType::Slice, &format!("["))?;
608613
fmt::Display::fmt(t, f)?;
609614
primitive_link(f, PrimitiveType::Slice, &format!("]"))
610615
}
611-
clean::FixedVector(ref t, ref s) => {
616+
clean::Vector(ref t) => write!(f, "[{}]", t),
617+
clean::FixedVector(ref t, ref s) if is_not_debug => {
612618
primitive_link(f, PrimitiveType::Array, "[")?;
613619
fmt::Display::fmt(t, f)?;
614620
if f.alternate() {
@@ -619,10 +625,17 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
619625
&format!("; {}]", Escape(s)))
620626
}
621627
}
628+
clean::FixedVector(ref t, ref s) => {
629+
if f.alternate() {
630+
write!(f, "[{}; {}]", t, s)
631+
} else {
632+
write!(f, "[{}; {}]", t, Escape(s))
633+
}
634+
}
622635
clean::Never => f.write_str("!"),
623636
clean::RawPointer(m, ref t) => {
624637
match **t {
625-
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
638+
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} if is_not_debug => {
626639
if f.alternate() {
627640
primitive_link(f, clean::PrimitiveType::RawPointer,
628641
&format!("*{}{:#}", RawMutableSpace(m), t))
@@ -631,11 +644,21 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
631644
&format!("*{}{}", RawMutableSpace(m), t))
632645
}
633646
}
634-
_ => {
647+
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
648+
if f.alternate() {
649+
write!(f, "*{}{:#}", RawMutableSpace(m), t)
650+
} else {
651+
write!(f, "*{}{}", RawMutableSpace(m), t)
652+
}
653+
}
654+
_ if is_not_debug => {
635655
primitive_link(f, clean::PrimitiveType::RawPointer,
636656
&format!("*{}", RawMutableSpace(m)))?;
637657
fmt::Display::fmt(t, f)
638658
}
659+
_ => {
660+
write!(f, "*{}{}", RawMutableSpace(m), t)
661+
}
639662
}
640663
}
641664
clean::BorrowedRef{ lifetime: ref l, mutability, type_: ref ty} => {
@@ -647,15 +670,23 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
647670
match **ty {
648671
clean::Vector(ref bt) => { // BorrowedRef{ ... Vector(T) } is &[T]
649672
match **bt {
650-
clean::Generic(_) =>
673+
clean::Generic(_) if is_not_debug => {
651674
if f.alternate() {
652675
primitive_link(f, PrimitiveType::Slice,
653676
&format!("&{}{}[{:#}]", lt, m, **bt))
654677
} else {
655678
primitive_link(f, PrimitiveType::Slice,
656679
&format!("&amp;{}{}[{}]", lt, m, **bt))
657-
},
658-
_ => {
680+
}
681+
}
682+
clean::Generic(_) => {
683+
if f.alternate() {
684+
write!(f, "&{}{}[{:#}]", lt, m, **bt)
685+
} else {
686+
write!(f, "&{}{}[{}]", lt, m, **bt)
687+
}
688+
}
689+
_ if is_not_debug => {
659690
if f.alternate() {
660691
primitive_link(f, PrimitiveType::Slice,
661692
&format!("&{}{}[", lt, m))?;
@@ -667,15 +698,26 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
667698
}
668699
primitive_link(f, PrimitiveType::Slice, "]")
669700
}
701+
_ => {
702+
if f.alternate() {
703+
write!(f, "&{}{}[{:#}]", lt, m, **bt)
704+
} else {
705+
write!(f, "&{}{}[{}]", lt, m, **bt)
706+
}
707+
}
670708
}
671709
}
672710
_ => {
673711
if f.alternate() {
674712
write!(f, "&{}{}", lt, m)?;
675-
fmt_type(&ty, f, use_absolute)
713+
fmt_type(&ty, f, use_absolute, is_not_debug)
676714
} else {
677-
write!(f, "&amp;{}{}", lt, m)?;
678-
fmt_type(&ty, f, use_absolute)
715+
if is_not_debug {
716+
write!(f, "&amp;{}{}", lt, m)?;
717+
} else {
718+
write!(f, "&{}{}", lt, m)?;
719+
}
720+
fmt_type(&ty, f, use_absolute, is_not_debug)
679721
}
680722
}
681723
}
@@ -725,7 +767,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
725767
if f.alternate() {
726768
write!(f, "<{:#} as {:#}>::{}", self_type, trait_, name)
727769
} else {
728-
write!(f, "&lt;{} as {}&gt;::{}", self_type, trait_, name)
770+
if is_not_debug {
771+
write!(f, "&lt;{} as {}&gt;::{}", self_type, trait_, name)
772+
} else {
773+
write!(f, "<{} as {}>::{}", self_type, trait_, name)
774+
}
729775
}
730776
}
731777
clean::Unique(..) => {
@@ -736,7 +782,13 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
736782

737783
impl fmt::Display for clean::Type {
738784
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
739-
fmt_type(self, f, false)
785+
fmt_type(self, f, false, true)
786+
}
787+
}
788+
789+
impl fmt::Debug for clean::Type {
790+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
791+
fmt_type(self, f, false, false)
740792
}
741793
}
742794

@@ -777,7 +829,7 @@ fn fmt_impl(i: &clean::Impl,
777829
plain.push_str(" for ");
778830
}
779831

780-
fmt_type(&i.for_, f, use_absolute)?;
832+
fmt_type(&i.for_, f, use_absolute, true)?;
781833
plain.push_str(&format!("{:#}", i.for_));
782834

783835
fmt::Display::fmt(&WhereClause(&i.generics, plain.len() + 1), f)?;

src/librustdoc/html/render.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -1654,9 +1654,23 @@ fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLin
16541654
Ok(())
16551655
}
16561656

1657+
fn md_render_assoc_item(item: &clean::Item) -> String {
1658+
match item.inner {
1659+
clean::AssociatedConstItem(ref ty, ref default) => {
1660+
if let Some(default) = default.as_ref() {
1661+
format!("```\n{}: {:?} = {}\n```\n\n", item.name.as_ref().unwrap(), ty, default)
1662+
} else {
1663+
format!("```\n{}: {:?}\n```\n\n", item.name.as_ref().unwrap(), ty)
1664+
}
1665+
}
1666+
_ => String::new(),
1667+
}
1668+
}
1669+
16571670
fn document_full(w: &mut fmt::Formatter, item: &clean::Item) -> fmt::Result {
16581671
if let Some(s) = item.doc_value() {
1659-
write!(w, "<div class='docblock'>{}</div>", Markdown(s))?;
1672+
write!(w, "<div class='docblock'>{}</div>",
1673+
Markdown(&format!("{}{}", md_render_assoc_item(item), s)))?;
16601674
}
16611675
Ok(())
16621676
}
@@ -2214,17 +2228,12 @@ fn naive_assoc_href(it: &clean::Item, link: AssocItemLink) -> String {
22142228

22152229
fn assoc_const(w: &mut fmt::Formatter,
22162230
it: &clean::Item,
2217-
ty: &clean::Type,
2218-
default: Option<&String>,
2231+
_ty: &clean::Type,
2232+
_default: Option<&String>,
22192233
link: AssocItemLink) -> fmt::Result {
22202234
write!(w, "const <a href='{}' class='constant'><b>{}</b></a>",
22212235
naive_assoc_href(it, link),
22222236
it.name.as_ref().unwrap())?;
2223-
2224-
write!(w, ": {}", ty)?;
2225-
if let Some(default) = default {
2226-
write!(w, " = {}", Escape(default))?;
2227-
}
22282237
Ok(())
22292238
}
22302239

0 commit comments

Comments
 (0)