@@ -1278,94 +1278,40 @@ fn item_type_alias(cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) ->
1278
1278
1279
1279
match inner_type {
1280
1280
clean:: TypeAliasInnerType :: Enum { variants, is_non_exhaustive } => {
1281
- let variants_iter = || variants. iter ( ) . filter ( |i| !i. is_stripped ( ) ) ;
1282
1281
let ty = cx. tcx ( ) . type_of ( it. def_id ( ) . unwrap ( ) ) . instantiate_identity ( ) ;
1283
1282
let enum_def_id = ty. ty_adt_def ( ) . unwrap ( ) . did ( ) ;
1284
1283
1285
- wrap_item ( w, |w| {
1286
- let variants_len = variants. len ( ) ;
1287
- let variants_count = variants_iter ( ) . count ( ) ;
1288
- let has_stripped_entries = variants_len != variants_count;
1289
-
1290
- write ! (
1291
- w,
1292
- "enum {}{}{}" ,
1293
- it. name. unwrap( ) ,
1294
- t. generics. print( cx) ,
1295
- render_enum_fields(
1296
- cx,
1297
- Some ( & t. generics) ,
1298
- variants,
1299
- variants_count,
1300
- has_stripped_entries,
1301
- * is_non_exhaustive,
1302
- enum_def_id,
1303
- )
1304
- )
1305
- } ) ?;
1306
- write ! ( w, "{}" , item_variants( cx, it, variants, enum_def_id) ) ?;
1284
+ DisplayEnum {
1285
+ variants,
1286
+ generics : & t. generics ,
1287
+ is_non_exhaustive : * is_non_exhaustive,
1288
+ def_id : enum_def_id,
1289
+ }
1290
+ . render_into ( cx, it, false , w) ?;
1307
1291
}
1308
1292
clean:: TypeAliasInnerType :: Union { fields } => {
1309
- wrap_item ( w, |w| {
1310
- let fields_count = fields. iter ( ) . filter ( |i| !i. is_stripped ( ) ) . count ( ) ;
1311
- let has_stripped_fields = fields. len ( ) != fields_count;
1312
-
1313
- write ! (
1314
- w,
1315
- "union {}{}{}" ,
1316
- it. name. unwrap( ) ,
1317
- t. generics. print( cx) ,
1318
- render_struct_fields(
1319
- Some ( & t. generics) ,
1320
- None ,
1321
- fields,
1322
- "" ,
1323
- true ,
1324
- has_stripped_fields,
1325
- cx,
1326
- ) ,
1327
- )
1328
- } ) ?;
1329
- write ! ( w, "{}" , item_fields( cx, it, fields, None ) ) ?;
1293
+ ItemUnion { cx, it, fields, generics : & t. generics , document_union : false }
1294
+ . render_into ( w) ?;
1330
1295
}
1331
1296
clean:: TypeAliasInnerType :: Struct { ctor_kind, fields } => {
1332
- wrap_item ( w, |w| {
1333
- let fields_count = fields. iter ( ) . filter ( |i| !i. is_stripped ( ) ) . count ( ) ;
1334
- let has_stripped_fields = fields. len ( ) != fields_count;
1335
-
1336
- write ! (
1337
- w,
1338
- "struct {}{}{}" ,
1339
- it. name. unwrap( ) ,
1340
- t. generics. print( cx) ,
1341
- render_struct_fields(
1342
- Some ( & t. generics) ,
1343
- * ctor_kind,
1344
- fields,
1345
- "" ,
1346
- true ,
1347
- has_stripped_fields,
1348
- cx,
1349
- ) ,
1350
- )
1351
- } ) ?;
1352
- write ! ( w, "{}" , item_fields( cx, it, fields, None ) ) ?;
1297
+ DisplayStruct { ctor_kind : * ctor_kind, generics : & t. generics , fields }
1298
+ . render_into ( cx, it, false , w) ?;
1353
1299
}
1354
1300
}
1301
+ } else {
1302
+ let def_id = it. item_id . expect_def_id ( ) ;
1303
+ // Render any items associated directly to this alias, as otherwise they
1304
+ // won't be visible anywhere in the docs. It would be nice to also show
1305
+ // associated items from the aliased type (see discussion in #32077), but
1306
+ // we need #14072 to make sense of the generics.
1307
+ write ! (
1308
+ w,
1309
+ "{}{}" ,
1310
+ render_assoc_items( cx, it, def_id, AssocItemRender :: All ) ,
1311
+ document_type_layout( cx, def_id)
1312
+ ) ?;
1355
1313
}
1356
1314
1357
- let def_id = it. item_id . expect_def_id ( ) ;
1358
- // Render any items associated directly to this alias, as otherwise they
1359
- // won't be visible anywhere in the docs. It would be nice to also show
1360
- // associated items from the aliased type (see discussion in #32077), but
1361
- // we need #14072 to make sense of the generics.
1362
- write ! (
1363
- w,
1364
- "{}{}" ,
1365
- render_assoc_items( cx, it, def_id, AssocItemRender :: All ) ,
1366
- document_type_layout( cx, def_id)
1367
- ) ?;
1368
-
1369
1315
// [RUSTDOCIMPL] type.impl
1370
1316
//
1371
1317
// Include type definitions from the alias target type.
@@ -1463,50 +1409,53 @@ fn item_type_alias(cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) ->
1463
1409
} )
1464
1410
}
1465
1411
1466
- fn item_union ( cx : & Context < ' _ > , it : & clean:: Item , s : & clean:: Union ) -> impl fmt:: Display {
1467
- item_template ! (
1468
- #[ template( path = "item_union.html" ) ]
1469
- struct ItemUnion <' a, ' cx> {
1470
- cx: & ' a Context <' cx>,
1471
- it: & ' a clean:: Item ,
1472
- s: & ' a clean:: Union ,
1473
- } ,
1474
- methods = [ document, document_type_layout, render_attributes_in_pre, render_assoc_items]
1475
- ) ;
1476
-
1477
- impl < ' a , ' cx : ' a > ItemUnion < ' a , ' cx > {
1478
- fn render_union ( & self ) -> impl Display {
1479
- render_union ( self . it , Some ( & self . s . generics ) , & self . s . fields , self . cx )
1480
- }
1412
+ item_template ! (
1413
+ #[ template( path = "item_union.html" ) ]
1414
+ struct ItemUnion <' a, ' cx> {
1415
+ cx: & ' a Context <' cx>,
1416
+ it: & ' a clean:: Item ,
1417
+ fields: & ' a [ clean:: Item ] ,
1418
+ generics: & ' a clean:: Generics ,
1419
+ document_union: bool ,
1420
+ } ,
1421
+ methods = [ document, document_type_layout, render_attributes_in_pre, render_assoc_items]
1422
+ ) ;
1423
+
1424
+ impl < ' a , ' cx : ' a > ItemUnion < ' a , ' cx > {
1425
+ fn render_union ( & self ) -> impl Display {
1426
+ render_union ( self . it , Some ( & self . generics ) , & self . fields , self . cx )
1427
+ }
1481
1428
1482
- fn document_field ( & self , field : & ' a clean:: Item ) -> impl Display {
1483
- document ( self . cx , field, Some ( self . it ) , HeadingOffset :: H3 )
1484
- }
1429
+ fn document_field ( & self , field : & ' a clean:: Item ) -> impl Display {
1430
+ document ( self . cx , field, Some ( self . it ) , HeadingOffset :: H3 )
1431
+ }
1485
1432
1486
- fn stability_field ( & self , field : & clean:: Item ) -> Option < String > {
1487
- field. stability_class ( self . cx . tcx ( ) )
1488
- }
1433
+ fn stability_field ( & self , field : & clean:: Item ) -> Option < String > {
1434
+ field. stability_class ( self . cx . tcx ( ) )
1435
+ }
1489
1436
1490
- fn print_ty ( & self , ty : & ' a clean:: Type ) -> impl Display {
1491
- ty. print ( self . cx )
1492
- }
1437
+ fn print_ty ( & self , ty : & ' a clean:: Type ) -> impl Display {
1438
+ ty. print ( self . cx )
1439
+ }
1493
1440
1494
- fn fields_iter (
1495
- & self ,
1496
- ) -> iter:: Peekable < impl Iterator < Item = ( & ' a clean:: Item , & ' a clean:: Type ) > > {
1497
- self . s
1498
- . fields
1499
- . iter ( )
1500
- . filter_map ( |f| match f. kind {
1501
- clean:: StructFieldItem ( ref ty) => Some ( ( f, ty) ) ,
1502
- _ => None ,
1503
- } )
1504
- . peekable ( )
1505
- }
1441
+ fn fields_iter (
1442
+ & self ,
1443
+ ) -> iter:: Peekable < impl Iterator < Item = ( & ' a clean:: Item , & ' a clean:: Type ) > > {
1444
+ self . fields
1445
+ . iter ( )
1446
+ . filter_map ( |f| match f. kind {
1447
+ clean:: StructFieldItem ( ref ty) => Some ( ( f, ty) ) ,
1448
+ _ => None ,
1449
+ } )
1450
+ . peekable ( )
1506
1451
}
1452
+ }
1507
1453
1454
+ fn item_union ( cx : & Context < ' _ > , it : & clean:: Item , s : & clean:: Union ) -> impl fmt:: Display {
1508
1455
fmt:: from_fn ( |w| {
1509
- ItemUnion { cx, it, s } . render_into ( w) . unwrap ( ) ;
1456
+ ItemUnion { cx, it, fields : & s. fields , generics : & s. generics , document_union : true }
1457
+ . render_into ( w)
1458
+ . unwrap ( ) ;
1510
1459
Ok ( ( ) )
1511
1460
} )
1512
1461
}
@@ -1533,33 +1482,51 @@ fn print_tuple_struct_fields(cx: &Context<'_>, s: &[clean::Item]) -> impl Displa
1533
1482
} )
1534
1483
}
1535
1484
1536
- fn item_enum ( cx : & Context < ' _ > , it : & clean:: Item , e : & clean:: Enum ) -> impl fmt:: Display {
1537
- fmt:: from_fn ( |w| {
1538
- let count_variants = e. non_stripped_variants ( ) . count ( ) ;
1485
+ struct DisplayEnum < ' a > {
1486
+ variants : & ' a IndexVec < VariantIdx , clean:: Item > ,
1487
+ generics : & ' a clean:: Generics ,
1488
+ is_non_exhaustive : bool ,
1489
+ def_id : DefId ,
1490
+ }
1491
+
1492
+ impl < ' a > DisplayEnum < ' a > {
1493
+ fn render_into < W : fmt:: Write > (
1494
+ self ,
1495
+ cx : & Context < ' _ > ,
1496
+ it : & clean:: Item ,
1497
+ document_enum : bool ,
1498
+ w : & mut W ,
1499
+ ) -> fmt:: Result {
1500
+ let variants_count = self . variants . iter ( ) . filter ( |i| !i. is_stripped ( ) ) . count ( ) ;
1501
+ let variants_len = self . variants . len ( ) ;
1502
+ let has_stripped_entries = variants_len != variants_count;
1503
+
1539
1504
wrap_item ( w, |w| {
1540
1505
render_attributes_in_code ( w, it, cx) ;
1541
1506
write ! (
1542
1507
w,
1543
1508
"{}enum {}{}{}" ,
1544
1509
visibility_print_with_space( it, cx) ,
1545
1510
it. name. unwrap( ) ,
1546
- e . generics. print( cx) ,
1511
+ self . generics. print( cx) ,
1547
1512
render_enum_fields(
1548
1513
cx,
1549
- Some ( & e . generics) ,
1550
- & e . variants,
1551
- count_variants ,
1552
- e . has_stripped_entries( ) ,
1553
- it . is_non_exhaustive( ) ,
1554
- it . def_id( ) . unwrap ( ) ,
1514
+ Some ( self . generics) ,
1515
+ self . variants,
1516
+ variants_count ,
1517
+ has_stripped_entries,
1518
+ self . is_non_exhaustive,
1519
+ self . def_id,
1555
1520
) ,
1556
1521
)
1557
1522
} ) ?;
1558
1523
1559
- write ! ( w, "{}" , document( cx, it, None , HeadingOffset :: H2 ) ) ?;
1524
+ if document_enum {
1525
+ write ! ( w, "{}" , document( cx, it, None , HeadingOffset :: H2 ) ) ?;
1526
+ }
1560
1527
1561
- if count_variants != 0 {
1562
- write ! ( w, "{}" , item_variants( cx, it, & e . variants, it . def_id( ) . unwrap ( ) ) ) ?;
1528
+ if variants_count != 0 {
1529
+ write ! ( w, "{}" , item_variants( cx, it, self . variants, self . def_id) ) ?;
1563
1530
}
1564
1531
let def_id = it. item_id . expect_def_id ( ) ;
1565
1532
write ! (
@@ -1568,6 +1535,18 @@ fn item_enum(cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) -> impl fmt::D
1568
1535
render_assoc_items( cx, it, def_id, AssocItemRender :: All ) ,
1569
1536
document_type_layout( cx, def_id)
1570
1537
)
1538
+ }
1539
+ }
1540
+
1541
+ fn item_enum ( cx : & Context < ' _ > , it : & clean:: Item , e : & clean:: Enum ) -> impl fmt:: Display {
1542
+ fmt:: from_fn ( |w| {
1543
+ DisplayEnum {
1544
+ variants : & e. variants ,
1545
+ generics : & e. generics ,
1546
+ is_non_exhaustive : it. is_non_exhaustive ( ) ,
1547
+ def_id : it. def_id ( ) . unwrap ( ) ,
1548
+ }
1549
+ . render_into ( cx, it, true , w)
1571
1550
} )
1572
1551
}
1573
1552
@@ -1955,27 +1934,48 @@ fn item_constant(
1955
1934
} )
1956
1935
}
1957
1936
1958
- fn item_struct ( cx : & Context < ' _ > , it : & clean:: Item , s : & clean:: Struct ) -> impl fmt:: Display {
1959
- fmt:: from_fn ( |w| {
1937
+ struct DisplayStruct < ' a > {
1938
+ ctor_kind : Option < CtorKind > ,
1939
+ generics : & ' a clean:: Generics ,
1940
+ fields : & ' a [ clean:: Item ] ,
1941
+ }
1942
+
1943
+ impl < ' a > DisplayStruct < ' a > {
1944
+ fn render_into < W : fmt:: Write > (
1945
+ self ,
1946
+ cx : & Context < ' _ > ,
1947
+ it : & clean:: Item ,
1948
+ document_struct : bool ,
1949
+ w : & mut W ,
1950
+ ) -> fmt:: Result {
1960
1951
wrap_item ( w, |w| {
1961
1952
render_attributes_in_code ( w, it, cx) ;
1962
1953
write ! (
1963
1954
w,
1964
1955
"{}" ,
1965
- render_struct( it, Some ( & s . generics) , s . ctor_kind, & s . fields, "" , true , cx)
1956
+ render_struct( it, Some ( self . generics) , self . ctor_kind, self . fields, "" , true , cx)
1966
1957
)
1967
1958
} ) ?;
1968
1959
1969
- let def_id = it. item_id . expect_def_id ( ) ;
1960
+ if document_struct {
1961
+ write ! ( w, "{}" , document( cx, it, None , HeadingOffset :: H2 ) ) ?;
1962
+ }
1970
1963
1964
+ let def_id = it. item_id . expect_def_id ( ) ;
1971
1965
write ! (
1972
1966
w,
1973
- "{}{}{}{}" ,
1974
- document( cx, it, None , HeadingOffset :: H2 ) ,
1975
- item_fields( cx, it, & s. fields, s. ctor_kind) ,
1967
+ "{}{}{}" ,
1968
+ item_fields( cx, it, self . fields, self . ctor_kind) ,
1976
1969
render_assoc_items( cx, it, def_id, AssocItemRender :: All ) ,
1977
1970
document_type_layout( cx, def_id) ,
1978
1971
)
1972
+ }
1973
+ }
1974
+
1975
+ fn item_struct ( cx : & Context < ' _ > , it : & clean:: Item , s : & clean:: Struct ) -> impl fmt:: Display {
1976
+ fmt:: from_fn ( |w| {
1977
+ DisplayStruct { ctor_kind : s. ctor_kind , generics : & s. generics , fields : s. fields . as_slice ( ) }
1978
+ . render_into ( cx, it, true , w)
1979
1979
} )
1980
1980
}
1981
1981
0 commit comments