2 /* global ALIASES, currentCrate, rootPath */
4 // Local js definitions:
5 /* global addClass, getCurrentValue, hasClass */
6 /* global isHidden, onEach, removeClass, updateLocalStorage */
8 if (!String.prototype.startsWith) {
9 String.prototype.startsWith = function(searchString, position) {
10 position = position || 0;
11 return this.indexOf(searchString, position) === position;
14 if (!String.prototype.endsWith) {
15 String.prototype.endsWith = function(suffix, length) {
16 var l = length || this.length;
17 return this.indexOf(suffix, l - suffix.length) !== -1;
21 if (!DOMTokenList.prototype.add) {
22 DOMTokenList.prototype.add = function(className) {
23 if (className && !hasClass(this, className)) {
24 if (this.className && this.className.length > 0) {
25 this.className += " " + className;
27 this.className = className;
33 if (!DOMTokenList.prototype.remove) {
34 DOMTokenList.prototype.remove = function(className) {
35 if (className && this.className) {
36 this.className = (" " + this.className + " ").replace(" " + className + " ", " ")
45 // This mapping table should match the discriminants of
46 // `rustdoc::html::item_type::ItemType` type in Rust.
47 var itemTypes = ["mod",
74 var search_input = document.getElementsByClassName("search-input")[0];
76 // On the search screen, so you remain on the last tab you opened.
79 // 1 for "In Parameters"
80 // 2 for "In Return Types"
83 var titleBeforeSearch = document.title;
85 function getPageId() {
86 var id = document.location.href.split("#")[1];
88 return id.split("?")[0].split("&")[0];
93 function showSidebar() {
94 var elems = document.getElementsByClassName("sidebar-elems")[0];
96 addClass(elems, "show-it");
98 var sidebar = document.getElementsByClassName("sidebar")[0];
100 addClass(sidebar, "mobile");
101 var filler = document.getElementById("sidebar-filler");
103 var div = document.createElement("div");
104 div.id = "sidebar-filler";
105 sidebar.appendChild(div);
108 var themePickers = document.getElementsByClassName("theme-picker");
109 if (themePickers && themePickers.length > 0) {
110 themePickers[0].style.display = "none";
114 function hideSidebar() {
115 var elems = document.getElementsByClassName("sidebar-elems")[0];
117 removeClass(elems, "show-it");
119 var sidebar = document.getElementsByClassName("sidebar")[0];
120 removeClass(sidebar, "mobile");
121 var filler = document.getElementById("sidebar-filler");
125 document.getElementsByTagName("body")[0].style.marginTop = "";
126 var themePickers = document.getElementsByClassName("theme-picker");
127 if (themePickers && themePickers.length > 0) {
128 themePickers[0].style.display = null;
132 // used for special search precedence
133 var TY_PRIMITIVE = itemTypes.indexOf("primitive");
134 var TY_KEYWORD = itemTypes.indexOf("keyword");
136 onEachLazy(document.getElementsByClassName("js-only"), function(e) {
137 removeClass(e, "js-only");
140 function getQueryStringParams() {
142 window.location.search.substring(1).split("&").
144 var pair = s.split("=");
145 params[decodeURIComponent(pair[0])] =
146 typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
151 function browserSupportsHistoryApi() {
152 return window.history && typeof window.history.pushState === "function";
155 var main = document.getElementById("main");
157 function highlightSourceLines(ev) {
158 // If we're in mobile mode, we should add the sidebar in any case.
161 var search = document.getElementById("search");
162 var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
164 from = parseInt(match[1], 10);
166 if (typeof match[2] !== "undefined") {
167 to = parseInt(match[2], 10);
174 elem = document.getElementById(from);
179 var x = document.getElementById(from);
184 onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
185 onEachLazy(e.getElementsByTagName("span"), function(i_e) {
186 removeClass(i_e, "line-highlighted");
189 for (i = from; i <= to; ++i) {
190 elem = document.getElementById(i);
194 addClass(elem, "line-highlighted");
196 } else if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
197 addClass(search, "hidden");
198 removeClass(main, "hidden");
199 var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
200 if (browserSupportsHistoryApi()) {
201 history.replaceState(hash, "", "?search=#" + hash);
203 elem = document.getElementById(hash);
205 elem.scrollIntoView();
210 function expandSection(id) {
211 var elem = document.getElementById(id);
212 if (elem && isHidden(elem)) {
213 var h3 = elem.parentNode.previousElementSibling;
214 if (h3 && h3.tagName !== "H3") {
215 h3 = h3.previousElementSibling; // skip div.docblock
219 var collapses = h3.getElementsByClassName("collapse-toggle");
220 if (collapses.length > 0) {
221 // The element is not visible, we need to make it appear!
222 collapseDocs(collapses[0], "show");
228 highlightSourceLines(null);
229 window.onhashchange = highlightSourceLines;
231 // Gets the human-readable string for the virtual-key code of the
232 // given KeyboardEvent, ev.
234 // This function is meant as a polyfill for KeyboardEvent#key,
235 // since it is not supported in Trident. We also test for
236 // KeyboardEvent#keyCode because the handleShortcut handler is
237 // also registered for the keydown event, because Blink doesn't fire
238 // keypress on hitting the Escape key.
240 // So I guess you could say things are getting pretty interoperable.
241 function getVirtualKey(ev) {
242 if ("key" in ev && typeof ev.key != "undefined") {
246 var c = ev.charCode || ev.keyCode;
250 return String.fromCharCode(c);
253 function displayHelp(display, ev, help) {
254 if (display === true) {
255 if (hasClass(help, "hidden")) {
257 removeClass(help, "hidden");
258 addClass(document.body, "blur");
260 } else if (hasClass(help, "hidden") === false) {
262 addClass(help, "hidden");
263 removeClass(document.body, "blur");
267 function handleEscape(ev, help) {
269 var search = document.getElementById("search");
270 if (hasClass(help, "hidden") === false) {
271 displayHelp(false, ev, help);
272 } else if (hasClass(search, "hidden") === false) {
274 addClass(search, "hidden");
275 removeClass(main, "hidden");
276 document.title = titleBeforeSearch;
281 function handleShortcut(ev) {
282 // Don't interfere with browser shortcuts
283 if (ev.ctrlKey || ev.altKey || ev.metaKey) {
287 var help = document.getElementById("help");
288 if (document.activeElement.tagName === "INPUT") {
289 switch (getVirtualKey(ev)) {
291 handleEscape(ev, help);
295 switch (getVirtualKey(ev)) {
297 handleEscape(ev, help);
302 displayHelp(false, ev, help);
317 displayHelp(true, ev, help);
324 function findParentElement(elem, tagName) {
326 if (elem && elem.tagName === tagName) {
329 elem = elem.parentNode;
334 document.onkeypress = handleShortcut;
335 document.onkeydown = handleShortcut;
336 document.onclick = function(ev) {
337 if (hasClass(ev.target, "collapse-toggle")) {
338 collapseDocs(ev.target, "toggle");
339 } else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
340 collapseDocs(ev.target.parentNode, "toggle");
341 } else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
344 var set_fragment = function(name) {
345 if (browserSupportsHistoryApi()) {
346 history.replaceState(null, null, "#" + name);
349 location.replace("#" + name);
353 var cur_id = parseInt(ev.target.id, 10);
355 if (ev.shiftKey && prev_id) {
356 if (prev_id > cur_id) {
362 set_fragment(prev_id + "-" + cur_id);
366 set_fragment(cur_id);
368 } else if (hasClass(document.getElementById("help"), "hidden") === false) {
369 addClass(document.getElementById("help"), "hidden");
370 removeClass(document.body, "blur");
372 // Making a collapsed element visible on onhashchange seems
374 var a = findParentElement(ev.target, "A");
376 expandSection(a.hash.replace(/^#/, ""));
381 var x = document.getElementsByClassName("version-selector");
383 x[0].onchange = function() {
385 url = document.location.href,
387 len = rootPath.match(/\.\.\//g).length + 1;
389 for (i = 0; i < len; ++i) {
390 match = url.match(/\/[^\/]*$/);
392 stripped = match[0] + stripped;
394 url = url.substring(0, url.length - match[0].length);
397 url += "/" + document.getElementsByClassName("version-selector")[0].value + stripped;
399 document.location.href = url;
404 * A function to compute the Levenshtein distance between two strings
405 * Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
406 * Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode
407 * This code is an unmodified version of the code written by Marco de Wit
408 * and was found at http://stackoverflow.com/a/18514751/745719
410 var levenshtein_row2 = [];
411 function levenshtein(s1, s2) {
415 var s1_len = s1.length, s2_len = s2.length;
416 if (s1_len && s2_len) {
417 var i1 = 0, i2 = 0, a, b, c, c2, row = levenshtein_row2;
418 while (i1 < s1_len) {
421 while (i2 < s2_len) {
422 c2 = s2.charCodeAt(i2);
426 for (i1 = 0; i1 < s1_len; ++i1) {
427 c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
429 b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
435 return s1_len + s2_len;
438 function initSearch(rawSearchIndex) {
439 var currentResults, index, searchIndex;
440 var MAX_LEV_DISTANCE = 3;
441 var MAX_RESULTS = 200;
442 var GENERICS_DATA = 1;
446 var params = getQueryStringParams();
448 // Set the crate filter from saved storage, if the current page has the saved crate filter.
450 // If not, ignore the crate filter -- we want to support filtering for crates on sites like
451 // doc.rust-lang.org where the crates may differ from page to page while on the same domain.
452 var savedCrate = getCurrentValue("rustdoc-saved-filter-crate");
453 if (savedCrate !== null) {
454 onEachLazy(document.getElementById("crate-search").getElementsByTagName("option"),
456 if (e.value === savedCrate) {
457 document.getElementById("crate-search").value = e.value;
463 // Populate search bar with query string search term when provided,
464 // but only if the input bar is empty. This avoid the obnoxious issue
465 // where you start trying to do a search, and the index loads, and
466 // suddenly your search is gone!
467 if (search_input.value === "") {
468 search_input.value = params.search || "";
472 * Executes the query and builds an index of results
473 * @param {[Object]} query [The user query]
474 * @param {[type]} searchWords [The list of search words to query
476 * @param {[type]} filterCrates [Crate to search in if defined]
477 * @return {[type]} [A search index of results]
479 function execQuery(query, searchWords, filterCrates) {
480 function itemTypeFromName(typename) {
481 var length = itemTypes.length;
482 for (var i = 0; i < length; ++i) {
483 if (itemTypes[i] === typename) {
490 var valLower = query.query.toLowerCase(),
492 typeFilter = itemTypeFromName(query.type),
493 results = {}, results_in_args = {}, results_returned = {},
494 split = valLower.split("::");
496 var length = split.length;
497 for (var z = 0; z < length; ++z) {
498 if (split[z] === "") {
504 function transformResults(results, isType) {
506 var length = results.length;
507 for (var i = 0; i < length; ++i) {
508 if (results[i].id > -1) {
509 var obj = searchIndex[results[i].id];
510 obj.lev = results[i].lev;
511 if (isType !== true || obj.type) {
512 var res = buildHrefAndPath(obj);
513 obj.displayPath = pathSplitter(res[0]);
514 obj.fullPath = obj.displayPath + obj.name;
515 // To be sure than it some items aren't considered as duplicate.
516 obj.fullPath += "|" + obj.ty;
519 if (out.length >= MAX_RESULTS) {
528 function sortResults(results, isType) {
530 for (var entry in results) {
531 if (results.hasOwnProperty(entry)) {
532 ar.push(results[entry]);
537 var nresults = results.length;
538 for (i = 0; i < nresults; ++i) {
539 results[i].word = searchWords[results[i].id];
540 results[i].item = searchIndex[results[i].id] || {};
542 // if there are no results then return to default and fail
543 if (results.length === 0) {
547 results.sort(function(aaa, bbb) {
550 // Sort by non levenshtein results and then levenshtein results by the distance
551 // (less changes required to match means higher rankings)
554 if (a !== b) { return a - b; }
556 // sort by crate (non-current crate goes later)
557 a = (aaa.item.crate !== window.currentCrate);
558 b = (bbb.item.crate !== window.currentCrate);
559 if (a !== b) { return a - b; }
561 // sort by exact match (mismatch goes later)
562 a = (aaa.word !== valLower);
563 b = (bbb.word !== valLower);
564 if (a !== b) { return a - b; }
566 // sort by item name length (longer goes later)
569 if (a !== b) { return a - b; }
571 // sort by item name (lexicographically larger goes later)
574 if (a !== b) { return (a > b ? +1 : -1); }
576 // sort by index of keyword in item name (no literal occurrence goes later)
579 if (a !== b) { return a - b; }
580 // (later literal occurrence, if any, goes later)
583 if (a !== b) { return a - b; }
585 // special precedence for primitive and keyword pages
586 if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
587 (aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
590 if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
591 (bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
595 // sort by description (no description goes later)
596 a = (aaa.item.desc === "");
597 b = (bbb.item.desc === "");
598 if (a !== b) { return a - b; }
600 // sort by type (later occurrence in `itemTypes` goes later)
603 if (a !== b) { return a - b; }
605 // sort by path (lexicographically larger goes later)
608 if (a !== b) { return (a > b ? +1 : -1); }
614 var length = results.length;
615 for (i = 0; i < length; ++i) {
616 var result = results[i];
618 // this validation does not make sense when searching by types
619 if (result.dontValidate) {
622 var name = result.item.name.toLowerCase(),
623 path = result.item.path.toLowerCase(),
624 parent = result.item.parent;
626 if (isType !== true &&
627 validateResult(name, path, split, parent) === false)
632 return transformResults(results);
635 function extractGenerics(val) {
636 val = val.toLowerCase();
637 if (val.indexOf("<") !== -1) {
638 var values = val.substring(val.indexOf("<") + 1, val.lastIndexOf(">"));
640 name: val.substring(0, val.indexOf("<")),
641 generics: values.split(/\s*,\s*/),
650 function checkGenerics(obj, val) {
651 // The names match, but we need to be sure that all generics kinda
653 var lev_distance = MAX_LEV_DISTANCE + 1;
654 if (val.generics.length > 0) {
655 if (obj.length > GENERICS_DATA &&
656 obj[GENERICS_DATA].length >= val.generics.length) {
657 var elems = obj[GENERICS_DATA].slice(0);
660 // We need to find the type that matches the most to remove it in order
662 var vlength = val.generics.length;
663 for (var y = 0; y < vlength; ++y) {
664 var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
665 var elength = elems.length;
666 for (var x = 0; x < elength; ++x) {
667 var tmp_lev = levenshtein(elems[x], val.generics[y]);
668 if (tmp_lev < lev.lev) {
673 if (lev.pos !== -1) {
674 elems.splice(lev.pos, 1);
675 lev_distance = Math.min(lev.lev, lev_distance);
679 return MAX_LEV_DISTANCE + 1;
682 return Math.ceil(total / done);
685 return MAX_LEV_DISTANCE + 1;
688 // Check for type name and type generics (if any).
689 function checkType(obj, val, literalSearch) {
690 var lev_distance = MAX_LEV_DISTANCE + 1;
692 if (obj[NAME] === val.name) {
693 if (literalSearch === true) {
694 if (val.generics && val.generics.length !== 0) {
695 if (obj.length > GENERICS_DATA &&
696 obj[GENERICS_DATA].length >= val.generics.length) {
697 var elems = obj[GENERICS_DATA].slice(0);
700 for (var y = 0; allFound === true && y < val.generics.length; ++y) {
702 for (x = 0; allFound === false && x < elems.length; ++x) {
703 allFound = elems[x] === val.generics[y];
705 if (allFound === true) {
706 elems.splice(x - 1, 1);
709 if (allFound === true) {
718 // If the type has generics but don't match, then it won't return at this point.
719 // Otherwise, `checkGenerics` will return 0 and it'll return.
720 if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length !== 0) {
721 var tmp_lev = checkGenerics(obj, val);
722 if (tmp_lev <= MAX_LEV_DISTANCE) {
729 // Names didn't match so let's check if one of the generic types could.
730 if (literalSearch === true) {
731 if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
732 var length = obj[GENERICS_DATA].length;
733 for (x = 0; x < length; ++x) {
734 if (obj[GENERICS_DATA][x] === val.name) {
741 lev_distance = Math.min(levenshtein(obj[NAME], val.name), lev_distance);
742 if (lev_distance <= MAX_LEV_DISTANCE) {
743 // The generics didn't match but the name kinda did so we give it
744 // a levenshtein distance value that isn't *this* good so it goes
745 // into the search results but not too high.
746 lev_distance = Math.ceil((checkGenerics(obj, val) + lev_distance) / 2);
747 } else if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
748 // We can check if the type we're looking for is inside the generics!
749 var olength = obj[GENERICS_DATA].length;
750 for (x = 0; x < olength; ++x) {
751 lev_distance = Math.min(levenshtein(obj[GENERICS_DATA][x], val.name),
755 // Now whatever happens, the returned distance is "less good" so we should mark it
756 // as such, and so we add 1 to the distance to make it "less good".
757 return lev_distance + 1;
760 function findArg(obj, val, literalSearch) {
761 var lev_distance = MAX_LEV_DISTANCE + 1;
763 if (obj && obj.type && obj.type[INPUTS_DATA] &&
764 obj.type[INPUTS_DATA].length > 0) {
765 var length = obj.type[INPUTS_DATA].length;
766 for (var i = 0; i < length; i++) {
767 var tmp = checkType(obj.type[INPUTS_DATA][i], val, literalSearch);
768 if (literalSearch === true && tmp === true) {
771 lev_distance = Math.min(tmp, lev_distance);
772 if (lev_distance === 0) {
777 return literalSearch === true ? false : lev_distance;
780 function checkReturned(obj, val, literalSearch) {
781 var lev_distance = MAX_LEV_DISTANCE + 1;
783 if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
784 var ret = obj.type[OUTPUT_DATA];
785 if (!obj.type[OUTPUT_DATA].length) {
788 for (var x = 0; x < ret.length; ++x) {
790 if (typeof r === "string") {
793 var tmp = checkType(r, val, literalSearch);
794 if (literalSearch === true) {
800 lev_distance = Math.min(tmp, lev_distance);
801 if (lev_distance === 0) {
806 return literalSearch === true ? false : lev_distance;
809 function checkPath(contains, lastElem, ty) {
810 if (contains.length === 0) {
813 var ret_lev = MAX_LEV_DISTANCE + 1;
814 var path = ty.path.split("::");
816 if (ty.parent && ty.parent.name) {
817 path.push(ty.parent.name.toLowerCase());
820 var length = path.length;
821 var clength = contains.length;
822 if (clength > length) {
823 return MAX_LEV_DISTANCE + 1;
825 for (var i = 0; i < length; ++i) {
826 if (i + clength > length) {
831 for (var x = 0; x < clength; ++x) {
832 var lev = levenshtein(path[i + x], contains[x]);
833 if (lev > MAX_LEV_DISTANCE) {
839 if (aborted === false) {
840 ret_lev = Math.min(ret_lev, Math.round(lev_total / clength));
846 function typePassesFilter(filter, type) {
848 if (filter < 0) return true;
851 if (filter === type) return true;
853 // Match related items
854 var name = itemTypes[type];
855 switch (itemTypes[filter]) {
857 return (name == "associatedconstant");
859 return (name == "method" || name == "tymethod");
861 return (name == "primitive" || name == "keyword");
868 function generateId(ty) {
869 if (ty.parent && ty.parent.name) {
870 return itemTypes[ty.ty] + ty.path + ty.parent.name + ty.name;
872 return itemTypes[ty.ty] + ty.path + ty.name;
875 // quoted values mean literal search
876 var nSearchWords = searchWords.length;
882 if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
883 val.charAt(val.length - 1) === val.charAt(0))
885 val = extractGenerics(val.substr(1, val.length - 2));
886 for (i = 0; i < nSearchWords; ++i) {
887 if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
890 in_args = findArg(searchIndex[i], val, true);
891 returned = checkReturned(searchIndex[i], val, true);
893 fullId = generateId(ty);
895 if (searchWords[i] === val.name) {
896 // filter type: ... queries
897 if (typePassesFilter(typeFilter, searchIndex[i].ty) &&
898 results[fullId] === undefined)
900 results[fullId] = {id: i, index: -1};
902 } else if ((in_args === true || returned === true) &&
903 typePassesFilter(typeFilter, searchIndex[i].ty)) {
904 if (in_args === true || returned === true) {
905 if (in_args === true) {
906 results_in_args[fullId] = {
912 if (returned === true) {
913 results_returned[fullId] = {
928 query.inputs = [val];
932 } else if (val.search("->") > -1) {
933 var trimmer = function(s) { return s.trim(); };
934 var parts = val.split("->").map(trimmer);
935 var input = parts[0];
936 // sort inputs so that order does not matter
937 var inputs = input.split(",").map(trimmer).sort();
938 for (i = 0; i < inputs.length; ++i) {
939 inputs[i] = extractGenerics(inputs[i]);
941 var output = extractGenerics(parts[1]);
943 for (i = 0; i < nSearchWords; ++i) {
944 if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
947 var type = searchIndex[i].type;
952 fullId = generateId(ty);
954 // allow searching for void (no output) functions as well
955 var typeOutput = type.length > OUTPUT_DATA ? type[OUTPUT_DATA].name : "";
956 returned = checkReturned(ty, output, true);
957 if (output.name === "*" || returned === true) {
959 var is_module = false;
965 for (var it = 0; allFound === true && it < inputs.length; it++) {
966 allFound = checkType(type, inputs[it], true);
970 if (in_args === true) {
971 results_in_args[fullId] = {
977 if (returned === true) {
978 results_returned[fullId] = {
984 if (is_module === true) {
993 query.inputs = inputs.map(function(input) {
996 query.output = output.name;
998 query.inputs = [val];
1001 // gather matching search results up to a certain maximum
1002 val = val.replace(/\_/g, "");
1004 var valGenerics = extractGenerics(val);
1006 var paths = valLower.split("::");
1008 for (j = 0; j < paths.length; ++j) {
1009 if (paths[j] === "") {
1014 val = paths[paths.length - 1];
1015 var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
1017 for (j = 0; j < nSearchWords; ++j) {
1020 ty = searchIndex[j];
1021 if (!ty || (filterCrates !== undefined && ty.crate !== filterCrates)) {
1026 if (paths.length > 1) {
1027 lev = checkPath(contains, paths[paths.length - 1], ty);
1028 if (lev > MAX_LEV_DISTANCE) {
1030 } else if (lev > 0) {
1035 returned = MAX_LEV_DISTANCE + 1;
1036 in_args = MAX_LEV_DISTANCE + 1;
1038 // we want lev results to go lower than others
1039 lev = MAX_LEV_DISTANCE + 1;
1040 fullId = generateId(ty);
1042 if (searchWords[j].indexOf(split[i]) > -1 ||
1043 searchWords[j].indexOf(val) > -1 ||
1044 searchWords[j].replace(/_/g, "").indexOf(val) > -1)
1046 // filter type: ... queries
1047 if (typePassesFilter(typeFilter, ty.ty) && results[fullId] === undefined) {
1048 index = searchWords[j].replace(/_/g, "").indexOf(val);
1051 if ((lev = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
1052 if (typePassesFilter(typeFilter, ty.ty) === false) {
1053 lev = MAX_LEV_DISTANCE + 1;
1058 if ((in_args = findArg(ty, valGenerics)) <= MAX_LEV_DISTANCE) {
1059 if (typePassesFilter(typeFilter, ty.ty) === false) {
1060 in_args = MAX_LEV_DISTANCE + 1;
1063 if ((returned = checkReturned(ty, valGenerics)) <= MAX_LEV_DISTANCE) {
1064 if (typePassesFilter(typeFilter, ty.ty) === false) {
1065 returned = MAX_LEV_DISTANCE + 1;
1070 if (lev > 0 && val.length > 3 && searchWords[j].indexOf(val) > -1) {
1071 if (val.length < 6) {
1077 if (in_args <= MAX_LEV_DISTANCE) {
1078 if (results_in_args[fullId] === undefined) {
1079 results_in_args[fullId] = {
1085 results_in_args[fullId].lev =
1086 Math.min(results_in_args[fullId].lev, in_args);
1088 if (returned <= MAX_LEV_DISTANCE) {
1089 if (results_returned[fullId] === undefined) {
1090 results_returned[fullId] = {
1096 results_returned[fullId].lev =
1097 Math.min(results_returned[fullId].lev, returned);
1099 if (index !== -1 || lev <= MAX_LEV_DISTANCE) {
1100 if (index !== -1 && paths.length < 2) {
1102 } else if (searchWords[j] === val) {
1103 // Small trick to fix when you're looking for a one letter type
1104 // and there are other short named types.
1107 if (results[fullId] === undefined) {
1114 results[fullId].lev = Math.min(results[fullId].lev, lev);
1120 "in_args": sortResults(results_in_args, true),
1121 "returned": sortResults(results_returned, true),
1122 "others": sortResults(results),
1124 if (ALIASES && ALIASES[window.currentCrate] &&
1125 ALIASES[window.currentCrate][query.raw]) {
1126 var aliases = ALIASES[window.currentCrate][query.raw];
1127 for (i = 0; i < aliases.length; ++i) {
1128 aliases[i].is_alias = true;
1129 aliases[i].alias = query.raw;
1130 aliases[i].path = aliases[i].p;
1131 var res = buildHrefAndPath(aliases[i]);
1132 aliases[i].displayPath = pathSplitter(res[0]);
1133 aliases[i].fullPath = aliases[i].displayPath + aliases[i].name;
1134 aliases[i].href = res[1];
1135 ret.others.unshift(aliases[i]);
1136 if (ret.others.length > MAX_RESULTS) {
1145 * Validate performs the following boolean logic. For example:
1146 * "File::open" will give IF A PARENT EXISTS => ("file" && "open")
1147 * exists in (name || path || parent) OR => ("file" && "open") exists in
1150 * This could be written functionally, but I wanted to minimise
1151 * functions on stack.
1153 * @param {[string]} name [The name of the result]
1154 * @param {[string]} path [The path of the result]
1155 * @param {[string]} keys [The keys to be used (["file", "open"])]
1156 * @param {[object]} parent [The parent of the result]
1157 * @return {[boolean]} [Whether the result is valid or not]
1159 function validateResult(name, path, keys, parent) {
1160 for (var i = 0; i < keys.length; ++i) {
1161 // each check is for validation so we negate the conditions and invalidate
1163 // check for an exact name match
1164 name.indexOf(keys[i]) > -1 ||
1165 // then an exact path match
1166 path.indexOf(keys[i]) > -1 ||
1167 // next if there is a parent, check for exact parent match
1168 (parent !== undefined &&
1169 parent.name.toLowerCase().indexOf(keys[i]) > -1) ||
1170 // lastly check to see if the name was a levenshtein match
1171 levenshtein(name, keys[i]) <= MAX_LEV_DISTANCE)) {
1178 function getQuery(raw) {
1179 var matches, type, query;
1182 matches = query.match(/^(fn|mod|struct|enum|trait|type|const|macro)\s*:\s*/i);
1184 type = matches[1].replace(/^const$/, "constant");
1185 query = query.substring(matches[0].length);
1196 function initSearchNav() {
1199 var click_func = function(e) {
1201 // to retrieve the real "owner" of the event.
1202 while (el.tagName !== "TR") {
1205 var dst = e.target.getElementsByTagName("a");
1206 if (dst.length < 1) {
1210 if (window.location.pathname === dst.pathname) {
1211 addClass(document.getElementById("search"), "hidden");
1212 removeClass(main, "hidden");
1213 document.location.href = dst.href;
1216 var mouseover_func = function(e) {
1218 // to retrieve the real "owner" of the event.
1219 while (el.tagName !== "TR") {
1222 clearTimeout(hoverTimeout);
1223 hoverTimeout = setTimeout(function() {
1224 onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1225 onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1226 removeClass(i_e, "highlighted");
1229 addClass(el, "highlighted");
1232 onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1233 onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1234 i_e.onclick = click_func;
1235 i_e.onmouseover = mouseover_func;
1239 search_input.onkeydown = function(e) {
1240 // "actives" references the currently highlighted item in each search tab.
1241 // Each array in "actives" represents a tab.
1242 var actives = [[], [], []];
1243 // "current" is used to know which tab we're looking into.
1245 onEachLazy(document.getElementById("results").childNodes, function(e) {
1246 onEachLazy(e.getElementsByClassName("highlighted"), function(e) {
1247 actives[current].push(e);
1252 if (e.which === 38) { // up
1253 if (!actives[currentTab].length ||
1254 !actives[currentTab][0].previousElementSibling) {
1258 addClass(actives[currentTab][0].previousElementSibling, "highlighted");
1259 removeClass(actives[currentTab][0], "highlighted");
1260 } else if (e.which === 40) { // down
1261 if (!actives[currentTab].length) {
1262 var results = document.getElementById("results").childNodes;
1263 if (results.length > 0) {
1264 var res = results[currentTab].getElementsByClassName("result");
1265 if (res.length > 0) {
1266 addClass(res[0], "highlighted");
1269 } else if (actives[currentTab][0].nextElementSibling) {
1270 addClass(actives[currentTab][0].nextElementSibling, "highlighted");
1271 removeClass(actives[currentTab][0], "highlighted");
1273 } else if (e.which === 13) { // return
1274 if (actives[currentTab].length) {
1275 document.location.href =
1276 actives[currentTab][0].getElementsByTagName("a")[0].href;
1278 } else if (e.which === 9) { // tab
1280 printTab(currentTab > 0 ? currentTab - 1 : 2);
1282 printTab(currentTab > 1 ? 0 : currentTab + 1);
1285 } else if (e.which === 16) { // shift
1286 // Does nothing, it's just to avoid losing "focus" on the highlighted element.
1287 } else if (e.which === 27) { // escape
1288 removeClass(actives[currentTab][0], "highlighted");
1289 search_input.value = "";
1291 } else if (actives[currentTab].length > 0) {
1292 removeClass(actives[currentTab][0], "highlighted");
1297 function buildHrefAndPath(item) {
1300 var type = itemTypes[item.ty];
1301 var name = item.name;
1303 if (type === "mod") {
1304 displayPath = item.path + "::";
1305 href = rootPath + item.path.replace(/::/g, "/") + "/" +
1306 name + "/index.html";
1307 } else if (type === "primitive" || type === "keyword") {
1309 href = rootPath + item.path.replace(/::/g, "/") +
1310 "/" + type + "." + name + ".html";
1311 } else if (type === "externcrate") {
1313 href = rootPath + name + "/index.html";
1314 } else if (item.parent !== undefined) {
1315 var myparent = item.parent;
1316 var anchor = "#" + type + "." + name;
1317 var parentType = itemTypes[myparent.ty];
1318 if (parentType === "primitive") {
1319 displayPath = myparent.name + "::";
1321 displayPath = item.path + "::" + myparent.name + "::";
1323 href = rootPath + item.path.replace(/::/g, "/") +
1325 "." + myparent.name +
1328 displayPath = item.path + "::";
1329 href = rootPath + item.path.replace(/::/g, "/") +
1330 "/" + type + "." + name + ".html";
1332 return [displayPath, href];
1335 function escape(content) {
1336 var h1 = document.createElement("h1");
1337 h1.textContent = content;
1338 return h1.innerHTML;
1341 function pathSplitter(path) {
1342 var tmp = "<span>" + path.replace(/::/g, "::</span><span>");
1343 if (tmp.endsWith("<span>")) {
1344 return tmp.slice(0, tmp.length - 6);
1349 function addTab(array, query, display) {
1350 var extraStyle = "";
1351 if (display === false) {
1352 extraStyle = " style=\"display: none;\"";
1356 var duplicates = {};
1358 if (array.length > 0) {
1359 output = "<table class=\"search-results\"" + extraStyle + ">";
1361 array.forEach(function(item) {
1365 type = itemTypes[item.ty];
1367 if (item.is_alias !== true) {
1368 if (duplicates[item.fullPath]) {
1371 duplicates[item.fullPath] = true;
1375 output += "<tr class=\"" + type + " result\"><td>" +
1376 "<a href=\"" + item.href + "\">" +
1377 (item.is_alias === true ?
1378 ("<span class=\"alias\"><b>" + item.alias + " </b></span><span " +
1379 "class=\"grey\"><i> - see </i></span>") : "") +
1380 item.displayPath + "<span class=\"" + type + "\">" +
1381 name + "</span></a></td><td>" +
1382 "<a href=\"" + item.href + "\">" +
1383 "<span class=\"desc\">" + escape(item.desc) +
1384 " </span></a></td></tr>";
1386 output += "</table>";
1388 output = "<div class=\"search-failed\"" + extraStyle + ">No results :(<br/>" +
1389 "Try on <a href=\"https://duckduckgo.com/?q=" +
1390 encodeURIComponent("rust " + query.query) +
1391 "\">DuckDuckGo</a>?<br/><br/>" +
1392 "Or try looking in one of these:<ul><li>The <a " +
1393 "href=\"https://doc.rust-lang.org/reference/index.html\">Rust Reference</a> " +
1394 " for technical details about the language.</li><li><a " +
1395 "href=\"https://doc.rust-lang.org/rust-by-example/index.html\">Rust By " +
1396 "Example</a> for expository code examples.</a></li><li>The <a " +
1397 "href=\"https://doc.rust-lang.org/book/index.html\">Rust Book</a> for " +
1398 "introductions to language features and the language itself.</li><li><a " +
1399 "href=\"https://docs.rs\">Docs.rs</a> for documentation of crates released on" +
1400 " <a href=\"https://crates.io/\">crates.io</a>.</li></ul></div>";
1402 return [output, length];
1405 function makeTabHeader(tabNb, text, nbElems) {
1406 if (currentTab === tabNb) {
1407 return "<div class=\"selected\">" + text +
1408 " <div class=\"count\">(" + nbElems + ")</div></div>";
1410 return "<div>" + text + " <div class=\"count\">(" + nbElems + ")</div></div>";
1413 function showResults(results) {
1414 if (results.others.length === 1 &&
1415 getCurrentValue("rustdoc-go-to-only-result") === "true") {
1416 var elem = document.createElement("a");
1417 elem.href = results.others[0].href;
1418 elem.style.display = "none";
1419 // For firefox, we need the element to be in the DOM so it can be clicked.
1420 document.body.appendChild(elem);
1423 var query = getQuery(search_input.value);
1425 currentResults = query.id;
1427 var ret_others = addTab(results.others, query);
1428 var ret_in_args = addTab(results.in_args, query, false);
1429 var ret_returned = addTab(results.returned, query, false);
1431 var output = "<h1>Results for " + escape(query.query) +
1432 (query.type ? " (type: " + escape(query.type) + ")" : "") + "</h1>" +
1433 "<div id=\"titles\">" +
1434 makeTabHeader(0, "In Names", ret_others[1]) +
1435 makeTabHeader(1, "In Parameters", ret_in_args[1]) +
1436 makeTabHeader(2, "In Return Types", ret_returned[1]) +
1437 "</div><div id=\"results\">" +
1438 ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
1440 addClass(main, "hidden");
1441 var search = document.getElementById("search");
1442 removeClass(search, "hidden");
1443 search.innerHTML = output;
1444 var tds = search.getElementsByTagName("td");
1446 if (tds.length > 0) {
1447 td_width = tds[0].offsetWidth;
1449 var width = search.offsetWidth - 40 - td_width;
1450 onEachLazy(search.getElementsByClassName("desc"), function(e) {
1451 e.style.width = width + "px";
1454 var elems = document.getElementById("titles").childNodes;
1455 elems[0].onclick = function() { printTab(0); };
1456 elems[1].onclick = function() { printTab(1); };
1457 elems[2].onclick = function() { printTab(2); };
1458 printTab(currentTab);
1461 function execSearch(query, searchWords, filterCrates) {
1462 function getSmallest(arrays, positions, notDuplicates) {
1465 for (var it = 0; it < positions.length; ++it) {
1466 if (arrays[it].length > positions[it] &&
1467 (start === null || start > arrays[it][positions[it]].lev) &&
1468 !notDuplicates[arrays[it][positions[it]].fullPath]) {
1469 start = arrays[it][positions[it]].lev;
1475 function mergeArrays(arrays) {
1478 var notDuplicates = {};
1480 for (var x = 0; x < arrays.length; ++x) {
1483 while (ret.length < MAX_RESULTS) {
1484 var smallest = getSmallest(arrays, positions, notDuplicates);
1486 if (smallest === null) {
1489 for (x = 0; x < arrays.length && ret.length < MAX_RESULTS; ++x) {
1490 if (arrays[x].length > positions[x] &&
1491 arrays[x][positions[x]].lev === smallest &&
1492 !notDuplicates[arrays[x][positions[x]].fullPath]) {
1493 ret.push(arrays[x][positions[x]]);
1494 notDuplicates[arrays[x][positions[x]].fullPath] = true;
1502 var queries = query.raw.split(",");
1509 for (var i = 0; i < queries.length; ++i) {
1510 query = queries[i].trim();
1511 if (query.length !== 0) {
1512 var tmp = execQuery(getQuery(query), searchWords, filterCrates);
1514 results.in_args.push(tmp.in_args);
1515 results.returned.push(tmp.returned);
1516 results.others.push(tmp.others);
1519 if (queries.length > 1) {
1521 "in_args": mergeArrays(results.in_args),
1522 "returned": mergeArrays(results.returned),
1523 "others": mergeArrays(results.others),
1527 "in_args": results.in_args[0],
1528 "returned": results.returned[0],
1529 "others": results.others[0],
1534 function getFilterCrates() {
1535 var elem = document.getElementById("crate-search");
1537 if (elem && elem.value !== "All crates" && rawSearchIndex.hasOwnProperty(elem.value)) {
1543 function search(e, forced) {
1544 var params = getQueryStringParams();
1545 var query = getQuery(search_input.value.trim());
1551 if (query.query.length === 0) {
1554 if (forced !== true && query.id === currentResults) {
1555 if (query.query.length > 0) {
1556 putBackSearch(search_input);
1561 // Update document title to maintain a meaningful browser history
1562 document.title = "Results for " + query.query + " - Rust";
1564 // Because searching is incremental by character, only the most
1565 // recent search query is added to the browser history.
1566 if (browserSupportsHistoryApi()) {
1567 if (!history.state && !params.search) {
1568 history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
1570 history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
1574 var filterCrates = getFilterCrates();
1575 showResults(execSearch(query, index, filterCrates), filterCrates);
1578 function buildIndex(rawSearchIndex) {
1580 var searchWords = [];
1583 for (var crate in rawSearchIndex) {
1584 if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
1586 searchWords.push(crate);
1589 ty: 1, // == ExternCrate
1592 desc: rawSearchIndex[crate].doc,
1596 // an array of [(Number) item type,
1598 // (String) full path or empty string for previous path,
1599 // (String) description,
1600 // (Number | null) the parent path index to `paths`]
1601 // (Object | null) the type of the function (if any)
1602 var items = rawSearchIndex[crate].i;
1603 // an array of [(Number) item type,
1605 var paths = rawSearchIndex[crate].p;
1607 // convert `paths` into an object form
1608 var len = paths.length;
1609 for (i = 0; i < len; ++i) {
1610 paths[i] = {ty: paths[i][0], name: paths[i][1]};
1613 // convert `items` into an object form, and construct word indices.
1615 // before any analysis is performed lets gather the search terms to
1616 // search against apart from the rest of the data. This is a quick
1617 // operation that is cached for the life of the page state so that
1618 // all other search operations have access to this cached data for
1619 // faster analysis operations
1622 for (i = 0; i < len; ++i) {
1623 var rawRow = items[i];
1624 var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
1625 path: rawRow[2] || lastPath, desc: rawRow[3],
1626 parent: paths[rawRow[4]], type: rawRow[5]};
1627 searchIndex.push(row);
1628 if (typeof row.name === "string") {
1629 var word = row.name.toLowerCase();
1630 searchWords.push(word);
1632 searchWords.push("");
1634 lastPath = row.path;
1640 function startSearch() {
1642 var callback = function() {
1643 clearTimeout(searchTimeout);
1644 if (search_input.value.length === 0) {
1645 if (browserSupportsHistoryApi()) {
1646 history.replaceState("", window.currentCrate + " - Rust", "?search=");
1648 if (hasClass(main, "content")) {
1649 removeClass(main, "hidden");
1651 var search_c = document.getElementById("search");
1652 if (hasClass(search_c, "content")) {
1653 addClass(search_c, "hidden");
1656 searchTimeout = setTimeout(search, 500);
1659 search_input.onkeyup = callback;
1660 search_input.oninput = callback;
1661 document.getElementsByClassName("search-form")[0].onsubmit = function(e) {
1663 clearTimeout(searchTimeout);
1666 search_input.onchange = function(e) {
1667 // Do NOT e.preventDefault() here. It will prevent pasting.
1668 clearTimeout(searchTimeout);
1669 // zero-timeout necessary here because at the time of event handler execution the
1670 // pasted content is not in the input field yet. Shouldn’t make any difference for
1672 setTimeout(search, 0);
1674 search_input.onpaste = search_input.onchange;
1676 var selectCrate = document.getElementById("crate-search");
1678 selectCrate.onchange = function() {
1679 updateLocalStorage("rustdoc-saved-filter-crate", selectCrate.value);
1680 search(undefined, true);
1684 // Push and pop states are used to add search results to the browser
1686 if (browserSupportsHistoryApi()) {
1687 // Store the previous <title> so we can revert back to it later.
1688 var previousTitle = document.title;
1690 window.onpopstate = function(e) {
1691 var params = getQueryStringParams();
1692 // When browsing back from search results the main page
1693 // visibility must be reset.
1694 if (!params.search) {
1695 if (hasClass(main, "content")) {
1696 removeClass(main, "hidden");
1698 var search_c = document.getElementById("search");
1699 if (hasClass(search_c, "content")) {
1700 addClass(search_c, "hidden");
1703 // Revert to the previous title manually since the History
1704 // API ignores the title parameter.
1705 document.title = previousTitle;
1706 // When browsing forward to search results the previous
1707 // search will be repeated, so the currentResults are
1708 // cleared to ensure the search is successful.
1709 currentResults = null;
1710 // Synchronize search bar with query string state and
1711 // perform the search. This will empty the bar if there's
1712 // nothing there, which lets you really go back to a
1713 // previous state with nothing in the bar.
1714 if (params.search) {
1715 search_input.value = params.search;
1717 search_input.value = "";
1719 // Some browsers fire "onpopstate" for every page load
1720 // (Chrome), while others fire the event only when actually
1721 // popping a state (Firefox), which is why search() is
1722 // called both here and at the end of the startSearch()
1730 index = buildIndex(rawSearchIndex);
1733 // Draw a convenient sidebar of known crates if we have a listing
1734 if (rootPath === "../" || rootPath === "./") {
1735 var sidebar = document.getElementsByClassName("sidebar-elems")[0];
1737 var div = document.createElement("div");
1738 div.className = "block crate";
1739 div.innerHTML = "<h3>Crates</h3>";
1740 var ul = document.createElement("ul");
1741 div.appendChild(ul);
1744 for (var crate in rawSearchIndex) {
1745 if (!rawSearchIndex.hasOwnProperty(crate)) {
1751 for (var i = 0; i < crates.length; ++i) {
1752 var klass = "crate";
1753 if (rootPath !== "./" && crates[i] === window.currentCrate) {
1754 klass += " current";
1756 var link = document.createElement("a");
1757 link.href = rootPath + crates[i] + "/index.html";
1758 link.title = rawSearchIndex[crates[i]].doc;
1759 link.className = klass;
1760 link.textContent = crates[i];
1762 var li = document.createElement("li");
1763 li.appendChild(link);
1766 sidebar.appendChild(div);
1771 window.initSearch = initSearch;
1773 // delayed sidebar rendering.
1774 function initSidebarItems(items) {
1775 var sidebar = document.getElementsByClassName("sidebar-elems")[0];
1776 var current = window.sidebarCurrent;
1778 function block(shortty, longty) {
1779 var filtered = items[shortty];
1784 var div = document.createElement("div");
1785 div.className = "block " + shortty;
1786 var h3 = document.createElement("h3");
1787 h3.textContent = longty;
1788 div.appendChild(h3);
1789 var ul = document.createElement("ul");
1791 var length = filtered.length;
1792 for (var i = 0; i < length; ++i) {
1793 var item = filtered[i];
1795 var desc = item[1]; // can be null
1797 var klass = shortty;
1798 if (name === current.name && shortty === current.ty) {
1799 klass += " current";
1802 if (shortty === "mod") {
1803 path = name + "/index.html";
1805 path = shortty + "." + name + ".html";
1807 var link = document.createElement("a");
1808 link.href = current.relpath + path;
1810 link.className = klass;
1811 link.textContent = name;
1812 var li = document.createElement("li");
1813 li.appendChild(link);
1816 div.appendChild(ul);
1818 sidebar.appendChild(div);
1822 block("primitive", "Primitive Types");
1823 block("mod", "Modules");
1824 block("macro", "Macros");
1825 block("struct", "Structs");
1826 block("enum", "Enums");
1827 block("union", "Unions");
1828 block("constant", "Constants");
1829 block("static", "Statics");
1830 block("trait", "Traits");
1831 block("fn", "Functions");
1832 block("type", "Type Definitions");
1833 block("foreigntype", "Foreign Types");
1834 block("keyword", "Keywords");
1835 block("traitalias", "Trait Aliases");
1838 window.initSidebarItems = initSidebarItems;
1840 window.register_implementors = function(imp) {
1841 var implementors = document.getElementById("implementors-list");
1842 var synthetic_implementors = document.getElementById("synthetic-implementors-list");
1844 var libs = Object.getOwnPropertyNames(imp);
1845 var llength = libs.length;
1846 for (var i = 0; i < llength; ++i) {
1847 if (libs[i] === currentCrate) { continue; }
1848 var structs = imp[libs[i]];
1850 var slength = structs.length;
1852 for (var j = 0; j < slength; ++j) {
1853 var struct = structs[j];
1855 var list = struct.synthetic ? synthetic_implementors : implementors;
1857 if (struct.synthetic) {
1858 var stlength = struct.types.length;
1859 for (var k = 0; k < stlength; k++) {
1860 if (window.inlined_types.has(struct.types[k])) {
1861 continue struct_loop;
1863 window.inlined_types.add(struct.types[k]);
1867 var code = document.createElement("code");
1868 code.innerHTML = struct.text;
1870 var x = code.getElementsByTagName("a");
1871 var xlength = x.length;
1872 for (var it = 0; it < xlength; it++) {
1873 var href = x[it].getAttribute("href");
1874 if (href && href.indexOf("http") !== 0) {
1875 x[it].setAttribute("href", rootPath + href);
1878 var display = document.createElement("h3");
1879 addClass(display, "impl");
1880 display.innerHTML = "<span class=\"in-band\"><table class=\"table-display\">" +
1881 "<tbody><tr><td><code>" + code.outerHTML + "</code></td><td></td></tr>" +
1882 "</tbody></table></span>";
1883 list.appendChild(display);
1887 if (window.pending_implementors) {
1888 window.register_implementors(window.pending_implementors);
1891 function labelForToggleButton(sectionIsCollapsed) {
1892 if (sectionIsCollapsed) {
1893 // button will expand the section
1896 // button will collapse the section
1897 // note that this text is also set in the HTML template in render.rs
1898 return "\u2212"; // "\u2212" is "−" minus sign
1901 function onEveryMatchingChild(elem, className, func) {
1902 if (elem && className && func) {
1903 var length = elem.childNodes.length;
1904 var nodes = elem.childNodes;
1905 for (var i = 0; i < length; ++i) {
1906 if (hasClass(nodes[i], className)) {
1909 onEveryMatchingChild(nodes[i], className, func);
1915 function toggleAllDocs(pageId, fromAutoCollapse) {
1916 var innerToggle = document.getElementById("toggle-all-docs");
1920 if (hasClass(innerToggle, "will-expand")) {
1921 updateLocalStorage("rustdoc-collapse", "false");
1922 removeClass(innerToggle, "will-expand");
1923 onEveryMatchingChild(innerToggle, "inner", function(e) {
1924 e.innerHTML = labelForToggleButton(false);
1926 innerToggle.title = "collapse all docs";
1927 if (fromAutoCollapse !== true) {
1928 onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
1929 collapseDocs(e, "show");
1933 updateLocalStorage("rustdoc-collapse", "true");
1934 addClass(innerToggle, "will-expand");
1935 onEveryMatchingChild(innerToggle, "inner", function(e) {
1936 var parent = e.parentNode;
1937 var superParent = null;
1940 superParent = parent.parentNode;
1942 if (!parent || !superParent || superParent.id !== "main" ||
1943 hasClass(parent, "impl") === false) {
1944 e.innerHTML = labelForToggleButton(true);
1947 innerToggle.title = "expand all docs";
1948 if (fromAutoCollapse !== true) {
1949 onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
1950 var parent = e.parentNode;
1951 var superParent = null;
1954 superParent = parent.parentNode;
1956 if (!parent || !superParent || superParent.id !== "main" ||
1957 hasClass(parent, "impl") === false) {
1958 collapseDocs(e, "hide", pageId);
1965 function collapseDocs(toggle, mode, pageId) {
1966 if (!toggle || !toggle.parentNode) {
1970 function adjustToggle(arg) {
1971 return function(e) {
1972 if (hasClass(e, "toggle-label")) {
1974 e.style.display = "inline-block";
1976 e.style.display = "none";
1979 if (hasClass(e, "inner")) {
1980 e.innerHTML = labelForToggleButton(arg);
1985 function implHider(addOrRemove, fullHide) {
1986 return function(n) {
1987 var is_method = hasClass(n, "method") || fullHide;
1988 if (is_method || hasClass(n, "type")) {
1989 if (is_method === true) {
1991 addClass(n, "hidden-by-impl-hider");
1993 removeClass(n, "hidden-by-impl-hider");
1996 var ns = n.nextElementSibling;
1999 hasClass(ns, "docblock") ||
2000 hasClass(ns, "stability"))) {
2002 addClass(ns, "hidden-by-impl-hider");
2004 removeClass(ns, "hidden-by-impl-hider");
2006 ns = ns.nextElementSibling;
2017 if (hasClass(toggle.parentNode, "impl") === false) {
2018 relatedDoc = toggle.parentNode.nextElementSibling;
2019 if (hasClass(relatedDoc, "stability")) {
2020 relatedDoc = relatedDoc.nextElementSibling;
2022 if (hasClass(relatedDoc, "docblock") || hasClass(relatedDoc, "sub-variant")) {
2023 if (mode === "toggle") {
2024 if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
2030 if (action === "hide") {
2031 addClass(relatedDoc, "hidden-by-usual-hider");
2032 onEachLazy(toggle.childNodes, adjustToggle(true));
2033 addClass(toggle.parentNode, "collapsed");
2034 } else if (action === "show") {
2035 removeClass(relatedDoc, "hidden-by-usual-hider");
2036 removeClass(toggle.parentNode, "collapsed");
2037 onEachLazy(toggle.childNodes, adjustToggle(false));
2041 // we are collapsing the impl block(s).
2043 var parentElem = toggle.parentNode;
2044 relatedDoc = parentElem;
2045 var docblock = relatedDoc.nextElementSibling;
2047 while (hasClass(relatedDoc, "impl-items") === false) {
2048 relatedDoc = relatedDoc.nextElementSibling;
2051 if ((!relatedDoc && hasClass(docblock, "docblock") === false) ||
2052 (pageId && document.getElementById(pageId))) {
2056 // Hide all functions, but not associated types/consts.
2058 if (mode === "toggle") {
2059 if (hasClass(relatedDoc, "fns-now-collapsed") ||
2060 hasClass(docblock, "hidden-by-impl-hider")) {
2067 var dontApplyBlockRule = toggle.parentNode.parentNode.id !== "main";
2068 if (action === "show") {
2069 removeClass(relatedDoc, "fns-now-collapsed");
2070 removeClass(docblock, "hidden-by-usual-hider");
2071 onEachLazy(toggle.childNodes, adjustToggle(false, dontApplyBlockRule));
2072 onEachLazy(relatedDoc.childNodes, implHider(false, dontApplyBlockRule));
2073 } else if (action === "hide") {
2074 addClass(relatedDoc, "fns-now-collapsed");
2075 addClass(docblock, "hidden-by-usual-hider");
2076 onEachLazy(toggle.childNodes, adjustToggle(true, dontApplyBlockRule));
2077 onEachLazy(relatedDoc.childNodes, implHider(true, dontApplyBlockRule));
2082 function collapser(e, collapse) {
2083 // inherent impl ids are like "impl" or impl-<number>'.
2084 // they will never be hidden by default.
2085 var n = e.parentElement;
2086 if (n.id.match(/^impl(?:-\d+)?$/) === null) {
2087 // Automatically minimize all non-inherent impls
2088 if (collapse || hasClass(n, "impl")) {
2089 collapseDocs(e, "hide", pageId);
2094 function autoCollapse(pageId, collapse) {
2096 toggleAllDocs(pageId, true);
2097 } else if (getCurrentValue("rustdoc-trait-implementations") !== "false") {
2098 var impl_list = document.getElementById("implementations-list");
2100 if (impl_list !== null) {
2101 onEachLazy(impl_list.getElementsByClassName("collapse-toggle"), function(e) {
2102 collapser(e, collapse);
2106 var blanket_list = document.getElementById("blanket-implementations-list");
2108 if (blanket_list !== null) {
2109 onEachLazy(blanket_list.getElementsByClassName("collapse-toggle"), function(e) {
2110 collapser(e, collapse);
2116 var toggles = document.getElementById("toggle-all-docs");
2118 toggles.onclick = toggleAllDocs;
2121 function insertAfter(newNode, referenceNode) {
2122 referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
2125 function createSimpleToggle(sectionIsCollapsed) {
2126 var toggle = document.createElement("a");
2127 toggle.href = "javascript:void(0)";
2128 toggle.className = "collapse-toggle";
2129 toggle.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(sectionIsCollapsed) +
2134 var toggle = createSimpleToggle(false);
2135 var hideMethodDocs = getCurrentValue("rustdoc-method-docs") === "true";
2136 var pageId = getPageId();
2138 var func = function(e) {
2139 var next = e.nextElementSibling;
2143 if (hasClass(next, "docblock") === true ||
2144 (hasClass(next, "stability") === true &&
2145 hasClass(next.nextElementSibling, "docblock") === true)) {
2146 var newToggle = toggle.cloneNode(true);
2147 insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
2148 if (hideMethodDocs === true && hasClass(e, "method") === true) {
2149 collapseDocs(newToggle, "hide", pageId);
2154 var funcImpl = function(e) {
2155 var next = e.nextElementSibling;
2156 if (next && hasClass(next, "docblock")) {
2157 next = next.nextElementSibling;
2162 if (next.getElementsByClassName("method").length > 0 && hasClass(e, "impl")) {
2163 insertAfter(toggle.cloneNode(true), e.childNodes[e.childNodes.length - 1]);
2167 onEachLazy(document.getElementsByClassName("method"), func);
2168 onEachLazy(document.getElementsByClassName("associatedconstant"), func);
2169 onEachLazy(document.getElementsByClassName("impl"), funcImpl);
2170 var impl_call = function() {};
2171 if (hideMethodDocs === true) {
2172 impl_call = function(e, newToggle, pageId) {
2173 if (e.id.match(/^impl(?:-\d+)?$/) === null) {
2174 // Automatically minimize all non-inherent impls
2175 if (hasClass(e, "impl") === true) {
2176 collapseDocs(newToggle, "hide", pageId);
2181 var newToggle = document.createElement("a");
2182 newToggle.href = "javascript:void(0)";
2183 newToggle.className = "collapse-toggle hidden-default collapsed";
2184 newToggle.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(true) +
2185 "</span>] Show hidden undocumented items";
2186 function toggleClicked() {
2187 if (hasClass(this, "collapsed")) {
2188 removeClass(this, "collapsed");
2189 onEachLazy(this.parentNode.getElementsByClassName("hidden"), function(x) {
2190 if (hasClass(x, "content") === false) {
2191 removeClass(x, "hidden");
2195 this.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(false) +
2196 "</span>] Hide undocumented items";
2198 addClass(this, "collapsed");
2199 onEachLazy(this.parentNode.getElementsByClassName("x"), function(x) {
2200 if (hasClass(x, "content") === false) {
2201 addClass(x, "hidden");
2202 removeClass(x, "x");
2205 this.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(true) +
2206 "</span>] Show hidden undocumented items";
2209 onEachLazy(document.getElementsByClassName("impl-items"), function(e) {
2210 onEachLazy(e.getElementsByClassName("associatedconstant"), func);
2211 var hiddenElems = e.getElementsByClassName("hidden");
2212 var needToggle = false;
2214 var hlength = hiddenElems.length;
2215 for (var i = 0; i < hlength; ++i) {
2216 if (hasClass(hiddenElems[i], "content") === false &&
2217 hasClass(hiddenElems[i], "docblock") === false) {
2222 if (needToggle === true) {
2223 var inner_toggle = newToggle.cloneNode(true);
2224 inner_toggle.onclick = toggleClicked;
2225 e.insertBefore(inner_toggle, e.firstChild);
2226 impl_call(e.previousSibling, inner_toggle, pageId);
2230 function createToggle(otherMessage, fontSize, extraClass, show) {
2231 var span = document.createElement("span");
2232 span.className = "toggle-label";
2234 span.style.display = "none";
2236 if (!otherMessage) {
2237 span.innerHTML = " Expand description";
2239 span.innerHTML = otherMessage;
2243 span.style.fontSize = fontSize;
2246 var mainToggle = toggle.cloneNode(true);
2247 mainToggle.appendChild(span);
2249 var wrapper = document.createElement("div");
2250 wrapper.className = "toggle-wrapper";
2252 addClass(wrapper, "collapsed");
2253 var inner = mainToggle.getElementsByClassName("inner");
2254 if (inner && inner.length > 0) {
2255 inner[0].innerHTML = "+";
2259 addClass(wrapper, extraClass);
2261 wrapper.appendChild(mainToggle);
2265 var showItemDeclarations = getCurrentValue("rustdoc-item-declarations") === "false";
2266 function buildToggleWrapper(e) {
2267 if (hasClass(e, "autohide")) {
2268 var wrap = e.previousElementSibling;
2269 if (wrap && hasClass(wrap, "toggle-wrapper")) {
2270 var inner_toggle = wrap.childNodes[0];
2271 var extra = e.childNodes[0].tagName === "H3";
2273 e.style.display = "none";
2274 addClass(wrap, "collapsed");
2275 onEachLazy(inner_toggle.getElementsByClassName("inner"), function(e) {
2276 e.innerHTML = labelForToggleButton(true);
2278 onEachLazy(inner_toggle.getElementsByClassName("toggle-label"), function(e) {
2279 e.style.display = "inline-block";
2280 if (extra === true) {
2281 i_e.innerHTML = " Show " + e.childNodes[0].innerHTML;
2286 if (e.parentNode.id === "main") {
2287 var otherMessage = "";
2291 if (hasClass(e, "type-decl")) {
2293 otherMessage = " Show declaration";
2294 if (showItemDeclarations === false) {
2295 extraClass = "collapsed";
2297 } else if (hasClass(e, "sub-variant")) {
2298 otherMessage = " Show fields";
2299 } else if (hasClass(e, "non-exhaustive")) {
2300 otherMessage = " This ";
2301 if (hasClass(e, "non-exhaustive-struct")) {
2302 otherMessage += "struct";
2303 } else if (hasClass(e, "non-exhaustive-enum")) {
2304 otherMessage += "enum";
2305 } else if (hasClass(e, "non-exhaustive-variant")) {
2306 otherMessage += "enum variant";
2307 } else if (hasClass(e, "non-exhaustive-type")) {
2308 otherMessage += "type";
2310 otherMessage += " is marked as non-exhaustive";
2311 } else if (hasClass(e.childNodes[0], "impl-items")) {
2312 extraClass = "marg-left";
2315 e.parentNode.insertBefore(
2316 createToggle(otherMessage,
2319 hasClass(e, "type-decl") === false || showItemDeclarations === true),
2321 if (hasClass(e, "type-decl") === true && showItemDeclarations === true) {
2322 collapseDocs(e.previousSibling.childNodes[0], "toggle");
2324 if (hasClass(e, "non-exhaustive") === true) {
2325 collapseDocs(e.previousSibling.childNodes[0], "toggle");
2330 onEachLazy(document.getElementsByClassName("docblock"), buildToggleWrapper);
2331 onEachLazy(document.getElementsByClassName("sub-variant"), buildToggleWrapper);
2333 function createToggleWrapper(tog) {
2334 var span = document.createElement("span");
2335 span.className = "toggle-label";
2336 span.style.display = "none";
2337 span.innerHTML = " Expand attributes";
2338 tog.appendChild(span);
2340 var wrapper = document.createElement("div");
2341 wrapper.className = "toggle-wrapper toggle-attributes";
2342 wrapper.appendChild(tog);
2346 // To avoid checking on "rustdoc-item-attributes" value on every loop...
2347 var itemAttributesFunc = function() {};
2348 if (getCurrentValue("rustdoc-item-attributes") !== "false") {
2349 itemAttributesFunc = function(x) {
2350 collapseDocs(x.previousSibling.childNodes[0], "toggle");
2353 var attributesToggle = createToggleWrapper(createSimpleToggle(false));
2354 onEachLazy(main.getElementsByClassName("attributes"), function(i_e) {
2355 var attr_tog = attributesToggle.cloneNode(true);
2356 if (hasClass(i_e, "top-attr") === true) {
2357 addClass(attr_tog, "top-attr");
2359 i_e.parentNode.insertBefore(attr_tog, i_e);
2360 itemAttributesFunc(i_e);
2363 // To avoid checking on "rustdoc-line-numbers" value on every loop...
2364 var lineNumbersFunc = function() {};
2365 if (getCurrentValue("rustdoc-line-numbers") === "true") {
2366 lineNumbersFunc = function(x) {
2367 var count = x.textContent.split("\n").length;
2369 for (var i = 0; i < count; ++i) {
2372 var node = document.createElement("pre");
2373 addClass(node, "line-number");
2374 node.innerHTML = elems.join("\n");
2375 x.parentNode.insertBefore(node, x);
2378 onEachLazy(document.getElementsByClassName("rust-example-rendered"), function(e) {
2379 if (hasClass(e, "compile_fail")) {
2380 e.addEventListener("mouseover", function(event) {
2381 this.parentElement.previousElementSibling.childNodes[0].style.color = "#f00";
2383 e.addEventListener("mouseout", function(event) {
2384 this.parentElement.previousElementSibling.childNodes[0].style.color = "";
2386 } else if (hasClass(e, "ignore")) {
2387 e.addEventListener("mouseover", function(event) {
2388 this.parentElement.previousElementSibling.childNodes[0].style.color = "#ff9200";
2390 e.addEventListener("mouseout", function(event) {
2391 this.parentElement.previousElementSibling.childNodes[0].style.color = "";
2397 function showModal(content) {
2398 var modal = document.createElement("div");
2399 modal.id = "important";
2400 addClass(modal, "modal");
2401 modal.innerHTML = "<div class=\"modal-content\"><div class=\"close\" id=\"modal-close\">✕" +
2402 "</div><div class=\"whiter\"></div><span class=\"docblock\">" + content +
2404 document.getElementsByTagName("body")[0].appendChild(modal);
2405 document.getElementById("modal-close").onclick = hideModal;
2406 modal.onclick = hideModal;
2409 function hideModal() {
2410 var modal = document.getElementById("important");
2412 modal.parentNode.removeChild(modal);
2416 onEachLazy(document.getElementsByClassName("important-traits"), function(e) {
2417 e.onclick = function() {
2418 showModal(e.lastElementChild.innerHTML);
2422 // In the search display, allows to switch between tabs.
2423 function printTab(nb) {
2424 if (nb === 0 || nb === 1 || nb === 2) {
2428 onEachLazy(document.getElementById("titles").childNodes, function(elem) {
2429 if (nb_copy === 0) {
2430 addClass(elem, "selected");
2432 removeClass(elem, "selected");
2436 onEachLazy(document.getElementById("results").childNodes, function(elem) {
2438 elem.style.display = "";
2440 elem.style.display = "none";
2446 function putBackSearch(search_input) {
2447 if (search_input.value !== "") {
2448 addClass(main, "hidden");
2449 removeClass(document.getElementById("search"), "hidden");
2450 if (browserSupportsHistoryApi()) {
2451 history.replaceState(search_input.value,
2453 "?search=" + encodeURIComponent(search_input.value));
2459 search_input.onfocus = function() {
2460 putBackSearch(this);
2464 var params = getQueryStringParams();
2465 if (params && params.search) {
2466 addClass(main, "hidden");
2467 var search = document.getElementById("search");
2468 removeClass(search, "hidden");
2469 search.innerHTML = "<h3 style=\"text-align: center;\">Loading search results...</h3>";
2472 var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];
2474 sidebar_menu.onclick = function() {
2475 var sidebar = document.getElementsByClassName("sidebar")[0];
2476 if (hasClass(sidebar, "mobile") === true) {
2484 window.onresize = function() {
2488 autoCollapse(getPageId(), getCurrentValue("rustdoc-collapse") === "true");
2490 if (window.location.hash && window.location.hash.length > 0) {
2491 expandSection(window.location.hash.replace(/^#/, ""));
2495 onEachLazy(main.getElementsByClassName("loading-content"), function(e) {
2498 onEachLazy(main.childNodes, function(e) {
2499 // Unhide the actual content once loading is complete. Headers get
2500 // flex treatment for their horizontal layout, divs get block treatment
2501 // for vertical layout (column-oriented flex layout for divs caused
2502 // errors in mobile browsers).
2503 if (e.tagName === "H2" || e.tagName === "H3") {
2504 var nextTagName = e.nextElementSibling.tagName;
2505 if (nextTagName == "H2" || nextTagName == "H3") {
2506 e.nextElementSibling.style.display = "flex";
2508 e.nextElementSibling.style.display = "block";
2514 function addSearchOptions(crates) {
2515 var elem = document.getElementById("crate-search");
2520 var crates_text = [];
2521 if (Object.keys(crates).length > 1) {
2522 for (var crate in crates) {
2523 if (crates.hasOwnProperty(crate)) {
2524 crates_text.push(crate);
2528 crates_text.sort(function(a, b) {
2529 var lower_a = a.toLowerCase();
2530 var lower_b = b.toLowerCase();
2532 if (lower_a < lower_b) {
2534 } else if (lower_a > lower_b) {
2539 for (var i = 0; i < crates_text.length; ++i) {
2540 var option = document.createElement("option");
2541 option.value = crates_text[i];
2542 option.innerText = crates_text[i];
2543 elem.appendChild(option);
2547 window.addSearchOptions = addSearchOptions;
2550 // Sets the focus on the search bar at the top of the page
2551 function focusSearchBar() {
2552 document.getElementsByClassName("search-input")[0].focus();
2555 // Removes the focus from the search bar
2556 function defocusSearchBar() {
2557 document.getElementsByClassName("search-input")[0].blur();