@@ -24,6 +24,20 @@ function takeFromArray(array, count) {
24
24
return result ;
25
25
}
26
26
27
+ function onEachBtwn ( arr , func , funcBtwn ) {
28
+ let i = 0 ;
29
+ for ( const value of arr ) {
30
+ if ( i !== 0 ) {
31
+ funcBtwn ( value , i ) ;
32
+ }
33
+ if ( func ( value , i ) ) {
34
+ return true ;
35
+ }
36
+ i += 1 ;
37
+ }
38
+ return false ;
39
+ }
40
+
27
41
( function ( ) {
28
42
// This mapping table should match the discriminants of
29
43
// `rustdoc::formats::item_type::ItemType` type in Rust.
@@ -1575,6 +1589,49 @@ function initSearch(rawSearchIndex) {
1575
1589
pushText ( { name : ")" , highlighted : false } , result ) ;
1576
1590
}
1577
1591
}
1592
+ /**
1593
+ * Write a primitive type with special syntax, like `!` or `[T]`.
1594
+ * Returns `false` if the supplied type isn't special.
1595
+ *
1596
+ * @param {FunctionType } fnType
1597
+ * @param {[string] } result
1598
+ */
1599
+ function writeSpecialPrimitive ( fnType , result ) {
1600
+ if ( fnType . id === typeNameIdOfArray || fnType . id === typeNameIdOfSlice ||
1601
+ fnType . id === typeNameIdOfTuple || fnType . id === typeNameIdOfUnit ) {
1602
+ const [ ob , sb ] =
1603
+ fnType . id === typeNameIdOfArray || fnType . id === typeNameIdOfSlice ?
1604
+ [ "[" , "]" ] :
1605
+ [ "(" , ")" ] ;
1606
+ pushText ( { name : ob , highlighted : fnType . highlighted } , result ) ;
1607
+ onEachBtwn (
1608
+ fnType . generics ,
1609
+ nested => writeFn ( nested , result ) ,
1610
+ ( ) => pushText ( { name : ", " , highlighted : false } , result ) ,
1611
+ ) ;
1612
+ pushText ( { name : sb , highlighted : fnType . highlighted } , result ) ;
1613
+ return true ;
1614
+ } else if ( fnType . id === typeNameIdOfReference ) {
1615
+ pushText ( { name : "&" , highlighted : fnType . highlighted } , result ) ;
1616
+ let prevHighlighted = false ;
1617
+ onEachBtwn (
1618
+ fnType . generics ,
1619
+ value => {
1620
+ prevHighlighted = value . highlighted ;
1621
+ writeFn ( value , result ) ;
1622
+ } ,
1623
+ value => pushText ( {
1624
+ name : " " ,
1625
+ highlighted : prevHighlighted && value . highlighted ,
1626
+ } , result ) ,
1627
+ ) ;
1628
+ return true ;
1629
+ } else if ( fnType . id === typeNameIdOfFn ) {
1630
+ writeHof ( fnType , result ) ;
1631
+ return true ;
1632
+ }
1633
+ return false ;
1634
+ }
1578
1635
/**
1579
1636
* Write a type. This function checks for special types,
1580
1637
* like slices, with their own formatting. It also handles
@@ -1602,55 +1659,17 @@ function initSearch(rawSearchIndex) {
1602
1659
highlighted : ! ! fnType . highlighted ,
1603
1660
} , result ) ;
1604
1661
const where = [ ] ;
1605
- let first = true ;
1606
- for ( const nested of fnType . generics ) {
1607
- if ( first ) {
1608
- first = false ;
1609
- } else {
1610
- pushText ( { name : " + " , highlighted : false } , where ) ;
1611
- }
1612
- writeFn ( nested , where ) ;
1613
- }
1662
+ onEachBtwn (
1663
+ fnType . generics ,
1664
+ nested => writeFn ( nested , where ) ,
1665
+ ( ) => pushText ( { name : " + " , highlighted : false } , where ) ,
1666
+ ) ;
1614
1667
if ( where . length > 0 ) {
1615
1668
whereClause . set ( fnParamNames [ - 1 - fnType . id ] , where ) ;
1616
1669
}
1617
1670
} else {
1618
1671
if ( fnType . ty === TY_PRIMITIVE ) {
1619
- if ( fnType . id === typeNameIdOfArray || fnType . id === typeNameIdOfSlice ||
1620
- fnType . id === typeNameIdOfTuple || fnType . id === typeNameIdOfUnit ) {
1621
- const [ ob , sb ] =
1622
- fnType . id === typeNameIdOfArray || fnType . id === typeNameIdOfSlice ?
1623
- [ "[" , "]" ] :
1624
- [ "(" , ")" ] ;
1625
- pushText ( { name : ob , highlighted : fnType . highlighted } , result ) ;
1626
- let needsComma = false ;
1627
- for ( const value of fnType . generics ) {
1628
- if ( needsComma ) {
1629
- pushText ( { name : ", " , highlighted : false } , result ) ;
1630
- }
1631
- needsComma = true ;
1632
- writeFn ( value , result ) ;
1633
- }
1634
- pushText ( { name : sb , highlighted : fnType . highlighted } , result ) ;
1635
- return ;
1636
- } else if ( fnType . id === typeNameIdOfReference ) {
1637
- pushText ( { name : "&" , highlighted : fnType . highlighted } , result ) ;
1638
- let needsSpace = false ;
1639
- let prevHighlighted = false ;
1640
- for ( const value of fnType . generics ) {
1641
- if ( needsSpace ) {
1642
- pushText ( {
1643
- name : " " ,
1644
- highlighted : prevHighlighted && value . highlighted ,
1645
- } , result ) ;
1646
- }
1647
- needsSpace = true ;
1648
- prevHighlighted = value . highlighted ;
1649
- writeFn ( value , result ) ;
1650
- }
1651
- return ;
1652
- } else if ( fnType . id === typeNameIdOfFn ) {
1653
- writeHof ( fnType , result ) ;
1672
+ if ( writeSpecialPrimitive ( fnType , result ) ) {
1654
1673
return ;
1655
1674
}
1656
1675
} else if ( fnType . ty === TY_TRAIT &&
@@ -1661,66 +1680,55 @@ function initSearch(rawSearchIndex) {
1661
1680
return ;
1662
1681
}
1663
1682
pushText ( fnType , result ) ;
1664
- if ( fnType . bindings . length > 0 || fnType . generics . length > 0 ) {
1683
+ if ( fnType . bindings . size > 0 || fnType . generics . length > 0 ) {
1665
1684
pushText ( { name : "<" , highlighted : false } , result ) ;
1666
1685
}
1667
- let needsComma = false ;
1668
- if ( fnType . bindings . length > 0 ) {
1669
- for ( const [ key , values ] of fnType . bindings ) {
1670
- if ( needsComma ) {
1671
- pushText ( { name : ", " , highlighted : false } , result ) ;
1672
- }
1673
- needsComma = true ;
1674
- writeFn ( key , result ) ;
1675
- pushText ( {
1676
- name : values . length > 1 ? "=(" : "=" ,
1677
- highlighted : false ,
1678
- } , result ) ;
1679
- let needsPlus = false ;
1680
- for ( const value of values ) {
1681
- if ( needsPlus ) {
1682
- pushText ( { name : " + " , highlighted : false } , result ) ;
1686
+ if ( fnType . bindings . size > 0 ) {
1687
+ onEachBtwn (
1688
+ fnType . bindings ,
1689
+ ( [ key , values ] ) => {
1690
+ pushText ( key , result ) ;
1691
+ pushText ( {
1692
+ name : values . length > 1 ? "=(" : "=" ,
1693
+ highlighted : false ,
1694
+ } , result ) ;
1695
+ onEachBtwn (
1696
+ values ,
1697
+ value => writeFn ( value , result ) ,
1698
+ ( ) => pushText ( { name : " + " , highlighted : false } , result ) ,
1699
+ ) ;
1700
+ if ( values . length > 1 ) {
1701
+ pushText ( { name : ") " , highlighted : false } , result ) ;
1683
1702
}
1684
- needsPlus = true ;
1685
- writeFn ( value , result ) ;
1686
- }
1687
- if ( values . length > 1 ) {
1688
- pushText ( { name : ")" , highlighted : false } , result ) ;
1689
- }
1690
- }
1703
+ } ,
1704
+ ( ) => pushText ( { name : ", " , highlighted : false } , result ) ,
1705
+ ) ;
1691
1706
}
1692
- if ( fnType . generics . length > 0 ) {
1693
- for ( const value of fnType . generics ) {
1694
- if ( needsComma ) {
1695
- pushText ( { name : ", " , highlighted : false } , result ) ;
1696
- }
1697
- needsComma = true ;
1698
- writeFn ( value , result ) ;
1699
- }
1707
+ if ( fnType . bindings . size > 0 && fnType . generics . length > 0 ) {
1708
+ pushText ( { name : ", " , highlighted : false } , result ) ;
1700
1709
}
1701
- if ( fnType . bindings . length > 0 || fnType . generics . length > 0 ) {
1710
+ onEachBtwn (
1711
+ fnType . generics ,
1712
+ value => writeFn ( value , result ) ,
1713
+ ( ) => pushText ( { name : ", " , highlighted : false } , result ) ,
1714
+ ) ;
1715
+ if ( fnType . bindings . size > 0 || fnType . generics . length > 0 ) {
1702
1716
pushText ( { name : ">" , highlighted : false } , result ) ;
1703
1717
}
1704
1718
}
1705
1719
}
1706
1720
const type = [ ] ;
1707
- let needsComma = false ;
1708
- for ( const fnType of fnInputs ) {
1709
- if ( needsComma ) {
1710
- pushText ( { name : ", " , highlighted : false } , type ) ;
1711
- }
1712
- needsComma = true ;
1713
- writeFn ( fnType , type ) ;
1714
- }
1721
+ onEachBtwn (
1722
+ fnInputs ,
1723
+ fnType => writeFn ( fnType , type ) ,
1724
+ ( ) => pushText ( { name : ", " , highlighted : false } , type ) ,
1725
+ ) ;
1715
1726
pushText ( { name : " -> " , highlighted : false } , type ) ;
1716
- needsComma = false ;
1717
- for ( const fnType of fnOutput ) {
1718
- if ( needsComma ) {
1719
- pushText ( { name : ", " , highlighted : false } , type ) ;
1720
- }
1721
- needsComma = true ;
1722
- writeFn ( fnType , type ) ;
1723
- }
1727
+ onEachBtwn (
1728
+ fnOutput ,
1729
+ fnType => writeFn ( fnType , type ) ,
1730
+ ( ) => pushText ( { name : ", " , highlighted : false } , type ) ,
1731
+ ) ;
1724
1732
1725
1733
return { type, mappedNames, whereClause} ;
1726
1734
}
@@ -3074,7 +3082,9 @@ function initSearch(rawSearchIndex) {
3074
3082
async function addTab ( array , query , display ) {
3075
3083
const extraClass = display ? " active" : "" ;
3076
3084
3077
- let output = document . createElement ( "ul" ) ;
3085
+ const output = document . createElement (
3086
+ array . length === 0 && query . error === null ? "div" : "ul" ,
3087
+ ) ;
3078
3088
if ( array . length > 0 ) {
3079
3089
output . className = "search-results " + extraClass ;
3080
3090
@@ -3193,7 +3203,6 @@ ${item.displayPath}<span class="${type}">${name}</span>\
3193
3203
}
3194
3204
} ) ;
3195
3205
} else if ( query . error === null ) {
3196
- output = document . createElement ( "div" ) ;
3197
3206
output . className = "search-failed" + extraClass ;
3198
3207
output . innerHTML = "No results :(<br/>" +
3199
3208
"Try on <a href=\"https://duckduckgo.com/?q=" +
0 commit comments