2 /* global ALIASES, currentCrate, rootPath */
4 // Local js definitions:
5 /* global addClass, getCurrentValue, hasClass */
6 /* global onEachLazy, hasOwnProperty, 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 + " ", " ")
42 function getSearchInput() {
43 return document.getElementsByClassName("search-input")[0];
46 function getSearchElement() {
47 return document.getElementById("search");
50 // Sets the focus on the search bar at the top of the page
51 function focusSearchBar() {
52 getSearchInput().focus();
55 // Removes the focus from the search bar
56 function defocusSearchBar() {
57 getSearchInput().blur();
63 // This mapping table should match the discriminants of
64 // `rustdoc::html::item_type::ItemType` type in Rust.
65 var itemTypes = ["mod",
92 var disableShortcuts = getCurrentValue("rustdoc-disable-shortcuts") === "true";
93 var search_input = getSearchInput();
94 var searchTimeout = null;
96 // On the search screen, so you remain on the last tab you opened.
99 // 1 for "In Parameters"
100 // 2 for "In Return Types"
103 var mouseMovedAfterSearch = true;
105 var titleBeforeSearch = document.title;
107 function clearInputTimeout() {
108 if (searchTimeout !== null) {
109 clearTimeout(searchTimeout);
110 searchTimeout = null;
114 function getPageId() {
115 if (window.location.hash) {
116 var tmp = window.location.hash.replace(/^#/, "");
117 if (tmp.length > 0) {
124 function showSidebar() {
125 var elems = document.getElementsByClassName("sidebar-elems")[0];
127 addClass(elems, "show-it");
129 var sidebar = document.getElementsByClassName("sidebar")[0];
131 addClass(sidebar, "mobile");
132 var filler = document.getElementById("sidebar-filler");
134 var div = document.createElement("div");
135 div.id = "sidebar-filler";
136 sidebar.appendChild(div);
139 var themePickers = document.getElementsByClassName("theme-picker");
140 if (themePickers && themePickers.length > 0) {
141 themePickers[0].style.display = "none";
145 function hideSidebar() {
146 var elems = document.getElementsByClassName("sidebar-elems")[0];
148 removeClass(elems, "show-it");
150 var sidebar = document.getElementsByClassName("sidebar")[0];
151 removeClass(sidebar, "mobile");
152 var filler = document.getElementById("sidebar-filler");
156 document.getElementsByTagName("body")[0].style.marginTop = "";
157 var themePickers = document.getElementsByClassName("theme-picker");
158 if (themePickers && themePickers.length > 0) {
159 themePickers[0].style.display = null;
163 function showSearchResults(search) {
164 if (search === null || typeof search === 'undefined') {
165 search = getSearchElement();
167 addClass(main, "hidden");
168 removeClass(search, "hidden");
169 mouseMovedAfterSearch = false;
172 function hideSearchResults(search) {
173 if (search === null || typeof search === 'undefined') {
174 search = getSearchElement();
176 addClass(search, "hidden");
177 removeClass(main, "hidden");
180 // used for special search precedence
181 var TY_PRIMITIVE = itemTypes.indexOf("primitive");
182 var TY_KEYWORD = itemTypes.indexOf("keyword");
184 function getQueryStringParams() {
186 window.location.search.substring(1).split("&").
188 var pair = s.split("=");
189 params[decodeURIComponent(pair[0])] =
190 typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
195 function browserSupportsHistoryApi() {
196 return window.history && typeof window.history.pushState === "function";
199 function isHidden(elem) {
200 return elem.offsetHeight === 0;
203 var main = document.getElementById("main");
206 function handleHashes(ev) {
208 var search = getSearchElement();
209 if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
210 // This block occurs when clicking on an element in the navbar while
212 hideSearchResults(search);
213 var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
214 if (browserSupportsHistoryApi()) {
215 history.replaceState(hash, "", "?search=#" + hash);
217 elem = document.getElementById(hash);
219 elem.scrollIntoView();
222 // This part is used in case an element is not visible.
223 if (savedHash !== window.location.hash) {
224 savedHash = window.location.hash;
225 if (savedHash.length === 0) {
228 elem = document.getElementById(savedHash.slice(1)); // we remove the '#'
229 if (!elem || !isHidden(elem)) {
232 var parent = elem.parentNode;
233 if (parent && hasClass(parent, "impl-items")) {
234 // In case this is a trait implementation item, we first need to toggle
235 // the "Show hidden undocumented items".
236 onEachLazy(parent.getElementsByClassName("collapsed"), function(e) {
237 if (e.parentNode === parent) {
238 // Only click on the toggle we're looking for.
243 if (isHidden(elem)) {
244 // The whole parent is collapsed. We need to click on its toggle as well!
245 if (hasClass(parent.lastElementChild, "collapse-toggle")) {
246 parent.lastElementChild.click();
253 function highlightSourceLines(match, ev) {
254 if (typeof match === "undefined") {
255 // If we're in mobile mode, we should hide the sidebar in any case.
257 match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
262 var from = parseInt(match[1], 10);
264 if (typeof match[2] !== "undefined") {
265 to = parseInt(match[2], 10);
272 var elem = document.getElementById(from);
277 var x = document.getElementById(from);
282 onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
283 onEachLazy(e.getElementsByTagName("span"), function(i_e) {
284 removeClass(i_e, "line-highlighted");
287 for (var i = from; i <= to; ++i) {
288 elem = document.getElementById(i);
292 addClass(elem, "line-highlighted");
296 function onHashChange(ev) {
297 // If we're in mobile mode, we should hide the sidebar in any case.
299 var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
301 return highlightSourceLines(match, ev);
306 function expandSection(id) {
307 var elem = document.getElementById(id);
308 if (elem && isHidden(elem)) {
309 var h3 = elem.parentNode.previousElementSibling;
310 if (h3 && h3.tagName !== "H3") {
311 h3 = h3.previousElementSibling; // skip div.docblock
315 var collapses = h3.getElementsByClassName("collapse-toggle");
316 if (collapses.length > 0) {
317 // The element is not visible, we need to make it appear!
318 collapseDocs(collapses[0], "show");
324 // Gets the human-readable string for the virtual-key code of the
325 // given KeyboardEvent, ev.
327 // This function is meant as a polyfill for KeyboardEvent#key,
328 // since it is not supported in Trident. We also test for
329 // KeyboardEvent#keyCode because the handleShortcut handler is
330 // also registered for the keydown event, because Blink doesn't fire
331 // keypress on hitting the Escape key.
333 // So I guess you could say things are getting pretty interoperable.
334 function getVirtualKey(ev) {
335 if ("key" in ev && typeof ev.key != "undefined") {
339 var c = ev.charCode || ev.keyCode;
343 return String.fromCharCode(c);
346 function getHelpElement() {
347 return document.getElementById("help");
350 function displayHelp(display, ev, help) {
351 help = help ? help : getHelpElement();
352 if (display === true) {
353 if (hasClass(help, "hidden")) {
355 removeClass(help, "hidden");
356 addClass(document.body, "blur");
358 } else if (hasClass(help, "hidden") === false) {
360 addClass(help, "hidden");
361 removeClass(document.body, "blur");
365 function handleEscape(ev) {
366 var help = getHelpElement();
367 var search = getSearchElement();
368 if (hasClass(help, "hidden") === false) {
369 displayHelp(false, ev, help);
370 } else if (hasClass(search, "hidden") === false) {
373 hideSearchResults(search);
374 document.title = titleBeforeSearch;
379 function handleShortcut(ev) {
380 // Don't interfere with browser shortcuts
381 if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts === true) {
385 if (document.activeElement.tagName === "INPUT") {
386 switch (getVirtualKey(ev)) {
392 switch (getVirtualKey(ev)) {
399 displayHelp(false, ev);
412 displayHelp(true, ev);
419 function findParentElement(elem, tagName) {
421 if (elem && elem.tagName === tagName) {
424 elem = elem.parentNode;
429 document.addEventListener("keypress", handleShortcut);
430 document.addEventListener("keydown", handleShortcut);
432 function resetMouseMoved(ev) {
433 mouseMovedAfterSearch = true;
436 document.addEventListener("mousemove", resetMouseMoved);
438 var handleSourceHighlight = (function() {
439 var prev_line_id = 0;
441 var set_fragment = function(name) {
442 var x = window.scrollX,
444 if (browserSupportsHistoryApi()) {
445 history.replaceState(null, null, "#" + name);
446 highlightSourceLines();
448 location.replace("#" + name);
450 // Prevent jumps when selecting one or many lines
451 window.scrollTo(x, y);
454 return function(ev) {
455 var cur_line_id = parseInt(ev.target.id, 10);
458 if (ev.shiftKey && prev_line_id) {
459 // Swap selection if needed
460 if (prev_line_id > cur_line_id) {
461 var tmp = prev_line_id;
462 prev_line_id = cur_line_id;
466 set_fragment(prev_line_id + "-" + cur_line_id);
468 prev_line_id = cur_line_id;
470 set_fragment(cur_line_id);
475 document.addEventListener("click", function(ev) {
476 if (hasClass(ev.target, "collapse-toggle")) {
477 collapseDocs(ev.target, "toggle");
478 } else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
479 collapseDocs(ev.target.parentNode, "toggle");
480 } else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
481 handleSourceHighlight(ev);
482 } else if (hasClass(getHelpElement(), "hidden") === false) {
483 var help = getHelpElement();
484 var is_inside_help_popup = ev.target !== help && help.contains(ev.target);
485 if (is_inside_help_popup === false) {
486 addClass(help, "hidden");
487 removeClass(document.body, "blur");
490 // Making a collapsed element visible on onhashchange seems
492 var a = findParentElement(ev.target, "A");
494 expandSection(a.hash.replace(/^#/, ""));
500 var x = document.getElementsByClassName("version-selector");
502 x[0].onchange = function() {
504 url = document.location.href,
506 len = rootPath.match(/\.\.\//g).length + 1;
508 for (i = 0; i < len; ++i) {
509 match = url.match(/\/[^\/]*$/);
511 stripped = match[0] + stripped;
513 url = url.substring(0, url.length - match[0].length);
516 var selectedVersion = document.getElementsByClassName("version-selector")[0].value;
517 url += "/" + selectedVersion + stripped;
519 document.location.href = url;
525 * A function to compute the Levenshtein distance between two strings
526 * Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
527 * Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode
528 * This code is an unmodified version of the code written by Marco de Wit
529 * and was found at http://stackoverflow.com/a/18514751/745719
531 var levenshtein_row2 = [];
532 function levenshtein(s1, s2) {
536 var s1_len = s1.length, s2_len = s2.length;
537 if (s1_len && s2_len) {
538 var i1 = 0, i2 = 0, a, b, c, c2, row = levenshtein_row2;
539 while (i1 < s1_len) {
542 while (i2 < s2_len) {
543 c2 = s2.charCodeAt(i2);
547 for (i1 = 0; i1 < s1_len; ++i1) {
548 c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
550 b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
556 return s1_len + s2_len;
559 window.initSearch = function(rawSearchIndex) {
560 var MAX_LEV_DISTANCE = 3;
561 var MAX_RESULTS = 200;
562 var GENERICS_DATA = 1;
566 var NO_TYPE_FILTER = -1;
567 var currentResults, index, searchIndex;
569 var params = getQueryStringParams();
571 // Populate search bar with query string search term when provided,
572 // but only if the input bar is empty. This avoid the obnoxious issue
573 // where you start trying to do a search, and the index loads, and
574 // suddenly your search is gone!
575 if (search_input.value === "") {
576 search_input.value = params.search || "";
580 * Executes the query and builds an index of results
581 * @param {[Object]} query [The user query]
582 * @param {[type]} searchWords [The list of search words to query
584 * @param {[type]} filterCrates [Crate to search in if defined]
585 * @return {[type]} [A search index of results]
587 function execQuery(query, searchWords, filterCrates) {
588 function itemTypeFromName(typename) {
589 var length = itemTypes.length;
590 for (var i = 0; i < length; ++i) {
591 if (itemTypes[i] === typename) {
595 return NO_TYPE_FILTER;
598 var valLower = query.query.toLowerCase(),
600 typeFilter = itemTypeFromName(query.type),
601 results = {}, results_in_args = {}, results_returned = {},
602 split = valLower.split("::");
604 var length = split.length;
605 for (var z = 0; z < length; ++z) {
606 if (split[z] === "") {
612 function transformResults(results, isType) {
614 var length = results.length;
615 for (var i = 0; i < length; ++i) {
616 if (results[i].id > -1) {
617 var obj = searchIndex[results[i].id];
618 obj.lev = results[i].lev;
619 if (isType !== true || obj.type) {
620 var res = buildHrefAndPath(obj);
621 obj.displayPath = pathSplitter(res[0]);
622 obj.fullPath = obj.displayPath + obj.name;
623 // To be sure than it some items aren't considered as duplicate.
624 obj.fullPath += "|" + obj.ty;
627 if (out.length >= MAX_RESULTS) {
636 function sortResults(results, isType) {
638 for (var entry in results) {
639 if (hasOwnProperty(results, entry)) {
640 ar.push(results[entry]);
645 var nresults = results.length;
646 for (i = 0; i < nresults; ++i) {
647 results[i].word = searchWords[results[i].id];
648 results[i].item = searchIndex[results[i].id] || {};
650 // if there are no results then return to default and fail
651 if (results.length === 0) {
655 results.sort(function(aaa, bbb) {
658 // sort by exact match with regard to the last word (mismatch goes later)
659 a = (aaa.word !== val);
660 b = (bbb.word !== val);
661 if (a !== b) { return a - b; }
663 // Sort by non levenshtein results and then levenshtein results by the distance
664 // (less changes required to match means higher rankings)
667 if (a !== b) { return a - b; }
669 // sort by crate (non-current crate goes later)
670 a = (aaa.item.crate !== window.currentCrate);
671 b = (bbb.item.crate !== window.currentCrate);
672 if (a !== b) { return a - b; }
674 // sort by item name length (longer goes later)
677 if (a !== b) { return a - b; }
679 // sort by item name (lexicographically larger goes later)
682 if (a !== b) { return (a > b ? +1 : -1); }
684 // sort by index of keyword in item name (no literal occurrence goes later)
687 if (a !== b) { return a - b; }
688 // (later literal occurrence, if any, goes later)
691 if (a !== b) { return a - b; }
693 // special precedence for primitive and keyword pages
694 if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
695 (aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
698 if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
699 (bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
703 // sort by description (no description goes later)
704 a = (aaa.item.desc === "");
705 b = (bbb.item.desc === "");
706 if (a !== b) { return a - b; }
708 // sort by type (later occurrence in `itemTypes` goes later)
711 if (a !== b) { return a - b; }
713 // sort by path (lexicographically larger goes later)
716 if (a !== b) { return (a > b ? +1 : -1); }
722 var length = results.length;
723 for (i = 0; i < length; ++i) {
724 var result = results[i];
726 // this validation does not make sense when searching by types
727 if (result.dontValidate) {
730 var name = result.item.name.toLowerCase(),
731 path = result.item.path.toLowerCase(),
732 parent = result.item.parent;
734 if (isType !== true &&
735 validateResult(name, path, split, parent) === false)
740 return transformResults(results);
743 function extractGenerics(val) {
744 val = val.toLowerCase();
745 if (val.indexOf("<") !== -1) {
746 var values = val.substring(val.indexOf("<") + 1, val.lastIndexOf(">"));
748 name: val.substring(0, val.indexOf("<")),
749 generics: values.split(/\s*,\s*/),
758 function getObjectFromId(id) {
759 if (typeof id === "number") {
760 return searchIndex[id];
765 function checkGenerics(obj, val) {
766 // The names match, but we need to be sure that all generics kinda
768 var lev_distance = MAX_LEV_DISTANCE + 1;
769 if (val.generics.length > 0) {
770 if (obj.length > GENERICS_DATA &&
771 obj[GENERICS_DATA].length >= val.generics.length) {
772 var elems = obj[GENERICS_DATA].slice(0);
775 // We need to find the type that matches the most to remove it in order
777 var vlength = val.generics.length;
778 for (var y = 0; y < vlength; ++y) {
779 var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
780 var elength = elems.length;
781 var firstGeneric = getObjectFromId(val.generics[y]).name;
782 for (var x = 0; x < elength; ++x) {
783 var tmp_lev = levenshtein(getObjectFromId(elems[x]).name,
785 if (tmp_lev < lev.lev) {
790 if (lev.pos !== -1) {
791 elems.splice(lev.pos, 1);
792 lev_distance = Math.min(lev.lev, lev_distance);
796 return MAX_LEV_DISTANCE + 1;
799 return Math.ceil(total / done);
802 return MAX_LEV_DISTANCE + 1;
805 // Check for type name and type generics (if any).
806 function checkType(obj, val, literalSearch) {
807 var lev_distance = MAX_LEV_DISTANCE + 1;
809 if (obj[NAME] === val.name) {
810 if (literalSearch === true) {
811 if (val.generics && val.generics.length !== 0) {
812 if (obj.length > GENERICS_DATA &&
813 obj[GENERICS_DATA].length >= val.generics.length) {
814 var elems = obj[GENERICS_DATA].slice(0);
817 for (var y = 0; allFound === true && y < val.generics.length; ++y) {
819 var firstGeneric = getObjectFromId(val.generics[y]).name;
820 for (x = 0; allFound === false && x < elems.length; ++x) {
821 allFound = getObjectFromId(elems[x]).name === firstGeneric;
823 if (allFound === true) {
824 elems.splice(x - 1, 1);
827 if (allFound === true) {
836 // If the type has generics but don't match, then it won't return at this point.
837 // Otherwise, `checkGenerics` will return 0 and it'll return.
838 if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length !== 0) {
839 var tmp_lev = checkGenerics(obj, val);
840 if (tmp_lev <= MAX_LEV_DISTANCE) {
847 // Names didn't match so let's check if one of the generic types could.
848 if (literalSearch === true) {
849 if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
850 var length = obj[GENERICS_DATA].length;
851 for (x = 0; x < length; ++x) {
852 if (obj[GENERICS_DATA][x] === val.name) {
859 lev_distance = Math.min(levenshtein(obj[NAME], val.name), lev_distance);
860 if (lev_distance <= MAX_LEV_DISTANCE) {
861 // The generics didn't match but the name kinda did so we give it
862 // a levenshtein distance value that isn't *this* good so it goes
863 // into the search results but not too high.
864 lev_distance = Math.ceil((checkGenerics(obj, val) + lev_distance) / 2);
865 } else if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
866 // We can check if the type we're looking for is inside the generics!
867 var olength = obj[GENERICS_DATA].length;
868 for (x = 0; x < olength; ++x) {
869 lev_distance = Math.min(levenshtein(obj[GENERICS_DATA][x], val.name),
873 // Now whatever happens, the returned distance is "less good" so we should mark it
874 // as such, and so we add 1 to the distance to make it "less good".
875 return lev_distance + 1;
878 function findArg(obj, val, literalSearch, typeFilter) {
879 var lev_distance = MAX_LEV_DISTANCE + 1;
881 if (obj && obj.type && obj.type[INPUTS_DATA] && obj.type[INPUTS_DATA].length > 0) {
882 var length = obj.type[INPUTS_DATA].length;
883 for (var i = 0; i < length; i++) {
884 var tmp = obj.type[INPUTS_DATA][i];
885 if (typePassesFilter(typeFilter, tmp[1]) === false) {
888 tmp = checkType(tmp, val, literalSearch);
889 if (literalSearch === true) {
895 lev_distance = Math.min(tmp, lev_distance);
896 if (lev_distance === 0) {
901 return literalSearch === true ? false : lev_distance;
904 function checkReturned(obj, val, literalSearch, typeFilter) {
905 var lev_distance = MAX_LEV_DISTANCE + 1;
907 if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
908 var ret = obj.type[OUTPUT_DATA];
909 if (typeof ret[0] === "string") {
912 for (var x = 0; x < ret.length; ++x) {
914 if (typePassesFilter(typeFilter, tmp[1]) === false) {
917 tmp = checkType(tmp, val, literalSearch);
918 if (literalSearch === true) {
924 lev_distance = Math.min(tmp, lev_distance);
925 if (lev_distance === 0) {
930 return literalSearch === true ? false : lev_distance;
933 function checkPath(contains, lastElem, ty) {
934 if (contains.length === 0) {
937 var ret_lev = MAX_LEV_DISTANCE + 1;
938 var path = ty.path.split("::");
940 if (ty.parent && ty.parent.name) {
941 path.push(ty.parent.name.toLowerCase());
944 var length = path.length;
945 var clength = contains.length;
946 if (clength > length) {
947 return MAX_LEV_DISTANCE + 1;
949 for (var i = 0; i < length; ++i) {
950 if (i + clength > length) {
955 for (var x = 0; x < clength; ++x) {
956 var lev = levenshtein(path[i + x], contains[x]);
957 if (lev > MAX_LEV_DISTANCE) {
963 if (aborted === false) {
964 ret_lev = Math.min(ret_lev, Math.round(lev_total / clength));
970 function typePassesFilter(filter, type) {
972 if (filter <= NO_TYPE_FILTER) return true;
975 if (filter === type) return true;
977 // Match related items
978 var name = itemTypes[type];
979 switch (itemTypes[filter]) {
981 return name === "associatedconstant";
983 return name === "method" || name === "tymethod";
985 return name === "primitive" || name === "associatedtype";
987 return name === "traitalias";
994 function generateId(ty) {
995 if (ty.parent && ty.parent.name) {
996 return itemTypes[ty.ty] + ty.path + ty.parent.name + ty.name;
998 return itemTypes[ty.ty] + ty.path + ty.name;
1001 function createAliasFromItem(item) {
1008 parent: item.parent,
1014 function handleAliases(ret, query, filterCrates) {
1015 // We separate aliases and crate aliases because we want to have current crate
1016 // aliases to be before the others in the displayed results.
1018 var crateAliases = [];
1020 if (filterCrates !== undefined) {
1021 if (ALIASES[filterCrates] && ALIASES[filterCrates][query.search]) {
1022 for (i = 0; i < ALIASES[filterCrates][query.search].length; ++i) {
1024 createAliasFromItem(
1025 searchIndex[ALIASES[filterCrates][query.search][i]]));
1029 Object.keys(ALIASES).forEach(function(crate) {
1030 if (ALIASES[crate][query.search]) {
1031 var pushTo = crate === window.currentCrate ? crateAliases : aliases;
1032 for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
1034 createAliasFromItem(
1035 searchIndex[ALIASES[crate][query.search][i]]));
1041 var sortFunc = function(aaa, bbb) {
1042 if (aaa.path < bbb.path) {
1044 } else if (aaa.path === bbb.path) {
1049 crateAliases.sort(sortFunc);
1050 aliases.sort(sortFunc);
1052 var pushFunc = function(alias) {
1053 alias.alias = query.raw;
1054 var res = buildHrefAndPath(alias);
1055 alias.displayPath = pathSplitter(res[0]);
1056 alias.fullPath = alias.displayPath + alias.name;
1057 alias.href = res[1];
1059 ret.others.unshift(alias);
1060 if (ret.others.length > MAX_RESULTS) {
1064 onEach(aliases, pushFunc);
1065 onEach(crateAliases, pushFunc);
1068 // quoted values mean literal search
1069 var nSearchWords = searchWords.length;
1075 if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
1076 val.charAt(val.length - 1) === val.charAt(0))
1078 val = extractGenerics(val.substr(1, val.length - 2));
1079 for (i = 0; i < nSearchWords; ++i) {
1080 if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
1083 in_args = findArg(searchIndex[i], val, true, typeFilter);
1084 returned = checkReturned(searchIndex[i], val, true, typeFilter);
1085 ty = searchIndex[i];
1086 fullId = generateId(ty);
1088 if (searchWords[i] === val.name
1089 && typePassesFilter(typeFilter, searchIndex[i].ty)
1090 && results[fullId] === undefined) {
1097 if (in_args === true && results_in_args[fullId] === undefined) {
1098 results_in_args[fullId] = {
1104 if (returned === true && results_returned[fullId] === undefined) {
1105 results_returned[fullId] = {
1112 query.inputs = [val];
1115 // searching by type
1116 } else if (val.search("->") > -1) {
1117 var trimmer = function(s) { return s.trim(); };
1118 var parts = val.split("->").map(trimmer);
1119 var input = parts[0];
1120 // sort inputs so that order does not matter
1121 var inputs = input.split(",").map(trimmer).sort();
1122 for (i = 0; i < inputs.length; ++i) {
1123 inputs[i] = extractGenerics(inputs[i]);
1125 var output = extractGenerics(parts[1]);
1127 for (i = 0; i < nSearchWords; ++i) {
1128 if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
1131 var type = searchIndex[i].type;
1132 ty = searchIndex[i];
1136 fullId = generateId(ty);
1138 returned = checkReturned(ty, output, true, NO_TYPE_FILTER);
1139 if (output.name === "*" || returned === true) {
1141 var is_module = false;
1143 if (input === "*") {
1146 var allFound = true;
1147 for (var it = 0; allFound === true && it < inputs.length; it++) {
1148 allFound = checkType(type, inputs[it], true);
1152 if (in_args === true) {
1153 results_in_args[fullId] = {
1159 if (returned === true) {
1160 results_returned[fullId] = {
1166 if (is_module === true) {
1175 query.inputs = inputs.map(function(input) {
1178 query.output = output.name;
1180 query.inputs = [val];
1183 // gather matching search results up to a certain maximum
1184 val = val.replace(/\_/g, "");
1186 var valGenerics = extractGenerics(val);
1188 var paths = valLower.split("::");
1190 for (j = 0; j < paths.length; ++j) {
1191 if (paths[j] === "") {
1196 val = paths[paths.length - 1];
1197 var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
1200 for (j = 0; j < nSearchWords; ++j) {
1201 ty = searchIndex[j];
1202 if (!ty || (filterCrates !== undefined && ty.crate !== filterCrates)) {
1206 if (paths.length > 1) {
1207 lev = checkPath(contains, paths[paths.length - 1], ty);
1208 if (lev > MAX_LEV_DISTANCE) {
1210 } else if (lev > 0) {
1215 returned = MAX_LEV_DISTANCE + 1;
1216 in_args = MAX_LEV_DISTANCE + 1;
1218 // we want lev results to go lower than others
1219 lev = MAX_LEV_DISTANCE + 1;
1220 fullId = generateId(ty);
1222 if (searchWords[j].indexOf(split[i]) > -1 ||
1223 searchWords[j].indexOf(val) > -1 ||
1224 searchWords[j].replace(/_/g, "").indexOf(val) > -1)
1226 // filter type: ... queries
1227 if (typePassesFilter(typeFilter, ty.ty) && results[fullId] === undefined) {
1228 index = searchWords[j].replace(/_/g, "").indexOf(val);
1231 if ((lev = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
1232 if (typePassesFilter(typeFilter, ty.ty) === false) {
1233 lev = MAX_LEV_DISTANCE + 1;
1238 in_args = findArg(ty, valGenerics, false, typeFilter);
1239 returned = checkReturned(ty, valGenerics, false, typeFilter);
1242 if (lev > 0 && val.length > 3 && searchWords[j].indexOf(val) > -1) {
1243 if (val.length < 6) {
1249 if (in_args <= MAX_LEV_DISTANCE) {
1250 if (results_in_args[fullId] === undefined) {
1251 results_in_args[fullId] = {
1257 results_in_args[fullId].lev =
1258 Math.min(results_in_args[fullId].lev, in_args);
1260 if (returned <= MAX_LEV_DISTANCE) {
1261 if (results_returned[fullId] === undefined) {
1262 results_returned[fullId] = {
1268 results_returned[fullId].lev =
1269 Math.min(results_returned[fullId].lev, returned);
1271 if (index !== -1 || lev <= MAX_LEV_DISTANCE) {
1272 if (index !== -1 && paths.length < 2) {
1275 if (results[fullId] === undefined) {
1282 results[fullId].lev = Math.min(results[fullId].lev, lev);
1288 "in_args": sortResults(results_in_args, true),
1289 "returned": sortResults(results_returned, true),
1290 "others": sortResults(results),
1292 handleAliases(ret, query, filterCrates);
1297 * Validate performs the following boolean logic. For example:
1298 * "File::open" will give IF A PARENT EXISTS => ("file" && "open")
1299 * exists in (name || path || parent) OR => ("file" && "open") exists in
1302 * This could be written functionally, but I wanted to minimise
1303 * functions on stack.
1305 * @param {[string]} name [The name of the result]
1306 * @param {[string]} path [The path of the result]
1307 * @param {[string]} keys [The keys to be used (["file", "open"])]
1308 * @param {[object]} parent [The parent of the result]
1309 * @return {[boolean]} [Whether the result is valid or not]
1311 function validateResult(name, path, keys, parent) {
1312 for (var i = 0; i < keys.length; ++i) {
1313 // each check is for validation so we negate the conditions and invalidate
1315 // check for an exact name match
1316 name.indexOf(keys[i]) > -1 ||
1317 // then an exact path match
1318 path.indexOf(keys[i]) > -1 ||
1319 // next if there is a parent, check for exact parent match
1320 (parent !== undefined && parent.name !== undefined &&
1321 parent.name.toLowerCase().indexOf(keys[i]) > -1) ||
1322 // lastly check to see if the name was a levenshtein match
1323 levenshtein(name, keys[i]) <= MAX_LEV_DISTANCE)) {
1330 function getQuery(raw) {
1331 var matches, type, query;
1334 matches = query.match(/^(fn|mod|struct|enum|trait|type|const|macro)\s*:\s*/i);
1336 type = matches[1].replace(/^const$/, "constant");
1337 query = query.substring(matches[0].length);
1348 function initSearchNav() {
1351 var click_func = function(e) {
1353 // to retrieve the real "owner" of the event.
1354 while (el.tagName !== "TR") {
1357 var dst = e.target.getElementsByTagName("a");
1358 if (dst.length < 1) {
1362 if (window.location.pathname === dst.pathname) {
1363 hideSearchResults();
1364 document.location.href = dst.href;
1367 var mouseover_func = function(e) {
1368 if (mouseMovedAfterSearch) {
1370 // to retrieve the real "owner" of the event.
1371 while (el.tagName !== "TR") {
1374 clearTimeout(hoverTimeout);
1375 hoverTimeout = setTimeout(function() {
1376 onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1377 onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1378 removeClass(i_e, "highlighted");
1381 addClass(el, "highlighted");
1385 onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1386 onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1387 i_e.onclick = click_func;
1388 i_e.onmouseover = mouseover_func;
1392 search_input.onkeydown = function(e) {
1393 // "actives" references the currently highlighted item in each search tab.
1394 // Each array in "actives" represents a tab.
1395 var actives = [[], [], []];
1396 // "current" is used to know which tab we're looking into.
1398 onEachLazy(document.getElementById("results").childNodes, function(e) {
1399 onEachLazy(e.getElementsByClassName("highlighted"), function(e) {
1400 actives[current].push(e);
1405 if (e.which === 38) { // up
1406 if (!actives[currentTab].length ||
1407 !actives[currentTab][0].previousElementSibling) {
1411 addClass(actives[currentTab][0].previousElementSibling, "highlighted");
1412 removeClass(actives[currentTab][0], "highlighted");
1414 } else if (e.which === 40) { // down
1415 if (!actives[currentTab].length) {
1416 var results = document.getElementById("results").childNodes;
1417 if (results.length > 0) {
1418 var res = results[currentTab].getElementsByClassName("result");
1419 if (res.length > 0) {
1420 addClass(res[0], "highlighted");
1423 } else if (actives[currentTab][0].nextElementSibling) {
1424 addClass(actives[currentTab][0].nextElementSibling, "highlighted");
1425 removeClass(actives[currentTab][0], "highlighted");
1428 } else if (e.which === 13) { // return
1429 if (actives[currentTab].length) {
1430 document.location.href =
1431 actives[currentTab][0].getElementsByTagName("a")[0].href;
1433 } else if (e.which === 9) { // tab
1435 printTab(currentTab > 0 ? currentTab - 1 : 2);
1437 printTab(currentTab > 1 ? 0 : currentTab + 1);
1440 } else if (e.which === 16) { // shift
1441 // Does nothing, it's just to avoid losing "focus" on the highlighted element.
1442 } else if (actives[currentTab].length > 0) {
1443 removeClass(actives[currentTab][0], "highlighted");
1448 function buildHrefAndPath(item) {
1451 var type = itemTypes[item.ty];
1452 var name = item.name;
1453 var path = item.path;
1455 if (type === "mod") {
1456 displayPath = path + "::";
1457 href = rootPath + path.replace(/::/g, "/") + "/" +
1458 name + "/index.html";
1459 } else if (type === "primitive" || type === "keyword") {
1461 href = rootPath + path.replace(/::/g, "/") +
1462 "/" + type + "." + name + ".html";
1463 } else if (type === "externcrate") {
1465 href = rootPath + name + "/index.html";
1466 } else if (item.parent !== undefined) {
1467 var myparent = item.parent;
1468 var anchor = "#" + type + "." + name;
1469 var parentType = itemTypes[myparent.ty];
1470 var pageType = parentType;
1471 var pageName = myparent.name;
1473 if (parentType === "primitive") {
1474 displayPath = myparent.name + "::";
1475 } else if (type === "structfield" && parentType === "variant") {
1476 // Structfields belonging to variants are special: the
1477 // final path element is the enum name.
1478 var splitPath = item.path.split("::");
1479 var enumName = splitPath.pop();
1480 path = splitPath.join("::");
1481 displayPath = path + "::" + enumName + "::" + myparent.name + "::";
1482 anchor = "#variant." + myparent.name + ".field." + name;
1484 pageName = enumName;
1486 displayPath = path + "::" + myparent.name + "::";
1488 href = rootPath + path.replace(/::/g, "/") +
1493 displayPath = item.path + "::";
1494 href = rootPath + item.path.replace(/::/g, "/") +
1495 "/" + type + "." + name + ".html";
1497 return [displayPath, href];
1500 function escape(content) {
1501 var h1 = document.createElement("h1");
1502 h1.textContent = content;
1503 return h1.innerHTML;
1506 function pathSplitter(path) {
1507 var tmp = "<span>" + path.replace(/::/g, "::</span><span>");
1508 if (tmp.endsWith("<span>")) {
1509 return tmp.slice(0, tmp.length - 6);
1514 function addTab(array, query, display) {
1515 var extraStyle = "";
1516 if (display === false) {
1517 extraStyle = " style=\"display: none;\"";
1521 var duplicates = {};
1523 if (array.length > 0) {
1524 output = "<table class=\"search-results\"" + extraStyle + ">";
1526 array.forEach(function(item) {
1530 type = itemTypes[item.ty];
1532 if (item.is_alias !== true) {
1533 if (duplicates[item.fullPath]) {
1536 duplicates[item.fullPath] = true;
1540 output += "<tr class=\"" + type + " result\"><td>" +
1541 "<a href=\"" + item.href + "\">" +
1542 (item.is_alias === true ?
1543 ("<span class=\"alias\"><b>" + item.alias + " </b></span><span " +
1544 "class=\"grey\"><i> - see </i></span>") : "") +
1545 item.displayPath + "<span class=\"" + type + "\">" +
1546 name + "</span></a></td><td>" +
1547 "<a href=\"" + item.href + "\">" +
1548 "<span class=\"desc\">" + escape(item.desc) +
1549 " </span></a></td></tr>";
1551 output += "</table>";
1553 output = "<div class=\"search-failed\"" + extraStyle + ">No results :(<br/>" +
1554 "Try on <a href=\"https://duckduckgo.com/?q=" +
1555 encodeURIComponent("rust " + query.query) +
1556 "\">DuckDuckGo</a>?<br/><br/>" +
1557 "Or try looking in one of these:<ul><li>The <a " +
1558 "href=\"https://doc.rust-lang.org/reference/index.html\">Rust Reference</a> " +
1559 " for technical details about the language.</li><li><a " +
1560 "href=\"https://doc.rust-lang.org/rust-by-example/index.html\">Rust By " +
1561 "Example</a> for expository code examples.</a></li><li>The <a " +
1562 "href=\"https://doc.rust-lang.org/book/index.html\">Rust Book</a> for " +
1563 "introductions to language features and the language itself.</li><li><a " +
1564 "href=\"https://docs.rs\">Docs.rs</a> for documentation of crates released on" +
1565 " <a href=\"https://crates.io/\">crates.io</a>.</li></ul></div>";
1567 return [output, length];
1570 function makeTabHeader(tabNb, text, nbElems) {
1571 if (currentTab === tabNb) {
1572 return "<div class=\"selected\">" + text +
1573 " <div class=\"count\">(" + nbElems + ")</div></div>";
1575 return "<div>" + text + " <div class=\"count\">(" + nbElems + ")</div></div>";
1578 function showResults(results) {
1579 if (results.others.length === 1 &&
1580 getCurrentValue("rustdoc-go-to-only-result") === "true") {
1581 var elem = document.createElement("a");
1582 elem.href = results.others[0].href;
1583 elem.style.display = "none";
1584 // For firefox, we need the element to be in the DOM so it can be clicked.
1585 document.body.appendChild(elem);
1588 var query = getQuery(search_input.value);
1590 currentResults = query.id;
1592 var ret_others = addTab(results.others, query);
1593 var ret_in_args = addTab(results.in_args, query, false);
1594 var ret_returned = addTab(results.returned, query, false);
1596 var output = "<h1>Results for " + escape(query.query) +
1597 (query.type ? " (type: " + escape(query.type) + ")" : "") + "</h1>" +
1598 "<div id=\"titles\">" +
1599 makeTabHeader(0, "In Names", ret_others[1]) +
1600 makeTabHeader(1, "In Parameters", ret_in_args[1]) +
1601 makeTabHeader(2, "In Return Types", ret_returned[1]) +
1602 "</div><div id=\"results\">" +
1603 ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
1605 var search = getSearchElement();
1606 search.innerHTML = output;
1607 showSearchResults(search);
1608 var tds = search.getElementsByTagName("td");
1610 if (tds.length > 0) {
1611 td_width = tds[0].offsetWidth;
1613 var width = search.offsetWidth - 40 - td_width;
1614 onEachLazy(search.getElementsByClassName("desc"), function(e) {
1615 e.style.width = width + "px";
1618 var elems = document.getElementById("titles").childNodes;
1619 elems[0].onclick = function() { printTab(0); };
1620 elems[1].onclick = function() { printTab(1); };
1621 elems[2].onclick = function() { printTab(2); };
1622 printTab(currentTab);
1625 function execSearch(query, searchWords, filterCrates) {
1626 function getSmallest(arrays, positions, notDuplicates) {
1629 for (var it = 0; it < positions.length; ++it) {
1630 if (arrays[it].length > positions[it] &&
1631 (start === null || start > arrays[it][positions[it]].lev) &&
1632 !notDuplicates[arrays[it][positions[it]].fullPath]) {
1633 start = arrays[it][positions[it]].lev;
1639 function mergeArrays(arrays) {
1642 var notDuplicates = {};
1644 for (var x = 0; x < arrays.length; ++x) {
1647 while (ret.length < MAX_RESULTS) {
1648 var smallest = getSmallest(arrays, positions, notDuplicates);
1650 if (smallest === null) {
1653 for (x = 0; x < arrays.length && ret.length < MAX_RESULTS; ++x) {
1654 if (arrays[x].length > positions[x] &&
1655 arrays[x][positions[x]].lev === smallest &&
1656 !notDuplicates[arrays[x][positions[x]].fullPath]) {
1657 ret.push(arrays[x][positions[x]]);
1658 notDuplicates[arrays[x][positions[x]].fullPath] = true;
1666 var queries = query.raw.split(",");
1673 for (var i = 0; i < queries.length; ++i) {
1674 query = queries[i].trim();
1675 if (query.length !== 0) {
1676 var tmp = execQuery(getQuery(query), searchWords, filterCrates);
1678 results.in_args.push(tmp.in_args);
1679 results.returned.push(tmp.returned);
1680 results.others.push(tmp.others);
1683 if (queries.length > 1) {
1685 "in_args": mergeArrays(results.in_args),
1686 "returned": mergeArrays(results.returned),
1687 "others": mergeArrays(results.others),
1691 "in_args": results.in_args[0],
1692 "returned": results.returned[0],
1693 "others": results.others[0],
1697 function getFilterCrates() {
1698 var elem = document.getElementById("crate-search");
1700 if (elem && elem.value !== "All crates" && hasOwnProperty(rawSearchIndex, elem.value)) {
1706 function search(e, forced) {
1707 var params = getQueryStringParams();
1708 var query = getQuery(search_input.value.trim());
1714 if (query.query.length === 0) {
1717 if (forced !== true && query.id === currentResults) {
1718 if (query.query.length > 0) {
1719 putBackSearch(search_input);
1724 // Update document title to maintain a meaningful browser history
1725 document.title = "Results for " + query.query + " - Rust";
1727 // Because searching is incremental by character, only the most
1728 // recent search query is added to the browser history.
1729 if (browserSupportsHistoryApi()) {
1730 if (!history.state && !params.search) {
1731 history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
1733 history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
1737 var filterCrates = getFilterCrates();
1738 showResults(execSearch(query, index, filterCrates));
1741 function buildIndex(rawSearchIndex) {
1743 var searchWords = [];
1745 var currentIndex = 0;
1747 for (var crate in rawSearchIndex) {
1748 if (!hasOwnProperty(rawSearchIndex, crate)) { continue; }
1752 searchWords.push(crate);
1755 ty: 1, // == ExternCrate
1758 desc: rawSearchIndex[crate].doc,
1763 // an array of [(Number) item type,
1765 // (String) full path or empty string for previous path,
1766 // (String) description,
1767 // (Number | null) the parent path index to `paths`]
1768 // (Object | null) the type of the function (if any)
1769 var items = rawSearchIndex[crate].i;
1770 // an array of [(Number) item type,
1772 var paths = rawSearchIndex[crate].p;
1773 // a array of [(String) alias name
1774 // [Number] index to items]
1775 var aliases = rawSearchIndex[crate].a;
1777 // convert `rawPaths` entries into object form
1778 var len = paths.length;
1779 for (i = 0; i < len; ++i) {
1780 paths[i] = {ty: paths[i][0], name: paths[i][1]};
1783 // convert `items` into an object form, and construct word indices.
1785 // before any analysis is performed lets gather the search terms to
1786 // search against apart from the rest of the data. This is a quick
1787 // operation that is cached for the life of the page state so that
1788 // all other search operations have access to this cached data for
1789 // faster analysis operations
1792 for (i = 0; i < len; ++i) {
1793 var rawRow = items[i];
1795 rawRow[2] = lastPath;
1803 parent: paths[rawRow[4]],
1806 searchIndex.push(row);
1807 if (typeof row.name === "string") {
1808 var word = row.name.toLowerCase();
1809 searchWords.push(word);
1811 searchWords.push("");
1813 lastPath = row.path;
1818 ALIASES[crate] = {};
1819 var j, local_aliases;
1820 for (var alias_name in aliases) {
1821 if (!aliases.hasOwnProperty(alias_name)) { continue; }
1823 if (!ALIASES[crate].hasOwnProperty(alias_name)) {
1824 ALIASES[crate][alias_name] = [];
1826 local_aliases = aliases[alias_name];
1827 for (j = 0; j < local_aliases.length; ++j) {
1828 ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
1832 currentIndex += crateSize;
1837 function startSearch() {
1838 var callback = function() {
1839 clearInputTimeout();
1840 if (search_input.value.length === 0) {
1841 if (browserSupportsHistoryApi()) {
1842 history.replaceState("", window.currentCrate + " - Rust", "?search=");
1844 hideSearchResults();
1846 searchTimeout = setTimeout(search, 500);
1849 search_input.onkeyup = callback;
1850 search_input.oninput = callback;
1851 document.getElementsByClassName("search-form")[0].onsubmit = function(e) {
1853 clearInputTimeout();
1856 search_input.onchange = function(e) {
1857 if (e.target !== document.activeElement) {
1858 // To prevent doing anything when it's from a blur event.
1861 // Do NOT e.preventDefault() here. It will prevent pasting.
1862 clearInputTimeout();
1863 // zero-timeout necessary here because at the time of event handler execution the
1864 // pasted content is not in the input field yet. Shouldn’t make any difference for
1866 setTimeout(search, 0);
1868 search_input.onpaste = search_input.onchange;
1870 var selectCrate = document.getElementById("crate-search");
1872 selectCrate.onchange = function() {
1873 updateLocalStorage("rustdoc-saved-filter-crate", selectCrate.value);
1874 search(undefined, true);
1878 // Push and pop states are used to add search results to the browser
1880 if (browserSupportsHistoryApi()) {
1881 // Store the previous <title> so we can revert back to it later.
1882 var previousTitle = document.title;
1884 window.addEventListener("popstate", function(e) {
1885 var params = getQueryStringParams();
1886 // Revert to the previous title manually since the History
1887 // API ignores the title parameter.
1888 document.title = previousTitle;
1889 // When browsing forward to search results the previous
1890 // search will be repeated, so the currentResults are
1891 // cleared to ensure the search is successful.
1892 currentResults = null;
1893 // Synchronize search bar with query string state and
1894 // perform the search. This will empty the bar if there's
1895 // nothing there, which lets you really go back to a
1896 // previous state with nothing in the bar.
1897 if (params.search && params.search.length > 0) {
1898 search_input.value = params.search;
1899 // Some browsers fire "onpopstate" for every page load
1900 // (Chrome), while others fire the event only when actually
1901 // popping a state (Firefox), which is why search() is
1902 // called both here and at the end of the startSearch()
1906 search_input.value = "";
1907 // When browsing back from search results the main page
1908 // visibility must be reset.
1909 hideSearchResults();
1916 index = buildIndex(rawSearchIndex);
1919 // Draw a convenient sidebar of known crates if we have a listing
1920 if (rootPath === "../" || rootPath === "./") {
1921 var sidebar = document.getElementsByClassName("sidebar-elems")[0];
1923 var div = document.createElement("div");
1924 div.className = "block crate";
1925 div.innerHTML = "<h3>Crates</h3>";
1926 var ul = document.createElement("ul");
1927 div.appendChild(ul);
1930 for (var crate in rawSearchIndex) {
1931 if (!hasOwnProperty(rawSearchIndex, crate)) {
1937 for (var i = 0; i < crates.length; ++i) {
1938 var klass = "crate";
1939 if (rootPath !== "./" && crates[i] === window.currentCrate) {
1940 klass += " current";
1942 var link = document.createElement("a");
1943 link.href = rootPath + crates[i] + "/index.html";
1944 link.title = rawSearchIndex[crates[i]].doc;
1945 link.className = klass;
1946 link.textContent = crates[i];
1948 var li = document.createElement("li");
1949 li.appendChild(link);
1952 sidebar.appendChild(div);
1958 // delayed sidebar rendering.
1959 window.initSidebarItems = function(items) {
1960 var sidebar = document.getElementsByClassName("sidebar-elems")[0];
1961 var current = window.sidebarCurrent;
1963 function block(shortty, longty) {
1964 var filtered = items[shortty];
1969 var div = document.createElement("div");
1970 div.className = "block " + shortty;
1971 var h3 = document.createElement("h3");
1972 h3.textContent = longty;
1973 div.appendChild(h3);
1974 var ul = document.createElement("ul");
1976 var length = filtered.length;
1977 for (var i = 0; i < length; ++i) {
1978 var item = filtered[i];
1980 var desc = item[1]; // can be null
1982 var klass = shortty;
1983 if (name === current.name && shortty === current.ty) {
1984 klass += " current";
1987 if (shortty === "mod") {
1988 path = name + "/index.html";
1990 path = shortty + "." + name + ".html";
1992 var link = document.createElement("a");
1993 link.href = current.relpath + path;
1995 link.className = klass;
1996 link.textContent = name;
1997 var li = document.createElement("li");
1998 li.appendChild(link);
2001 div.appendChild(ul);
2003 sidebar.appendChild(div);
2007 block("primitive", "Primitive Types");
2008 block("mod", "Modules");
2009 block("macro", "Macros");
2010 block("struct", "Structs");
2011 block("enum", "Enums");
2012 block("union", "Unions");
2013 block("constant", "Constants");
2014 block("static", "Statics");
2015 block("trait", "Traits");
2016 block("fn", "Functions");
2017 block("type", "Type Definitions");
2018 block("foreigntype", "Foreign Types");
2019 block("keyword", "Keywords");
2020 block("traitalias", "Trait Aliases");
2023 window.register_implementors = function(imp) {
2024 var implementors = document.getElementById("implementors-list");
2025 var synthetic_implementors = document.getElementById("synthetic-implementors-list");
2027 if (synthetic_implementors) {
2028 // This `inlined_types` variable is used to avoid having the same implementation
2029 // showing up twice. For example "String" in the "Sync" doc page.
2031 // By the way, this is only used by and useful for traits implemented automatically
2032 // (like "Send" and "Sync").
2033 var inlined_types = new Set();
2034 onEachLazy(synthetic_implementors.getElementsByClassName("impl"), function(el) {
2035 var aliases = el.getAttribute("aliases");
2039 aliases.split(",").forEach(function(alias) {
2040 inlined_types.add(alias);
2045 var libs = Object.getOwnPropertyNames(imp);
2046 var llength = libs.length;
2047 for (var i = 0; i < llength; ++i) {
2048 if (libs[i] === currentCrate) { continue; }
2049 var structs = imp[libs[i]];
2051 var slength = structs.length;
2053 for (var j = 0; j < slength; ++j) {
2054 var struct = structs[j];
2056 var list = struct.synthetic ? synthetic_implementors : implementors;
2058 if (struct.synthetic) {
2059 var stlength = struct.types.length;
2060 for (var k = 0; k < stlength; k++) {
2061 if (inlined_types.has(struct.types[k])) {
2062 continue struct_loop;
2064 inlined_types.add(struct.types[k]);
2068 var code = document.createElement("code");
2069 code.innerHTML = struct.text;
2071 var x = code.getElementsByTagName("a");
2072 var xlength = x.length;
2073 for (var it = 0; it < xlength; it++) {
2074 var href = x[it].getAttribute("href");
2075 if (href && href.indexOf("http") !== 0) {
2076 x[it].setAttribute("href", rootPath + href);
2079 var display = document.createElement("h3");
2080 addClass(display, "impl");
2081 display.innerHTML = "<span class=\"in-band\"><table class=\"table-display\">" +
2082 "<tbody><tr><td><code>" + code.outerHTML + "</code></td><td></td></tr>" +
2083 "</tbody></table></span>";
2084 list.appendChild(display);
2088 if (window.pending_implementors) {
2089 window.register_implementors(window.pending_implementors);
2092 function labelForToggleButton(sectionIsCollapsed) {
2093 if (sectionIsCollapsed) {
2094 // button will expand the section
2097 // button will collapse the section
2098 // note that this text is also set in the HTML template in render.rs
2099 return "\u2212"; // "\u2212" is "−" minus sign
2102 function onEveryMatchingChild(elem, className, func) {
2103 if (elem && className && func) {
2104 var length = elem.childNodes.length;
2105 var nodes = elem.childNodes;
2106 for (var i = 0; i < length; ++i) {
2107 if (hasClass(nodes[i], className)) {
2110 onEveryMatchingChild(nodes[i], className, func);
2116 function toggleAllDocs(pageId, fromAutoCollapse) {
2117 var innerToggle = document.getElementById("toggle-all-docs");
2121 if (hasClass(innerToggle, "will-expand")) {
2122 updateLocalStorage("rustdoc-collapse", "false");
2123 removeClass(innerToggle, "will-expand");
2124 onEveryMatchingChild(innerToggle, "inner", function(e) {
2125 e.innerHTML = labelForToggleButton(false);
2127 innerToggle.title = "collapse all docs";
2128 if (fromAutoCollapse !== true) {
2129 onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
2130 collapseDocs(e, "show");
2134 updateLocalStorage("rustdoc-collapse", "true");
2135 addClass(innerToggle, "will-expand");
2136 onEveryMatchingChild(innerToggle, "inner", function(e) {
2137 var parent = e.parentNode;
2138 var superParent = null;
2141 superParent = parent.parentNode;
2143 if (!parent || !superParent || superParent.id !== "main" ||
2144 hasClass(parent, "impl") === false) {
2145 e.innerHTML = labelForToggleButton(true);
2148 innerToggle.title = "expand all docs";
2149 if (fromAutoCollapse !== true) {
2150 onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
2151 var parent = e.parentNode;
2152 var superParent = null;
2155 superParent = parent.parentNode;
2157 if (!parent || !superParent || superParent.id !== "main" ||
2158 hasClass(parent, "impl") === false) {
2159 collapseDocs(e, "hide", pageId);
2166 function collapseDocs(toggle, mode, pageId) {
2167 if (!toggle || !toggle.parentNode) {
2171 function adjustToggle(arg) {
2172 return function(e) {
2173 if (hasClass(e, "toggle-label")) {
2175 e.style.display = "inline-block";
2177 e.style.display = "none";
2180 if (hasClass(e, "inner")) {
2181 e.innerHTML = labelForToggleButton(arg);
2186 function implHider(addOrRemove, fullHide) {
2187 return function(n) {
2188 var is_method = hasClass(n, "method") || fullHide;
2189 if (is_method || hasClass(n, "type")) {
2190 if (is_method === true) {
2192 addClass(n, "hidden-by-impl-hider");
2194 removeClass(n, "hidden-by-impl-hider");
2197 var ns = n.nextElementSibling;
2198 while (ns && (hasClass(ns, "docblock") || hasClass(ns, "stability"))) {
2200 addClass(ns, "hidden-by-impl-hider");
2202 removeClass(ns, "hidden-by-impl-hider");
2204 ns = ns.nextElementSibling;
2212 if (hasClass(toggle.parentNode, "impl") === false) {
2213 relatedDoc = toggle.parentNode.nextElementSibling;
2214 if (hasClass(relatedDoc, "stability")) {
2215 relatedDoc = relatedDoc.nextElementSibling;
2217 if (hasClass(relatedDoc, "docblock") || hasClass(relatedDoc, "sub-variant")) {
2218 if (mode === "toggle") {
2219 if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
2225 if (action === "hide") {
2226 addClass(relatedDoc, "hidden-by-usual-hider");
2227 onEachLazy(toggle.childNodes, adjustToggle(true));
2228 addClass(toggle.parentNode, "collapsed");
2229 } else if (action === "show") {
2230 removeClass(relatedDoc, "hidden-by-usual-hider");
2231 removeClass(toggle.parentNode, "collapsed");
2232 onEachLazy(toggle.childNodes, adjustToggle(false));
2236 // we are collapsing the impl block(s).
2238 var parentElem = toggle.parentNode;
2239 relatedDoc = parentElem;
2240 var docblock = relatedDoc.nextElementSibling;
2242 while (hasClass(relatedDoc, "impl-items") === false) {
2243 relatedDoc = relatedDoc.nextElementSibling;
2246 if (!relatedDoc && hasClass(docblock, "docblock") === false) {
2250 // Hide all functions, but not associated types/consts.
2252 if (mode === "toggle") {
2253 if (hasClass(relatedDoc, "fns-now-collapsed") ||
2254 hasClass(docblock, "hidden-by-impl-hider")) {
2261 var dontApplyBlockRule = toggle.parentNode.parentNode.id !== "main";
2262 if (action === "show") {
2263 removeClass(relatedDoc, "fns-now-collapsed");
2264 removeClass(docblock, "hidden-by-usual-hider");
2265 onEachLazy(toggle.childNodes, adjustToggle(false, dontApplyBlockRule));
2266 onEachLazy(relatedDoc.childNodes, implHider(false, dontApplyBlockRule));
2267 } else if (action === "hide") {
2268 addClass(relatedDoc, "fns-now-collapsed");
2269 addClass(docblock, "hidden-by-usual-hider");
2270 onEachLazy(toggle.childNodes, adjustToggle(true, dontApplyBlockRule));
2271 onEachLazy(relatedDoc.childNodes, implHider(true, dontApplyBlockRule));
2276 function collapser(pageId, e, collapse) {
2277 // inherent impl ids are like "impl" or impl-<number>'.
2278 // they will never be hidden by default.
2279 var n = e.parentElement;
2280 if (n.id.match(/^impl(?:-\d+)?$/) === null) {
2281 // Automatically minimize all non-inherent impls
2282 if (collapse || hasClass(n, "impl")) {
2283 collapseDocs(e, "hide", pageId);
2288 function autoCollapse(pageId, collapse) {
2290 toggleAllDocs(pageId, true);
2291 } else if (getCurrentValue("rustdoc-auto-hide-trait-implementations") !== "false") {
2292 var impl_list = document.getElementById("trait-implementations-list");
2294 if (impl_list !== null) {
2295 onEachLazy(impl_list.getElementsByClassName("collapse-toggle"), function(e) {
2296 collapser(pageId, e, collapse);
2300 var blanket_list = document.getElementById("blanket-implementations-list");
2302 if (blanket_list !== null) {
2303 onEachLazy(blanket_list.getElementsByClassName("collapse-toggle"), function(e) {
2304 collapser(pageId, e, collapse);
2310 var toggles = document.getElementById("toggle-all-docs");
2312 toggles.onclick = toggleAllDocs;
2315 function insertAfter(newNode, referenceNode) {
2316 referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
2319 function createSimpleToggle(sectionIsCollapsed) {
2320 var toggle = document.createElement("a");
2321 toggle.href = "javascript:void(0)";
2322 toggle.className = "collapse-toggle";
2323 toggle.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(sectionIsCollapsed) +
2328 function createToggle(toggle, otherMessage, fontSize, extraClass, show) {
2329 var span = document.createElement("span");
2330 span.className = "toggle-label";
2332 span.style.display = "none";
2334 if (!otherMessage) {
2335 span.innerHTML = " Expand description";
2337 span.innerHTML = otherMessage;
2341 span.style.fontSize = fontSize;
2344 var mainToggle = toggle.cloneNode(true);
2345 mainToggle.appendChild(span);
2347 var wrapper = document.createElement("div");
2348 wrapper.className = "toggle-wrapper";
2350 addClass(wrapper, "collapsed");
2351 var inner = mainToggle.getElementsByClassName("inner");
2352 if (inner && inner.length > 0) {
2353 inner[0].innerHTML = "+";
2357 addClass(wrapper, extraClass);
2359 wrapper.appendChild(mainToggle);
2364 var toggle = createSimpleToggle(false);
2365 var hideMethodDocs = getCurrentValue("rustdoc-auto-hide-method-docs") === "true";
2366 var hideImplementors = getCurrentValue("rustdoc-auto-collapse-implementors") !== "false";
2367 var pageId = getPageId();
2369 var func = function(e) {
2370 var next = e.nextElementSibling;
2374 if (hasClass(next, "docblock") === true ||
2375 (hasClass(next, "stability") === true &&
2376 hasClass(next.nextElementSibling, "docblock") === true)) {
2377 var newToggle = toggle.cloneNode(true);
2378 insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
2379 if (hideMethodDocs === true && hasClass(e, "method") === true) {
2380 collapseDocs(newToggle, "hide", pageId);
2385 var funcImpl = function(e) {
2386 var next = e.nextElementSibling;
2387 if (next && hasClass(next, "docblock")) {
2388 next = next.nextElementSibling;
2393 if (hasClass(e, "impl") &&
2394 (next.getElementsByClassName("method").length > 0 ||
2395 next.getElementsByClassName("associatedconstant").length > 0)) {
2396 var newToggle = toggle.cloneNode(true);
2397 insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
2398 // In case the option "auto-collapse implementors" is not set to false, we collapse
2399 // all implementors.
2400 if (hideImplementors === true && e.parentNode.id === "implementors-list") {
2401 collapseDocs(newToggle, "hide", pageId);
2406 onEachLazy(document.getElementsByClassName("method"), func);
2407 onEachLazy(document.getElementsByClassName("associatedconstant"), func);
2408 onEachLazy(document.getElementsByClassName("impl"), funcImpl);
2409 var impl_call = function() {};
2410 if (hideMethodDocs === true) {
2411 impl_call = function(e, newToggle) {
2412 if (e.id.match(/^impl(?:-\d+)?$/) === null) {
2413 // Automatically minimize all non-inherent impls
2414 if (hasClass(e, "impl") === true) {
2415 collapseDocs(newToggle, "hide", pageId);
2420 var newToggle = document.createElement("a");
2421 newToggle.href = "javascript:void(0)";
2422 newToggle.className = "collapse-toggle hidden-default collapsed";
2423 newToggle.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(true) +
2424 "</span>] Show hidden undocumented items";
2425 function toggleClicked() {
2426 if (hasClass(this, "collapsed")) {
2427 removeClass(this, "collapsed");
2428 onEachLazy(this.parentNode.getElementsByClassName("hidden"), function(x) {
2429 if (hasClass(x, "content") === false) {
2430 removeClass(x, "hidden");
2434 this.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(false) +
2435 "</span>] Hide undocumented items";
2437 addClass(this, "collapsed");
2438 onEachLazy(this.parentNode.getElementsByClassName("x"), function(x) {
2439 if (hasClass(x, "content") === false) {
2440 addClass(x, "hidden");
2441 removeClass(x, "x");
2444 this.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(true) +
2445 "</span>] Show hidden undocumented items";
2448 onEachLazy(document.getElementsByClassName("impl-items"), function(e) {
2449 onEachLazy(e.getElementsByClassName("associatedconstant"), func);
2450 var hiddenElems = e.getElementsByClassName("hidden");
2451 var needToggle = false;
2453 var hlength = hiddenElems.length;
2454 for (var i = 0; i < hlength; ++i) {
2455 if (hasClass(hiddenElems[i], "content") === false &&
2456 hasClass(hiddenElems[i], "docblock") === false) {
2461 if (needToggle === true) {
2462 var inner_toggle = newToggle.cloneNode(true);
2463 inner_toggle.onclick = toggleClicked;
2464 e.insertBefore(inner_toggle, e.firstChild);
2465 impl_call(e.previousSibling, inner_toggle);
2469 var currentType = document.getElementsByClassName("type-decl")[0];
2470 var className = null;
2472 currentType = currentType.getElementsByClassName("rust")[0];
2474 currentType.classList.forEach(function(item) {
2475 if (item !== "main") {
2482 var showItemDeclarations = getCurrentValue("rustdoc-auto-hide-" + className);
2483 if (showItemDeclarations === null) {
2484 if (className === "enum" || className === "macro") {
2485 showItemDeclarations = "false";
2486 } else if (className === "struct" || className === "union" || className === "trait") {
2487 showItemDeclarations = "true";
2489 // In case we found an unknown type, we just use the "parent" value.
2490 showItemDeclarations = getCurrentValue("rustdoc-auto-hide-declarations");
2493 showItemDeclarations = showItemDeclarations === "false";
2494 function buildToggleWrapper(e) {
2495 if (hasClass(e, "autohide")) {
2496 var wrap = e.previousElementSibling;
2497 if (wrap && hasClass(wrap, "toggle-wrapper")) {
2498 var inner_toggle = wrap.childNodes[0];
2499 var extra = e.childNodes[0].tagName === "H3";
2501 e.style.display = "none";
2502 addClass(wrap, "collapsed");
2503 onEachLazy(inner_toggle.getElementsByClassName("inner"), function(e) {
2504 e.innerHTML = labelForToggleButton(true);
2506 onEachLazy(inner_toggle.getElementsByClassName("toggle-label"), function(e) {
2507 e.style.display = "inline-block";
2508 if (extra === true) {
2509 e.innerHTML = " Show " + e.childNodes[0].innerHTML;
2514 if (e.parentNode.id === "main") {
2515 var otherMessage = "";
2519 if (hasClass(e, "type-decl")) {
2521 otherMessage = " Show declaration";
2522 if (showItemDeclarations === false) {
2523 extraClass = "collapsed";
2525 } else if (hasClass(e, "sub-variant")) {
2526 otherMessage = " Show fields";
2527 } else if (hasClass(e, "non-exhaustive")) {
2528 otherMessage = " This ";
2529 if (hasClass(e, "non-exhaustive-struct")) {
2530 otherMessage += "struct";
2531 } else if (hasClass(e, "non-exhaustive-enum")) {
2532 otherMessage += "enum";
2533 } else if (hasClass(e, "non-exhaustive-variant")) {
2534 otherMessage += "enum variant";
2535 } else if (hasClass(e, "non-exhaustive-type")) {
2536 otherMessage += "type";
2538 otherMessage += " is marked as non-exhaustive";
2539 } else if (hasClass(e.childNodes[0], "impl-items")) {
2540 extraClass = "marg-left";
2543 e.parentNode.insertBefore(
2549 hasClass(e, "type-decl") === false || showItemDeclarations === true),
2551 if (hasClass(e, "type-decl") === true && showItemDeclarations === true) {
2552 collapseDocs(e.previousSibling.childNodes[0], "toggle");
2554 if (hasClass(e, "non-exhaustive") === true) {
2555 collapseDocs(e.previousSibling.childNodes[0], "toggle");
2560 onEachLazy(document.getElementsByClassName("docblock"), buildToggleWrapper);
2561 onEachLazy(document.getElementsByClassName("sub-variant"), buildToggleWrapper);
2562 var pageId = getPageId();
2564 autoCollapse(pageId, getCurrentValue("rustdoc-collapse") === "true");
2566 if (pageId !== null) {
2567 expandSection(pageId);
2571 function createToggleWrapper(tog) {
2572 var span = document.createElement("span");
2573 span.className = "toggle-label";
2574 span.style.display = "none";
2575 span.innerHTML = " Expand attributes";
2576 tog.appendChild(span);
2578 var wrapper = document.createElement("div");
2579 wrapper.className = "toggle-wrapper toggle-attributes";
2580 wrapper.appendChild(tog);
2585 // To avoid checking on "rustdoc-item-attributes" value on every loop...
2586 var itemAttributesFunc = function() {};
2587 if (getCurrentValue("rustdoc-auto-hide-attributes") !== "false") {
2588 itemAttributesFunc = function(x) {
2589 collapseDocs(x.previousSibling.childNodes[0], "toggle");
2592 var attributesToggle = createToggleWrapper(createSimpleToggle(false));
2593 onEachLazy(main.getElementsByClassName("attributes"), function(i_e) {
2594 var attr_tog = attributesToggle.cloneNode(true);
2595 if (hasClass(i_e, "top-attr") === true) {
2596 addClass(attr_tog, "top-attr");
2598 i_e.parentNode.insertBefore(attr_tog, i_e);
2599 itemAttributesFunc(i_e);
2604 // To avoid checking on "rustdoc-line-numbers" value on every loop...
2605 var lineNumbersFunc = function() {};
2606 if (getCurrentValue("rustdoc-line-numbers") === "true") {
2607 lineNumbersFunc = function(x) {
2608 var count = x.textContent.split("\n").length;
2610 for (var i = 0; i < count; ++i) {
2613 var node = document.createElement("pre");
2614 addClass(node, "line-number");
2615 node.innerHTML = elems.join("\n");
2616 x.parentNode.insertBefore(node, x);
2619 onEachLazy(document.getElementsByClassName("rust-example-rendered"), function(e) {
2620 if (hasClass(e, "compile_fail")) {
2621 e.addEventListener("mouseover", function() {
2622 this.parentElement.previousElementSibling.childNodes[0].style.color = "#f00";
2624 e.addEventListener("mouseout", function() {
2625 this.parentElement.previousElementSibling.childNodes[0].style.color = "";
2627 } else if (hasClass(e, "ignore")) {
2628 e.addEventListener("mouseover", function() {
2629 this.parentElement.previousElementSibling.childNodes[0].style.color = "#ff9200";
2631 e.addEventListener("mouseout", function() {
2632 this.parentElement.previousElementSibling.childNodes[0].style.color = "";
2639 onEachLazy(document.getElementsByClassName("important-traits"), function(e) {
2640 e.onclick = function() {
2641 this.getElementsByClassName('important-traits-tooltiptext')[0]
2642 .classList.toggle("force-tooltip");
2646 // In the search display, allows to switch between tabs.
2647 function printTab(nb) {
2648 if (nb === 0 || nb === 1 || nb === 2) {
2652 onEachLazy(document.getElementById("titles").childNodes, function(elem) {
2653 if (nb_copy === 0) {
2654 addClass(elem, "selected");
2656 removeClass(elem, "selected");
2660 onEachLazy(document.getElementById("results").childNodes, function(elem) {
2662 elem.style.display = "";
2664 elem.style.display = "none";
2670 function putBackSearch(search_input) {
2671 var search = getSearchElement();
2672 if (search_input.value !== "" && hasClass(search, "hidden")) {
2673 showSearchResults(search);
2674 if (browserSupportsHistoryApi()) {
2675 history.replaceState(search_input.value,
2677 "?search=" + encodeURIComponent(search_input.value));
2683 search_input.onfocus = function() {
2684 putBackSearch(this);
2688 var params = getQueryStringParams();
2689 if (params && params.search) {
2690 var search = getSearchElement();
2691 search.innerHTML = "<h3 style=\"text-align: center;\">Loading search results...</h3>";
2692 showSearchResults(search);
2695 var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];
2697 sidebar_menu.onclick = function() {
2698 var sidebar = document.getElementsByClassName("sidebar")[0];
2699 if (hasClass(sidebar, "mobile") === true) {
2707 window.onresize = function() {
2712 onEachLazy(main.getElementsByClassName("loading-content"), function(e) {
2715 onEachLazy(main.childNodes, function(e) {
2716 // Unhide the actual content once loading is complete. Headers get
2717 // flex treatment for their horizontal layout, divs get block treatment
2718 // for vertical layout (column-oriented flex layout for divs caused
2719 // errors in mobile browsers).
2720 if (e.tagName === "H2" || e.tagName === "H3") {
2721 var nextTagName = e.nextElementSibling.tagName;
2722 if (nextTagName == "H2" || nextTagName == "H3") {
2723 e.nextElementSibling.style.display = "flex";
2725 e.nextElementSibling.style.display = "block";
2731 window.addSearchOptions = function(crates) {
2732 var elem = document.getElementById("crate-search");
2737 var crates_text = [];
2738 if (Object.keys(crates).length > 1) {
2739 for (var crate in crates) {
2740 if (hasOwnProperty(crates, crate)) {
2741 crates_text.push(crate);
2745 crates_text.sort(function(a, b) {
2746 var lower_a = a.toLowerCase();
2747 var lower_b = b.toLowerCase();
2749 if (lower_a < lower_b) {
2751 } else if (lower_a > lower_b) {
2756 var savedCrate = getCurrentValue("rustdoc-saved-filter-crate");
2757 for (var i = 0; i < crates_text.length; ++i) {
2758 var option = document.createElement("option");
2759 option.value = crates_text[i];
2760 option.innerText = crates_text[i];
2761 elem.appendChild(option);
2762 // Set the crate filter from saved storage, if the current page has the saved crate
2765 // If not, ignore the crate filter -- we want to support filtering for crates on sites
2766 // like doc.rust-lang.org where the crates may differ from page to page while on the
2768 if (crates_text[i] === savedCrate) {
2769 elem.value = savedCrate;
2774 search_input.removeAttribute('disabled');
2778 function buildHelperPopup() {
2779 var popup = document.createElement("aside");
2780 addClass(popup, "hidden");
2783 var container = document.createElement("div");
2785 ["?", "Show this help dialog"],
2786 ["S", "Focus the search field"],
2787 ["↑", "Move up in search results"],
2788 ["↓", "Move down in search results"],
2789 ["↹", "Switch tab"],
2790 ["⏎", "Go to active search result"],
2791 ["+", "Expand all sections"],
2792 ["-", "Collapse all sections"],
2793 ].map(x => "<dt><kbd>" + x[0] + "</kbd></dt><dd>" + x[1] + "</dd>").join("");
2794 var div_shortcuts = document.createElement("div");
2795 addClass(div_shortcuts, "shortcuts");
2796 div_shortcuts.innerHTML = "<h2>Keyboard Shortcuts</h2><dl>" + shortcuts + "</dl></div>";
2799 "Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to \
2800 restrict the search to a given type.",
2801 "Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, \
2802 <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, \
2803 and <code>const</code>.",
2804 "Search functions by type signature (e.g., <code>vec -> usize</code> or \
2805 <code>* -> vec</code>)",
2806 "Search multiple things at once by splitting your query with comma (e.g., \
2807 <code>str,u8</code> or <code>String,struct:Vec,test</code>)",
2808 "You can look for items with an exact name by putting double quotes around \
2809 your request: <code>\"string\"</code>",
2810 "Look for items inside another one by searching for a path: <code>vec::Vec</code>",
2811 ].map(x => "<p>" + x + "</p>").join("");
2812 var div_infos = document.createElement("div");
2813 addClass(div_infos, "infos");
2814 div_infos.innerHTML = "<h2>Search Tricks</h2>" + infos;
2816 container.appendChild(div_shortcuts);
2817 container.appendChild(div_infos);
2819 popup.appendChild(container);
2820 insertAfter(popup, getSearchElement());
2824 window.onhashchange = onHashChange;
2829 // This is required in firefox. Explanations: when going back in the history, firefox doesn't re-run
2830 // the JS, therefore preventing rustdoc from setting a few things required to be able to reload the
2831 // previous search results (if you navigated to a search result with the keyboard, pressed enter on
2832 // it to navigate to that result, and then came back to this page).
2833 window.onunload = function(){};