@@ -2021,10 +2021,33 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
2021
2021
Ok ( ( ) )
2022
2022
}
2023
2023
2024
- fn assoc_const ( w : & mut fmt:: Formatter , it : & clean:: Item ,
2025
- ty : & clean:: Type , default : Option < & String > )
2026
- -> fmt:: Result {
2027
- write ! ( w, "const {}" , it. name. as_ref( ) . unwrap( ) ) ?;
2024
+ fn naive_assoc_href ( it : & clean:: Item , link : AssocItemLink ) -> String {
2025
+ use html:: item_type:: ItemType :: * ;
2026
+
2027
+ let name = it. name . as_ref ( ) . unwrap ( ) ;
2028
+ let ty = match shortty ( it) {
2029
+ Typedef | AssociatedType => AssociatedType ,
2030
+ s@_ => s,
2031
+ } ;
2032
+
2033
+ let anchor = format ! ( "#{}.{}" , ty, name) ;
2034
+ match link {
2035
+ AssocItemLink :: Anchor => anchor,
2036
+ AssocItemLink :: GotoSource ( did, _) => {
2037
+ href ( did) . map ( |p| format ! ( "{}{}" , p. 0 , anchor) ) . unwrap_or ( anchor)
2038
+ }
2039
+ }
2040
+ }
2041
+
2042
+ fn assoc_const ( w : & mut fmt:: Formatter ,
2043
+ it : & clean:: Item ,
2044
+ ty : & clean:: Type ,
2045
+ default : Option < & String > ,
2046
+ link : AssocItemLink ) -> fmt:: Result {
2047
+ write ! ( w, "const <a href='{}' class='constant'>{}</a>" ,
2048
+ naive_assoc_href( it, link) ,
2049
+ it. name. as_ref( ) . unwrap( ) ) ?;
2050
+
2028
2051
write ! ( w, ": {}" , ty) ?;
2029
2052
if let Some ( default) = default {
2030
2053
write ! ( w, " = {}" , default ) ?;
@@ -2034,13 +2057,15 @@ fn assoc_const(w: &mut fmt::Formatter, it: &clean::Item,
2034
2057
2035
2058
fn assoc_type ( w : & mut fmt:: Formatter , it : & clean:: Item ,
2036
2059
bounds : & Vec < clean:: TyParamBound > ,
2037
- default : & Option < clean:: Type > )
2038
- -> fmt:: Result {
2039
- write ! ( w, "type {}" , it. name. as_ref( ) . unwrap( ) ) ?;
2060
+ default : Option < & clean:: Type > ,
2061
+ link : AssocItemLink ) -> fmt:: Result {
2062
+ write ! ( w, "type <a href='{}' class='type'>{}</a>" ,
2063
+ naive_assoc_href( it, link) ,
2064
+ it. name. as_ref( ) . unwrap( ) ) ?;
2040
2065
if !bounds. is_empty ( ) {
2041
2066
write ! ( w, ": {}" , TyParamBounds ( bounds) ) ?
2042
2067
}
2043
- if let Some ( ref default) = * default {
2068
+ if let Some ( default) = default {
2044
2069
write ! ( w, " = {}" , default ) ?;
2045
2070
}
2046
2071
Ok ( ( ) )
@@ -2095,6 +2120,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
2095
2120
href ( did) . map ( |p| format ! ( "{}#{}.{}" , p. 0 , ty, name) ) . unwrap_or ( anchor)
2096
2121
}
2097
2122
} ;
2123
+ // FIXME(#24111): remove when `const_fn` is stabilized
2098
2124
let vis_constness = match get_unstable_features_setting ( ) {
2099
2125
UnstableFeatures :: Allow => constness,
2100
2126
_ => hir:: Constness :: NotConst
@@ -2124,10 +2150,10 @@ fn render_assoc_item(w: &mut fmt::Formatter,
2124
2150
link)
2125
2151
}
2126
2152
clean:: AssociatedConstItem ( ref ty, ref default) => {
2127
- assoc_const ( w, item, ty, default. as_ref ( ) )
2153
+ assoc_const ( w, item, ty, default. as_ref ( ) , link )
2128
2154
}
2129
2155
clean:: AssociatedTypeItem ( ref bounds, ref default) => {
2130
- assoc_type ( w, item, bounds, default)
2156
+ assoc_type ( w, item, bounds, default. as_ref ( ) , link )
2131
2157
}
2132
2158
_ => panic ! ( "render_assoc_item called on non-associated-item" )
2133
2159
}
@@ -2487,25 +2513,25 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
2487
2513
clean:: TypedefItem ( ref tydef, _) => {
2488
2514
let id = derive_id ( format ! ( "{}.{}" , ItemType :: AssociatedType , name) ) ;
2489
2515
write ! ( w, "<h4 id='{}' class='{}'><code>" , id, shortty) ?;
2490
- write ! ( w, "type {} = {}" , name , tydef. type_) ?;
2516
+ assoc_type ( w, item , & Vec :: new ( ) , Some ( & tydef. type_ ) , link ) ?;
2491
2517
write ! ( w, "</code></h4>\n " ) ?;
2492
2518
}
2493
2519
clean:: AssociatedConstItem ( ref ty, ref default) => {
2494
2520
let id = derive_id ( format ! ( "{}.{}" , shortty, name) ) ;
2495
2521
write ! ( w, "<h4 id='{}' class='{}'><code>" , id, shortty) ?;
2496
- assoc_const ( w, item, ty, default. as_ref ( ) ) ?;
2522
+ assoc_const ( w, item, ty, default. as_ref ( ) , link ) ?;
2497
2523
write ! ( w, "</code></h4>\n " ) ?;
2498
2524
}
2499
2525
clean:: ConstantItem ( ref c) => {
2500
2526
let id = derive_id ( format ! ( "{}.{}" , shortty, name) ) ;
2501
2527
write ! ( w, "<h4 id='{}' class='{}'><code>" , id, shortty) ?;
2502
- assoc_const ( w, item, & c. type_ , Some ( & c. expr ) ) ?;
2528
+ assoc_const ( w, item, & c. type_ , Some ( & c. expr ) , link ) ?;
2503
2529
write ! ( w, "</code></h4>\n " ) ?;
2504
2530
}
2505
2531
clean:: AssociatedTypeItem ( ref bounds, ref default) => {
2506
2532
let id = derive_id ( format ! ( "{}.{}" , shortty, name) ) ;
2507
2533
write ! ( w, "<h4 id='{}' class='{}'><code>" , id, shortty) ?;
2508
- assoc_type ( w, item, bounds, default) ?;
2534
+ assoc_type ( w, item, bounds, default. as_ref ( ) , link ) ?;
2509
2535
write ! ( w, "</code></h4>\n " ) ?;
2510
2536
}
2511
2537
_ => panic ! ( "can't make docs for trait item with name {:?}" , item. name)
@@ -2545,9 +2571,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
2545
2571
}
2546
2572
2547
2573
// If we've implemented a trait, then also emit documentation for all
2548
- // default methods which weren't overridden in the implementation block.
2549
- // FIXME: this also needs to be done for associated types, whenever defaults
2550
- // for them work.
2574
+ // default items which weren't overridden in the implementation block.
2551
2575
if let Some ( did) = i. trait_did ( ) {
2552
2576
if let Some ( t) = cache ( ) . traits . get ( & did) {
2553
2577
render_default_items ( w, cx, t, & i. impl_ , render_header, outer_version) ?;
0 commit comments