Skip to content

Commit 8081096

Browse files
committed
Add documentation
1 parent 5deb396 commit 8081096

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/librustdoc/html/static/js/externs.js

+53
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,56 @@ let ResultsTable;
8181
* }}
8282
*/
8383
let Results;
84+
85+
/**
86+
* A pair of [inputs, outputs], or 0 for null. This is gets stored in the search index.
87+
* The JavaScript deserializes this into FunctionSearchType.
88+
*
89+
* An input or output can be encoded as just a number if there is only one of them, AND
90+
* it has no generics. The no generics rule exists to avoid ambiguity: imagine if you had
91+
* a function with a single output, and that output had a single generic:
92+
*
93+
* fn something() -> Result<usize, usize>
94+
*
95+
* If output was allowed to be any RawFunctionType, it would look like this
96+
*
97+
* [[], [50, [3, 3]]]
98+
*
99+
* The problem is that the above output could be interpreted as either a type with ID 50 and two
100+
* generics, or it could be interpreted as a pair of types, the first one with ID 50 and the second
101+
* with ID 3 and a single generic parameter that is also ID 3. We avoid this ambiguity by choosing
102+
* in favor of the pair of types interpretation. This is why the `(number|Array<RawFunctionType>)`
103+
* is used instead of `(RawFunctionType|Array<RawFunctionType>)`.
104+
*
105+
* @typedef {(
106+
* 0 |
107+
* [(number|Array<RawFunctionType>)] |
108+
* [(number|Array<RawFunctionType>), (number|Array<RawFunctionType>)]
109+
* )}
110+
*/
111+
let RawFunctionSearchType;
112+
113+
/**
114+
* A single function input or output type. This is either a single path ID, or a pair of
115+
* [path ID, generics].
116+
*
117+
* @typedef {number | [number, Array<RawFunctionType>]}
118+
*/
119+
let RawFunctionType;
120+
121+
/**
122+
* @typedef {{
123+
* inputs: Array<FunctionType>,
124+
* outputs: Array<FunctionType>,
125+
* }}
126+
*/
127+
let FunctionSearchType;
128+
129+
/**
130+
* @typedef {{
131+
* name: (null|string),
132+
* ty: (null|number),
133+
* generics: Array<FunctionType>,
134+
* }}
135+
*/
136+
let FunctionType;

src/librustdoc/html/static/js/search.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,24 @@ function initSearch(rawSearchIndex) {
18251825
filterCrates);
18261826
}
18271827

1828+
/**
1829+
* Convert a list of RawFunctionType / ID to object-based FunctionType.
1830+
*
1831+
* Crates often have lots of functions in them, and it's common to have a large number of
1832+
* functions that operate on a small set of data types, so the search index compresses them
1833+
* by encoding function parameter and return types as indexes into an array of names.
1834+
*
1835+
* Even when a general-purpose compression algorithm is used, this is still a win. I checked.
1836+
* https://github.com/rust-lang/rust/pull/98475#issue-1284395985
1837+
*
1838+
* The format for individual function types is encoded in
1839+
* librustdoc/html/render/mod.rs: impl Serialize for RenderType
1840+
*
1841+
* @param {null|Array<RawFunctionType>} types
1842+
* @param {Array<{name: string, ty: number}>} lowercasePaths
1843+
*
1844+
* @return {Array<FunctionSearchType>}
1845+
*/
18281846
function buildItemSearchTypeAll(types, lowercasePaths) {
18291847
const PATH_INDEX_DATA = 0;
18301848
const GENERICS_DATA = 1;
@@ -1848,6 +1866,21 @@ function initSearch(rawSearchIndex) {
18481866
});
18491867
}
18501868

1869+
/**
1870+
* Convert from RawFunctionSearchType to FunctionSearchType.
1871+
*
1872+
* Crates often have lots of functions in them, and function signatures are sometimes complex,
1873+
* so rustdoc uses a pretty tight encoding for them. This function converts it to a simpler,
1874+
* object-based encoding so that the actual search code is more readable and easier to debug.
1875+
*
1876+
* The raw function search type format is generated using serde in
1877+
* librustdoc/html/render/mod.rs: impl Serialize for IndexItemFunctionType
1878+
*
1879+
* @param {RawFunctionSearchType} functionSearchType
1880+
* @param {Array<{name: string, ty: number}>} lowercasePaths
1881+
*
1882+
* @return {null|FunctionSearchType}
1883+
*/
18511884
function buildFunctionSearchType(functionSearchType, lowercasePaths) {
18521885
const INPUTS_DATA = 0;
18531886
const OUTPUT_DATA = 1;
@@ -1935,7 +1968,7 @@ function initSearch(rawSearchIndex) {
19351968
* d: Array<string>,
19361969
* q: Array<string>,
19371970
* i: Array<Number>,
1938-
* f: Array<0 | Object>,
1971+
* f: Array<RawFunctionSearchType>,
19391972
* p: Array<Object>,
19401973
* }}
19411974
*/

0 commit comments

Comments
 (0)