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;
95 var toggleAllDocsId = "toggle-all-docs";
97 // On the search screen, so you remain on the last tab you opened.
100 // 1 for "In Parameters"
101 // 2 for "In Return Types"
104 var mouseMovedAfterSearch = true;
106 var titleBeforeSearch = document.title;
108 function clearInputTimeout() {
109 if (searchTimeout !== null) {
110 clearTimeout(searchTimeout);
111 searchTimeout = null;
115 function getPageId() {
116 if (window.location.hash) {
117 var tmp = window.location.hash.replace(/^#/, "");
118 if (tmp.length > 0) {
125 function showSidebar() {
126 var elems = document.getElementsByClassName("sidebar-elems")[0];
128 addClass(elems, "show-it");
130 var sidebar = document.getElementsByClassName("sidebar")[0];
132 addClass(sidebar, "mobile");
133 var filler = document.getElementById("sidebar-filler");
135 var div = document.createElement("div");
136 div.id = "sidebar-filler";
137 sidebar.appendChild(div);
140 var themePickers = document.getElementsByClassName("theme-picker");
141 if (themePickers && themePickers.length > 0) {
142 themePickers[0].style.display = "none";
146 function hideSidebar() {
147 var elems = document.getElementsByClassName("sidebar-elems")[0];
149 removeClass(elems, "show-it");
151 var sidebar = document.getElementsByClassName("sidebar")[0];
152 removeClass(sidebar, "mobile");
153 var filler = document.getElementById("sidebar-filler");
157 document.getElementsByTagName("body")[0].style.marginTop = "";
158 var themePickers = document.getElementsByClassName("theme-picker");
159 if (themePickers && themePickers.length > 0) {
160 themePickers[0].style.display = null;
164 function showSearchResults(search) {
165 if (search === null || typeof search === 'undefined') {
166 search = getSearchElement();
168 addClass(main, "hidden");
169 removeClass(search, "hidden");
170 mouseMovedAfterSearch = false;
173 function hideSearchResults(search) {
174 if (search === null || typeof search === 'undefined') {
175 search = getSearchElement();
177 addClass(search, "hidden");
178 removeClass(main, "hidden");
181 // used for special search precedence
182 var TY_PRIMITIVE = itemTypes.indexOf("primitive");
183 var TY_KEYWORD = itemTypes.indexOf("keyword");
185 function getQueryStringParams() {
187 window.location.search.substring(1).split("&").
189 var pair = s.split("=");
190 params[decodeURIComponent(pair[0])] =
191 typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
196 function browserSupportsHistoryApi() {
197 return window.history && typeof window.history.pushState === "function";
200 function isHidden(elem) {
201 return elem.offsetHeight === 0;
204 var main = document.getElementById("main");
207 function handleHashes(ev) {
209 var search = getSearchElement();
210 if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
211 // This block occurs when clicking on an element in the navbar while
213 hideSearchResults(search);
214 var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
215 if (browserSupportsHistoryApi()) {
216 history.replaceState(hash, "", "?search=#" + hash);
218 elem = document.getElementById(hash);
220 elem.scrollIntoView();
223 // This part is used in case an element is not visible.
224 if (savedHash !== window.location.hash) {
225 savedHash = window.location.hash;
226 if (savedHash.length === 0) {
229 elem = document.getElementById(savedHash.slice(1)); // we remove the '#'
230 if (!elem || !isHidden(elem)) {
233 var parent = elem.parentNode;
234 if (parent && hasClass(parent, "impl-items")) {
235 // In case this is a trait implementation item, we first need to toggle
236 // the "Show hidden undocumented items".
237 onEachLazy(parent.getElementsByClassName("collapsed"), function(e) {
238 if (e.parentNode === parent) {
239 // Only click on the toggle we're looking for.
244 if (isHidden(elem)) {
245 // The whole parent is collapsed. We need to click on its toggle as well!
246 if (hasClass(parent.lastElementChild, "collapse-toggle")) {
247 parent.lastElementChild.click();
254 function highlightSourceLines(match, ev) {
255 if (typeof match === "undefined") {
256 // If we're in mobile mode, we should hide the sidebar in any case.
258 match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
263 var from = parseInt(match[1], 10);
265 if (typeof match[2] !== "undefined") {
266 to = parseInt(match[2], 10);
273 var elem = document.getElementById(from);
278 var x = document.getElementById(from);
283 onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
284 onEachLazy(e.getElementsByTagName("span"), function(i_e) {
285 removeClass(i_e, "line-highlighted");
288 for (var i = from; i <= to; ++i) {
289 elem = document.getElementById(i);
293 addClass(elem, "line-highlighted");
297 function onHashChange(ev) {
298 // If we're in mobile mode, we should hide the sidebar in any case.
300 var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
302 return highlightSourceLines(match, ev);
307 function expandSection(id) {
308 var elem = document.getElementById(id);
309 if (elem && isHidden(elem)) {
310 var h3 = elem.parentNode.previousElementSibling;
311 if (h3 && h3.tagName !== "H3") {
312 h3 = h3.previousElementSibling; // skip div.docblock
316 var collapses = h3.getElementsByClassName("collapse-toggle");
317 if (collapses.length > 0) {
318 // The element is not visible, we need to make it appear!
319 collapseDocs(collapses[0], "show");
325 // Gets the human-readable string for the virtual-key code of the
326 // given KeyboardEvent, ev.
328 // This function is meant as a polyfill for KeyboardEvent#key,
329 // since it is not supported in Trident. We also test for
330 // KeyboardEvent#keyCode because the handleShortcut handler is
331 // also registered for the keydown event, because Blink doesn't fire
332 // keypress on hitting the Escape key.
334 // So I guess you could say things are getting pretty interoperable.
335 function getVirtualKey(ev) {
336 if ("key" in ev && typeof ev.key != "undefined") {
340 var c = ev.charCode || ev.keyCode;
344 return String.fromCharCode(c);
347 function getHelpElement() {
349 return document.getElementById("help");
352 function displayHelp(display, ev, help) {
353 help = help ? help : getHelpElement();
354 if (display === true) {
355 if (hasClass(help, "hidden")) {
357 removeClass(help, "hidden");
358 addClass(document.body, "blur");
360 } else if (hasClass(help, "hidden") === false) {
362 addClass(help, "hidden");
363 removeClass(document.body, "blur");
367 function handleEscape(ev) {
368 var help = getHelpElement();
369 var search = getSearchElement();
370 if (hasClass(help, "hidden") === false) {
371 displayHelp(false, ev, help);
372 } else if (hasClass(search, "hidden") === false) {
375 hideSearchResults(search);
376 document.title = titleBeforeSearch;
381 function handleShortcut(ev) {
382 // Don't interfere with browser shortcuts
383 if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts === true) {
387 if (document.activeElement.tagName === "INPUT") {
388 switch (getVirtualKey(ev)) {
394 switch (getVirtualKey(ev)) {
401 displayHelp(false, ev);
413 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, "help-button")) {
477 displayHelp(true, ev);
478 } else if (hasClass(ev.target, "collapse-toggle")) {
479 collapseDocs(ev.target, "toggle");
480 } else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
481 collapseDocs(ev.target.parentNode, "toggle");
482 } else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
483 handleSourceHighlight(ev);
484 } else if (hasClass(getHelpElement(), "hidden") === false) {
485 var help = getHelpElement();
486 var is_inside_help_popup = ev.target !== help && help.contains(ev.target);
487 if (is_inside_help_popup === false) {
488 addClass(help, "hidden");
489 removeClass(document.body, "blur");
492 // Making a collapsed element visible on onhashchange seems
494 var a = findParentElement(ev.target, "A");
496 expandSection(a.hash.replace(/^#/, ""));
502 var x = document.getElementsByClassName("version-selector");
504 x[0].onchange = function() {
506 url = document.location.href,
508 len = rootPath.match(/\.\.\//g).length + 1;
510 for (i = 0; i < len; ++i) {
511 match = url.match(/\/[^\/]*$/);
513 stripped = match[0] + stripped;
515 url = url.substring(0, url.length - match[0].length);
518 var selectedVersion = document.getElementsByClassName("version-selector")[0].value;
519 url += "/" + selectedVersion + stripped;
521 document.location.href = url;
527 * A function to compute the Levenshtein distance between two strings
528 * Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
529 * Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode
530 * This code is an unmodified version of the code written by Marco de Wit
531 * and was found at http://stackoverflow.com/a/18514751/745719
533 var levenshtein_row2 = [];
534 function levenshtein(s1, s2) {
538 var s1_len = s1.length, s2_len = s2.length;
539 if (s1_len && s2_len) {
540 var i1 = 0, i2 = 0, a, b, c, c2, row = levenshtein_row2;
541 while (i1 < s1_len) {
544 while (i2 < s2_len) {
545 c2 = s2.charCodeAt(i2);
549 for (i1 = 0; i1 < s1_len; ++i1) {
550 c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
552 b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
558 return s1_len + s2_len;
561 window.initSearch = function(rawSearchIndex) {
562 var MAX_LEV_DISTANCE = 3;
563 var MAX_RESULTS = 200;
564 var GENERICS_DATA = 1;
568 var NO_TYPE_FILTER = -1;
569 var currentResults, index, searchIndex;
571 var params = getQueryStringParams();
573 // Populate search bar with query string search term when provided,
574 // but only if the input bar is empty. This avoid the obnoxious issue
575 // where you start trying to do a search, and the index loads, and
576 // suddenly your search is gone!
577 if (search_input.value === "") {
578 search_input.value = params.search || "";
582 * Executes the query and builds an index of results
583 * @param {[Object]} query [The user query]
584 * @param {[type]} searchWords [The list of search words to query
586 * @param {[type]} filterCrates [Crate to search in if defined]
587 * @return {[type]} [A search index of results]
589 function execQuery(query, searchWords, filterCrates) {
590 function itemTypeFromName(typename) {
591 var length = itemTypes.length;
592 for (var i = 0; i < length; ++i) {
593 if (itemTypes[i] === typename) {
597 return NO_TYPE_FILTER;
600 var valLower = query.query.toLowerCase(),
602 typeFilter = itemTypeFromName(query.type),
603 results = {}, results_in_args = {}, results_returned = {},
604 split = valLower.split("::");
606 var length = split.length;
607 for (var z = 0; z < length; ++z) {
608 if (split[z] === "") {
614 function transformResults(results, isType) {
616 var length = results.length;
617 for (var i = 0; i < length; ++i) {
618 if (results[i].id > -1) {
619 var obj = searchIndex[results[i].id];
620 obj.lev = results[i].lev;
621 if (isType !== true || obj.type) {
622 var res = buildHrefAndPath(obj);
623 obj.displayPath = pathSplitter(res[0]);
624 obj.fullPath = obj.displayPath + obj.name;
625 // To be sure than it some items aren't considered as duplicate.
626 obj.fullPath += "|" + obj.ty;
629 if (out.length >= MAX_RESULTS) {
638 function sortResults(results, isType) {
640 for (var entry in results) {
641 if (hasOwnProperty(results, entry)) {
642 ar.push(results[entry]);
647 var nresults = results.length;
648 for (i = 0; i < nresults; ++i) {
649 results[i].word = searchWords[results[i].id];
650 results[i].item = searchIndex[results[i].id] || {};
652 // if there are no results then return to default and fail
653 if (results.length === 0) {
657 results.sort(function(aaa, bbb) {
660 // sort by exact match with regard to the last word (mismatch goes later)
661 a = (aaa.word !== val);
662 b = (bbb.word !== val);
663 if (a !== b) { return a - b; }
665 // Sort by non levenshtein results and then levenshtein results by the distance
666 // (less changes required to match means higher rankings)
669 if (a !== b) { return a - b; }
671 // sort by crate (non-current crate goes later)
672 a = (aaa.item.crate !== window.currentCrate);
673 b = (bbb.item.crate !== window.currentCrate);
674 if (a !== b) { return a - b; }
676 // sort by item name length (longer goes later)
679 if (a !== b) { return a - b; }
681 // sort by item name (lexicographically larger goes later)
684 if (a !== b) { return (a > b ? +1 : -1); }
686 // sort by index of keyword in item name (no literal occurrence goes later)
689 if (a !== b) { return a - b; }
690 // (later literal occurrence, if any, goes later)
693 if (a !== b) { return a - b; }
695 // special precedence for primitive and keyword pages
696 if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
697 (aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
700 if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
701 (bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
705 // sort by description (no description goes later)
706 a = (aaa.item.desc === "");
707 b = (bbb.item.desc === "");
708 if (a !== b) { return a - b; }
710 // sort by type (later occurrence in `itemTypes` goes later)
713 if (a !== b) { return a - b; }
715 // sort by path (lexicographically larger goes later)
718 if (a !== b) { return (a > b ? +1 : -1); }
724 var length = results.length;
725 for (i = 0; i < length; ++i) {
726 var result = results[i];
728 // this validation does not make sense when searching by types
729 if (result.dontValidate) {
732 var name = result.item.name.toLowerCase(),
733 path = result.item.path.toLowerCase(),
734 parent = result.item.parent;
736 if (isType !== true &&
737 validateResult(name, path, split, parent) === false)
742 return transformResults(results);
745 function extractGenerics(val) {
746 val = val.toLowerCase();
747 if (val.indexOf("<") !== -1) {
748 var values = val.substring(val.indexOf("<") + 1, val.lastIndexOf(">"));
750 name: val.substring(0, val.indexOf("<")),
751 generics: values.split(/\s*,\s*/),
760 function getObjectFromId(id) {
761 if (typeof id === "number") {
762 return searchIndex[id];
767 function checkGenerics(obj, val) {
768 // The names match, but we need to be sure that all generics kinda
770 var lev_distance = MAX_LEV_DISTANCE + 1;
771 if (val.generics.length > 0) {
772 if (obj.length > GENERICS_DATA &&
773 obj[GENERICS_DATA].length >= val.generics.length) {
774 var elems = obj[GENERICS_DATA].slice(0);
777 // We need to find the type that matches the most to remove it in order
779 var vlength = val.generics.length;
780 for (var y = 0; y < vlength; ++y) {
781 var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
782 var elength = elems.length;
783 var firstGeneric = getObjectFromId(val.generics[y]).name;
784 for (var x = 0; x < elength; ++x) {
785 var tmp_lev = levenshtein(getObjectFromId(elems[x]).name,
787 if (tmp_lev < lev.lev) {
792 if (lev.pos !== -1) {
793 elems.splice(lev.pos, 1);
794 lev_distance = Math.min(lev.lev, lev_distance);
798 return MAX_LEV_DISTANCE + 1;
801 return Math.ceil(total / done);
804 return MAX_LEV_DISTANCE + 1;
807 // Check for type name and type generics (if any).
808 function checkType(obj, val, literalSearch) {
809 var lev_distance = MAX_LEV_DISTANCE + 1;
811 if (obj[NAME] === val.name) {
812 if (literalSearch === true) {
813 if (val.generics && val.generics.length !== 0) {
814 if (obj.length > GENERICS_DATA &&
815 obj[GENERICS_DATA].length >= val.generics.length) {
816 var elems = obj[GENERICS_DATA].slice(0);
819 for (var y = 0; allFound === true && y < val.generics.length; ++y) {
821 var firstGeneric = getObjectFromId(val.generics[y]).name;
822 for (x = 0; allFound === false && x < elems.length; ++x) {
823 allFound = getObjectFromId(elems[x]).name === firstGeneric;
825 if (allFound === true) {
826 elems.splice(x - 1, 1);
829 if (allFound === true) {
838 // If the type has generics but don't match, then it won't return at this point.
839 // Otherwise, `checkGenerics` will return 0 and it'll return.
840 if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length !== 0) {
841 var tmp_lev = checkGenerics(obj, val);
842 if (tmp_lev <= MAX_LEV_DISTANCE) {
849 // Names didn't match so let's check if one of the generic types could.
850 if (literalSearch === true) {
851 if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
852 var length = obj[GENERICS_DATA].length;
853 for (x = 0; x < length; ++x) {
854 if (obj[GENERICS_DATA][x] === val.name) {
861 lev_distance = Math.min(levenshtein(obj[NAME], val.name), lev_distance);
862 if (lev_distance <= MAX_LEV_DISTANCE) {
863 // The generics didn't match but the name kinda did so we give it
864 // a levenshtein distance value that isn't *this* good so it goes
865 // into the search results but not too high.
866 lev_distance = Math.ceil((checkGenerics(obj, val) + lev_distance) / 2);
867 } else if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
868 // We can check if the type we're looking for is inside the generics!
869 var olength = obj[GENERICS_DATA].length;
870 for (x = 0; x < olength; ++x) {
871 lev_distance = Math.min(levenshtein(obj[GENERICS_DATA][x], val.name),
875 // Now whatever happens, the returned distance is "less good" so we should mark it
876 // as such, and so we add 1 to the distance to make it "less good".
877 return lev_distance + 1;
880 function findArg(obj, val, literalSearch, typeFilter) {
881 var lev_distance = MAX_LEV_DISTANCE + 1;
883 if (obj && obj.type && obj.type[INPUTS_DATA] && obj.type[INPUTS_DATA].length > 0) {
884 var length = obj.type[INPUTS_DATA].length;
885 for (var i = 0; i < length; i++) {
886 var tmp = obj.type[INPUTS_DATA][i];
887 if (typePassesFilter(typeFilter, tmp[1]) === false) {
890 tmp = checkType(tmp, val, literalSearch);
891 if (literalSearch === true) {
897 lev_distance = Math.min(tmp, lev_distance);
898 if (lev_distance === 0) {
903 return literalSearch === true ? false : lev_distance;
906 function checkReturned(obj, val, literalSearch, typeFilter) {
907 var lev_distance = MAX_LEV_DISTANCE + 1;
909 if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
910 var ret = obj.type[OUTPUT_DATA];
911 if (typeof ret[0] === "string") {
914 for (var x = 0; x < ret.length; ++x) {
916 if (typePassesFilter(typeFilter, tmp[1]) === false) {
919 tmp = checkType(tmp, val, literalSearch);
920 if (literalSearch === true) {
926 lev_distance = Math.min(tmp, lev_distance);
927 if (lev_distance === 0) {
932 return literalSearch === true ? false : lev_distance;
935 function checkPath(contains, lastElem, ty) {
936 if (contains.length === 0) {
939 var ret_lev = MAX_LEV_DISTANCE + 1;
940 var path = ty.path.split("::");
942 if (ty.parent && ty.parent.name) {
943 path.push(ty.parent.name.toLowerCase());
946 var length = path.length;
947 var clength = contains.length;
948 if (clength > length) {
949 return MAX_LEV_DISTANCE + 1;
951 for (var i = 0; i < length; ++i) {
952 if (i + clength > length) {
957 for (var x = 0; x < clength; ++x) {
958 var lev = levenshtein(path[i + x], contains[x]);
959 if (lev > MAX_LEV_DISTANCE) {
965 if (aborted === false) {
966 ret_lev = Math.min(ret_lev, Math.round(lev_total / clength));
972 function typePassesFilter(filter, type) {
974 if (filter <= NO_TYPE_FILTER) return true;
977 if (filter === type) return true;
979 // Match related items
980 var name = itemTypes[type];
981 switch (itemTypes[filter]) {
983 return name === "associatedconstant";
985 return name === "method" || name === "tymethod";
987 return name === "primitive" || name === "associatedtype";
989 return name === "traitalias";
996 function generateId(ty) {
997 if (ty.parent && ty.parent.name) {
998 return itemTypes[ty.ty] + ty.path + ty.parent.name + ty.name;
1000 return itemTypes[ty.ty] + ty.path + ty.name;
1003 function createAliasFromItem(item) {
1010 parent: item.parent,
1016 function handleAliases(ret, query, filterCrates) {
1017 // We separate aliases and crate aliases because we want to have current crate
1018 // aliases to be before the others in the displayed results.
1020 var crateAliases = [];
1022 if (filterCrates !== undefined) {
1023 if (ALIASES[filterCrates] && ALIASES[filterCrates][query.search]) {
1024 for (i = 0; i < ALIASES[filterCrates][query.search].length; ++i) {
1026 createAliasFromItem(
1027 searchIndex[ALIASES[filterCrates][query.search][i]]));
1031 Object.keys(ALIASES).forEach(function(crate) {
1032 if (ALIASES[crate][query.search]) {
1033 var pushTo = crate === window.currentCrate ? crateAliases : aliases;
1034 for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
1036 createAliasFromItem(
1037 searchIndex[ALIASES[crate][query.search][i]]));
1043 var sortFunc = function(aaa, bbb) {
1044 if (aaa.path < bbb.path) {
1046 } else if (aaa.path === bbb.path) {
1051 crateAliases.sort(sortFunc);
1052 aliases.sort(sortFunc);
1054 var pushFunc = function(alias) {
1055 alias.alias = query.raw;
1056 var res = buildHrefAndPath(alias);
1057 alias.displayPath = pathSplitter(res[0]);
1058 alias.fullPath = alias.displayPath + alias.name;
1059 alias.href = res[1];
1061 ret.others.unshift(alias);
1062 if (ret.others.length > MAX_RESULTS) {
1066 onEach(aliases, pushFunc);
1067 onEach(crateAliases, pushFunc);
1070 // quoted values mean literal search
1071 var nSearchWords = searchWords.length;
1077 if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
1078 val.charAt(val.length - 1) === val.charAt(0))
1080 val = extractGenerics(val.substr(1, val.length - 2));
1081 for (i = 0; i < nSearchWords; ++i) {
1082 if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
1085 in_args = findArg(searchIndex[i], val, true, typeFilter);
1086 returned = checkReturned(searchIndex[i], val, true, typeFilter);
1087 ty = searchIndex[i];
1088 fullId = generateId(ty);
1090 if (searchWords[i] === val.name
1091 && typePassesFilter(typeFilter, searchIndex[i].ty)
1092 && results[fullId] === undefined) {
1099 if (in_args === true && results_in_args[fullId] === undefined) {
1100 results_in_args[fullId] = {
1106 if (returned === true && results_returned[fullId] === undefined) {
1107 results_returned[fullId] = {
1114 query.inputs = [val];
1117 // searching by type
1118 } else if (val.search("->") > -1) {
1119 var trimmer = function(s) { return s.trim(); };
1120 var parts = val.split("->").map(trimmer);
1121 var input = parts[0];
1122 // sort inputs so that order does not matter
1123 var inputs = input.split(",").map(trimmer).sort();
1124 for (i = 0; i < inputs.length; ++i) {
1125 inputs[i] = extractGenerics(inputs[i]);
1127 var output = extractGenerics(parts[1]);
1129 for (i = 0; i < nSearchWords; ++i) {
1130 if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
1133 var type = searchIndex[i].type;
1134 ty = searchIndex[i];
1138 fullId = generateId(ty);
1140 returned = checkReturned(ty, output, true, NO_TYPE_FILTER);
1141 if (output.name === "*" || returned === true) {
1143 var is_module = false;
1145 if (input === "*") {
1148 var allFound = true;
1149 for (var it = 0; allFound === true && it < inputs.length; it++) {
1150 allFound = checkType(type, inputs[it], true);
1154 if (in_args === true) {
1155 results_in_args[fullId] = {
1161 if (returned === true) {
1162 results_returned[fullId] = {
1168 if (is_module === true) {
1177 query.inputs = inputs.map(function(input) {
1180 query.output = output.name;
1182 query.inputs = [val];
1185 // gather matching search results up to a certain maximum
1186 val = val.replace(/\_/g, "");
1188 var valGenerics = extractGenerics(val);
1190 var paths = valLower.split("::");
1192 for (j = 0; j < paths.length; ++j) {
1193 if (paths[j] === "") {
1198 val = paths[paths.length - 1];
1199 var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
1202 for (j = 0; j < nSearchWords; ++j) {
1203 ty = searchIndex[j];
1204 if (!ty || (filterCrates !== undefined && ty.crate !== filterCrates)) {
1208 if (paths.length > 1) {
1209 lev = checkPath(contains, paths[paths.length - 1], ty);
1210 if (lev > MAX_LEV_DISTANCE) {
1212 } else if (lev > 0) {
1217 returned = MAX_LEV_DISTANCE + 1;
1218 in_args = MAX_LEV_DISTANCE + 1;
1220 // we want lev results to go lower than others
1221 lev = MAX_LEV_DISTANCE + 1;
1222 fullId = generateId(ty);
1224 if (searchWords[j].indexOf(split[i]) > -1 ||
1225 searchWords[j].indexOf(val) > -1 ||
1226 searchWords[j].replace(/_/g, "").indexOf(val) > -1)
1228 // filter type: ... queries
1229 if (typePassesFilter(typeFilter, ty.ty) && results[fullId] === undefined) {
1230 index = searchWords[j].replace(/_/g, "").indexOf(val);
1233 if ((lev = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
1234 if (typePassesFilter(typeFilter, ty.ty) === false) {
1235 lev = MAX_LEV_DISTANCE + 1;
1240 in_args = findArg(ty, valGenerics, false, typeFilter);
1241 returned = checkReturned(ty, valGenerics, false, typeFilter);
1244 if (lev > 0 && val.length > 3 && searchWords[j].indexOf(val) > -1) {
1245 if (val.length < 6) {
1251 if (in_args <= MAX_LEV_DISTANCE) {
1252 if (results_in_args[fullId] === undefined) {
1253 results_in_args[fullId] = {
1259 results_in_args[fullId].lev =
1260 Math.min(results_in_args[fullId].lev, in_args);
1262 if (returned <= MAX_LEV_DISTANCE) {
1263 if (results_returned[fullId] === undefined) {
1264 results_returned[fullId] = {
1270 results_returned[fullId].lev =
1271 Math.min(results_returned[fullId].lev, returned);
1273 if (index !== -1 || lev <= MAX_LEV_DISTANCE) {
1274 if (index !== -1 && paths.length < 2) {
1277 if (results[fullId] === undefined) {
1284 results[fullId].lev = Math.min(results[fullId].lev, lev);
1290 "in_args": sortResults(results_in_args, true),
1291 "returned": sortResults(results_returned, true),
1292 "others": sortResults(results),
1294 handleAliases(ret, query, filterCrates);
1299 * Validate performs the following boolean logic. For example:
1300 * "File::open" will give IF A PARENT EXISTS => ("file" && "open")
1301 * exists in (name || path || parent) OR => ("file" && "open") exists in
1304 * This could be written functionally, but I wanted to minimise
1305 * functions on stack.
1307 * @param {[string]} name [The name of the result]
1308 * @param {[string]} path [The path of the result]
1309 * @param {[string]} keys [The keys to be used (["file", "open"])]
1310 * @param {[object]} parent [The parent of the result]
1311 * @return {[boolean]} [Whether the result is valid or not]
1313 function validateResult(name, path, keys, parent) {
1314 for (var i = 0; i < keys.length; ++i) {
1315 // each check is for validation so we negate the conditions and invalidate
1317 // check for an exact name match
1318 name.indexOf(keys[i]) > -1 ||
1319 // then an exact path match
1320 path.indexOf(keys[i]) > -1 ||
1321 // next if there is a parent, check for exact parent match
1322 (parent !== undefined && parent.name !== undefined &&
1323 parent.name.toLowerCase().indexOf(keys[i]) > -1) ||
1324 // lastly check to see if the name was a levenshtein match
1325 levenshtein(name, keys[i]) <= MAX_LEV_DISTANCE)) {
1332 function getQuery(raw) {
1333 var matches, type, query;
1336 matches = query.match(/^(fn|mod|struct|enum|trait|type|const|macro)\s*:\s*/i);
1338 type = matches[1].replace(/^const$/, "constant");
1339 query = query.substring(matches[0].length);
1350 function initSearchNav() {
1353 var click_func = function(e) {
1355 // to retrieve the real "owner" of the event.
1356 while (el.tagName !== "TR") {
1359 var dst = e.target.getElementsByTagName("a");
1360 if (dst.length < 1) {
1364 if (window.location.pathname === dst.pathname) {
1365 hideSearchResults();
1366 document.location.href = dst.href;
1369 var mouseover_func = function(e) {
1370 if (mouseMovedAfterSearch) {
1372 // to retrieve the real "owner" of the event.
1373 while (el.tagName !== "TR") {
1376 clearTimeout(hoverTimeout);
1377 hoverTimeout = setTimeout(function() {
1378 onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1379 onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1380 removeClass(i_e, "highlighted");
1383 addClass(el, "highlighted");
1387 onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1388 onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1389 i_e.onclick = click_func;
1390 i_e.onmouseover = mouseover_func;
1394 search_input.onkeydown = function(e) {
1395 // "actives" references the currently highlighted item in each search tab.
1396 // Each array in "actives" represents a tab.
1397 var actives = [[], [], []];
1398 // "current" is used to know which tab we're looking into.
1400 onEachLazy(document.getElementById("results").childNodes, function(e) {
1401 onEachLazy(e.getElementsByClassName("highlighted"), function(h_e) {
1402 actives[current].push(h_e);
1407 if (e.which === 38) { // up
1408 if (!actives[currentTab].length ||
1409 !actives[currentTab][0].previousElementSibling) {
1413 addClass(actives[currentTab][0].previousElementSibling, "highlighted");
1414 removeClass(actives[currentTab][0], "highlighted");
1416 } else if (e.which === 40) { // down
1417 if (!actives[currentTab].length) {
1418 var results = document.getElementById("results").childNodes;
1419 if (results.length > 0) {
1420 var res = results[currentTab].getElementsByClassName("result");
1421 if (res.length > 0) {
1422 addClass(res[0], "highlighted");
1425 } else if (actives[currentTab][0].nextElementSibling) {
1426 addClass(actives[currentTab][0].nextElementSibling, "highlighted");
1427 removeClass(actives[currentTab][0], "highlighted");
1430 } else if (e.which === 13) { // return
1431 if (actives[currentTab].length) {
1432 document.location.href =
1433 actives[currentTab][0].getElementsByTagName("a")[0].href;
1435 } else if (e.which === 9) { // tab
1437 printTab(currentTab > 0 ? currentTab - 1 : 2);
1439 printTab(currentTab > 1 ? 0 : currentTab + 1);
1442 } else if (e.which === 16) { // shift
1443 // Does nothing, it's just to avoid losing "focus" on the highlighted element.
1444 } else if (actives[currentTab].length > 0) {
1445 removeClass(actives[currentTab][0], "highlighted");
1450 function buildHrefAndPath(item) {
1453 var type = itemTypes[item.ty];
1454 var name = item.name;
1455 var path = item.path;
1457 if (type === "mod") {
1458 displayPath = path + "::";
1459 href = rootPath + path.replace(/::/g, "/") + "/" +
1460 name + "/index.html";
1461 } else if (type === "primitive" || type === "keyword") {
1463 href = rootPath + path.replace(/::/g, "/") +
1464 "/" + type + "." + name + ".html";
1465 } else if (type === "externcrate") {
1467 href = rootPath + name + "/index.html";
1468 } else if (item.parent !== undefined) {
1469 var myparent = item.parent;
1470 var anchor = "#" + type + "." + name;
1471 var parentType = itemTypes[myparent.ty];
1472 var pageType = parentType;
1473 var pageName = myparent.name;
1475 if (parentType === "primitive") {
1476 displayPath = myparent.name + "::";
1477 } else if (type === "structfield" && parentType === "variant") {
1478 // Structfields belonging to variants are special: the
1479 // final path element is the enum name.
1480 var splitPath = item.path.split("::");
1481 var enumName = splitPath.pop();
1482 path = splitPath.join("::");
1483 displayPath = path + "::" + enumName + "::" + myparent.name + "::";
1484 anchor = "#variant." + myparent.name + ".field." + name;
1486 pageName = enumName;
1488 displayPath = path + "::" + myparent.name + "::";
1490 href = rootPath + path.replace(/::/g, "/") +
1495 displayPath = item.path + "::";
1496 href = rootPath + item.path.replace(/::/g, "/") +
1497 "/" + type + "." + name + ".html";
1499 return [displayPath, href];
1502 function escape(content) {
1503 var h1 = document.createElement("h1");
1504 h1.textContent = content;
1505 return h1.innerHTML;
1508 function pathSplitter(path) {
1509 var tmp = "<span>" + path.replace(/::/g, "::</span><span>");
1510 if (tmp.endsWith("<span>")) {
1511 return tmp.slice(0, tmp.length - 6);
1516 function addTab(array, query, display) {
1517 var extraStyle = "";
1518 if (display === false) {
1519 extraStyle = " style=\"display: none;\"";
1523 var duplicates = {};
1525 if (array.length > 0) {
1526 output = "<table class=\"search-results\"" + extraStyle + ">";
1528 array.forEach(function(item) {
1532 type = itemTypes[item.ty];
1534 if (item.is_alias !== true) {
1535 if (duplicates[item.fullPath]) {
1538 duplicates[item.fullPath] = true;
1542 output += "<tr class=\"" + type + " result\"><td>" +
1543 "<a href=\"" + item.href + "\">" +
1544 (item.is_alias === true ?
1545 ("<span class=\"alias\"><b>" + item.alias + " </b></span><span " +
1546 "class=\"grey\"><i> - see </i></span>") : "") +
1547 item.displayPath + "<span class=\"" + type + "\">" +
1548 name + "</span></a></td><td>" +
1549 "<a href=\"" + item.href + "\">" +
1550 "<span class=\"desc\">" + escape(item.desc) +
1551 " </span></a></td></tr>";
1553 output += "</table>";
1555 output = "<div class=\"search-failed\"" + extraStyle + ">No results :(<br/>" +
1556 "Try on <a href=\"https://duckduckgo.com/?q=" +
1557 encodeURIComponent("rust " + query.query) +
1558 "\">DuckDuckGo</a>?<br/><br/>" +
1559 "Or try looking in one of these:<ul><li>The <a " +
1560 "href=\"https://doc.rust-lang.org/reference/index.html\">Rust Reference</a> " +
1561 " for technical details about the language.</li><li><a " +
1562 "href=\"https://doc.rust-lang.org/rust-by-example/index.html\">Rust By " +
1563 "Example</a> for expository code examples.</a></li><li>The <a " +
1564 "href=\"https://doc.rust-lang.org/book/index.html\">Rust Book</a> for " +
1565 "introductions to language features and the language itself.</li><li><a " +
1566 "href=\"https://docs.rs\">Docs.rs</a> for documentation of crates released on" +
1567 " <a href=\"https://crates.io/\">crates.io</a>.</li></ul></div>";
1569 return [output, length];
1572 function makeTabHeader(tabNb, text, nbElems) {
1573 if (currentTab === tabNb) {
1574 return "<div class=\"selected\">" + text +
1575 " <div class=\"count\">(" + nbElems + ")</div></div>";
1577 return "<div>" + text + " <div class=\"count\">(" + nbElems + ")</div></div>";
1580 function showResults(results) {
1581 var search = getSearchElement();
1582 if (results.others.length === 1
1583 && getCurrentValue("rustdoc-go-to-only-result") === "true"
1584 // By default, the search DOM element is "empty" (meaning it has no children not
1585 // text content). Once a search has been run, it won't be empty, even if you press
1586 // ESC or empty the search input (which also "cancels" the search).
1587 && (!search.firstChild || search.firstChild.innerText !== getSearchLoadingText()))
1589 var elem = document.createElement("a");
1590 elem.href = results.others[0].href;
1591 elem.style.display = "none";
1592 // For firefox, we need the element to be in the DOM so it can be clicked.
1593 document.body.appendChild(elem);
1597 var query = getQuery(search_input.value);
1599 currentResults = query.id;
1601 var ret_others = addTab(results.others, query);
1602 var ret_in_args = addTab(results.in_args, query, false);
1603 var ret_returned = addTab(results.returned, query, false);
1605 var output = "<h1>Results for " + escape(query.query) +
1606 (query.type ? " (type: " + escape(query.type) + ")" : "") + "</h1>" +
1607 "<div id=\"titles\">" +
1608 makeTabHeader(0, "In Names", ret_others[1]) +
1609 makeTabHeader(1, "In Parameters", ret_in_args[1]) +
1610 makeTabHeader(2, "In Return Types", ret_returned[1]) +
1611 "</div><div id=\"results\">" +
1612 ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
1614 search.innerHTML = output;
1615 showSearchResults(search);
1616 var tds = search.getElementsByTagName("td");
1618 if (tds.length > 0) {
1619 td_width = tds[0].offsetWidth;
1621 var width = search.offsetWidth - 40 - td_width;
1622 onEachLazy(search.getElementsByClassName("desc"), function(e) {
1623 e.style.width = width + "px";
1626 var elems = document.getElementById("titles").childNodes;
1627 elems[0].onclick = function() { printTab(0); };
1628 elems[1].onclick = function() { printTab(1); };
1629 elems[2].onclick = function() { printTab(2); };
1630 printTab(currentTab);
1633 function execSearch(query, searchWords, filterCrates) {
1634 function getSmallest(arrays, positions, notDuplicates) {
1637 for (var it = 0; it < positions.length; ++it) {
1638 if (arrays[it].length > positions[it] &&
1639 (start === null || start > arrays[it][positions[it]].lev) &&
1640 !notDuplicates[arrays[it][positions[it]].fullPath]) {
1641 start = arrays[it][positions[it]].lev;
1647 function mergeArrays(arrays) {
1650 var notDuplicates = {};
1652 for (var x = 0; x < arrays.length; ++x) {
1655 while (ret.length < MAX_RESULTS) {
1656 var smallest = getSmallest(arrays, positions, notDuplicates);
1658 if (smallest === null) {
1661 for (x = 0; x < arrays.length && ret.length < MAX_RESULTS; ++x) {
1662 if (arrays[x].length > positions[x] &&
1663 arrays[x][positions[x]].lev === smallest &&
1664 !notDuplicates[arrays[x][positions[x]].fullPath]) {
1665 ret.push(arrays[x][positions[x]]);
1666 notDuplicates[arrays[x][positions[x]].fullPath] = true;
1674 var queries = query.raw.split(",");
1681 for (var i = 0; i < queries.length; ++i) {
1682 query = queries[i].trim();
1683 if (query.length !== 0) {
1684 var tmp = execQuery(getQuery(query), searchWords, filterCrates);
1686 results.in_args.push(tmp.in_args);
1687 results.returned.push(tmp.returned);
1688 results.others.push(tmp.others);
1691 if (queries.length > 1) {
1693 "in_args": mergeArrays(results.in_args),
1694 "returned": mergeArrays(results.returned),
1695 "others": mergeArrays(results.others),
1699 "in_args": results.in_args[0],
1700 "returned": results.returned[0],
1701 "others": results.others[0],
1705 function getFilterCrates() {
1706 var elem = document.getElementById("crate-search");
1708 if (elem && elem.value !== "All crates" && hasOwnProperty(rawSearchIndex, elem.value)) {
1714 function search(e, forced) {
1715 var params = getQueryStringParams();
1716 var query = getQuery(search_input.value.trim());
1722 if (query.query.length === 0) {
1725 if (forced !== true && query.id === currentResults) {
1726 if (query.query.length > 0) {
1727 putBackSearch(search_input);
1732 // Update document title to maintain a meaningful browser history
1733 document.title = "Results for " + query.query + " - Rust";
1735 // Because searching is incremental by character, only the most
1736 // recent search query is added to the browser history.
1737 if (browserSupportsHistoryApi()) {
1738 if (!history.state && !params.search) {
1739 history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
1741 history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
1745 var filterCrates = getFilterCrates();
1746 showResults(execSearch(query, index, filterCrates));
1749 function buildIndex(rawSearchIndex) {
1751 var searchWords = [];
1753 var currentIndex = 0;
1755 for (var crate in rawSearchIndex) {
1756 if (!hasOwnProperty(rawSearchIndex, crate)) { continue; }
1760 searchWords.push(crate);
1763 ty: 1, // == ExternCrate
1766 desc: rawSearchIndex[crate].doc,
1771 // an array of [(Number) item type,
1773 // (String) full path or empty string for previous path,
1774 // (String) description,
1775 // (Number | null) the parent path index to `paths`]
1776 // (Object | null) the type of the function (if any)
1777 var items = rawSearchIndex[crate].i;
1778 // an array of [(Number) item type,
1780 var paths = rawSearchIndex[crate].p;
1781 // a array of [(String) alias name
1782 // [Number] index to items]
1783 var aliases = rawSearchIndex[crate].a;
1785 // convert `rawPaths` entries into object form
1786 var len = paths.length;
1787 for (i = 0; i < len; ++i) {
1788 paths[i] = {ty: paths[i][0], name: paths[i][1]};
1791 // convert `items` into an object form, and construct word indices.
1793 // before any analysis is performed lets gather the search terms to
1794 // search against apart from the rest of the data. This is a quick
1795 // operation that is cached for the life of the page state so that
1796 // all other search operations have access to this cached data for
1797 // faster analysis operations
1800 for (i = 0; i < len; ++i) {
1801 var rawRow = items[i];
1803 rawRow[2] = lastPath;
1811 parent: paths[rawRow[4]],
1814 searchIndex.push(row);
1815 if (typeof row.name === "string") {
1816 var word = row.name.toLowerCase();
1817 searchWords.push(word);
1819 searchWords.push("");
1821 lastPath = row.path;
1826 ALIASES[crate] = {};
1827 var j, local_aliases;
1828 for (var alias_name in aliases) {
1829 if (!aliases.hasOwnProperty(alias_name)) { continue; }
1831 if (!ALIASES[crate].hasOwnProperty(alias_name)) {
1832 ALIASES[crate][alias_name] = [];
1834 local_aliases = aliases[alias_name];
1835 for (j = 0; j < local_aliases.length; ++j) {
1836 ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
1840 currentIndex += crateSize;
1845 function startSearch() {
1846 var callback = function() {
1847 clearInputTimeout();
1848 if (search_input.value.length === 0) {
1849 if (browserSupportsHistoryApi()) {
1850 history.replaceState("", window.currentCrate + " - Rust", "?search=");
1852 hideSearchResults();
1854 searchTimeout = setTimeout(search, 500);
1857 search_input.onkeyup = callback;
1858 search_input.oninput = callback;
1859 document.getElementsByClassName("search-form")[0].onsubmit = function(e) {
1861 clearInputTimeout();
1864 search_input.onchange = function(e) {
1865 if (e.target !== document.activeElement) {
1866 // To prevent doing anything when it's from a blur event.
1869 // Do NOT e.preventDefault() here. It will prevent pasting.
1870 clearInputTimeout();
1871 // zero-timeout necessary here because at the time of event handler execution the
1872 // pasted content is not in the input field yet. Shouldn’t make any difference for
1874 setTimeout(search, 0);
1876 search_input.onpaste = search_input.onchange;
1878 var selectCrate = document.getElementById("crate-search");
1880 selectCrate.onchange = function() {
1881 updateLocalStorage("rustdoc-saved-filter-crate", selectCrate.value);
1882 search(undefined, true);
1886 // Push and pop states are used to add search results to the browser
1888 if (browserSupportsHistoryApi()) {
1889 // Store the previous <title> so we can revert back to it later.
1890 var previousTitle = document.title;
1892 window.addEventListener("popstate", function(e) {
1893 var params = getQueryStringParams();
1894 // Revert to the previous title manually since the History
1895 // API ignores the title parameter.
1896 document.title = previousTitle;
1897 // When browsing forward to search results the previous
1898 // search will be repeated, so the currentResults are
1899 // cleared to ensure the search is successful.
1900 currentResults = null;
1901 // Synchronize search bar with query string state and
1902 // perform the search. This will empty the bar if there's
1903 // nothing there, which lets you really go back to a
1904 // previous state with nothing in the bar.
1905 if (params.search && params.search.length > 0) {
1906 search_input.value = params.search;
1907 // Some browsers fire "onpopstate" for every page load
1908 // (Chrome), while others fire the event only when actually
1909 // popping a state (Firefox), which is why search() is
1910 // called both here and at the end of the startSearch()
1914 search_input.value = "";
1915 // When browsing back from search results the main page
1916 // visibility must be reset.
1917 hideSearchResults();
1924 index = buildIndex(rawSearchIndex);
1927 // Draw a convenient sidebar of known crates if we have a listing
1928 if (rootPath === "../" || rootPath === "./") {
1929 var sidebar = document.getElementsByClassName("sidebar-elems")[0];
1931 var div = document.createElement("div");
1932 div.className = "block crate";
1933 div.innerHTML = "<h3>Crates</h3>";
1934 var ul = document.createElement("ul");
1935 div.appendChild(ul);
1938 for (var crate in rawSearchIndex) {
1939 if (!hasOwnProperty(rawSearchIndex, crate)) {
1945 for (var i = 0; i < crates.length; ++i) {
1946 var klass = "crate";
1947 if (rootPath !== "./" && crates[i] === window.currentCrate) {
1948 klass += " current";
1950 var link = document.createElement("a");
1951 link.href = rootPath + crates[i] + "/index.html";
1952 link.title = rawSearchIndex[crates[i]].doc;
1953 link.className = klass;
1954 link.textContent = crates[i];
1956 var li = document.createElement("li");
1957 li.appendChild(link);
1960 sidebar.appendChild(div);
1966 // delayed sidebar rendering.
1967 window.initSidebarItems = function(items) {
1968 var sidebar = document.getElementsByClassName("sidebar-elems")[0];
1969 var current = window.sidebarCurrent;
1971 function block(shortty, longty) {
1972 var filtered = items[shortty];
1977 var div = document.createElement("div");
1978 div.className = "block " + shortty;
1979 var h3 = document.createElement("h3");
1980 h3.textContent = longty;
1981 div.appendChild(h3);
1982 var ul = document.createElement("ul");
1984 var length = filtered.length;
1985 for (var i = 0; i < length; ++i) {
1986 var item = filtered[i];
1988 var desc = item[1]; // can be null
1990 var klass = shortty;
1991 if (name === current.name && shortty === current.ty) {
1992 klass += " current";
1995 if (shortty === "mod") {
1996 path = name + "/index.html";
1998 path = shortty + "." + name + ".html";
2000 var link = document.createElement("a");
2001 link.href = current.relpath + path;
2003 link.className = klass;
2004 link.textContent = name;
2005 var li = document.createElement("li");
2006 li.appendChild(link);
2009 div.appendChild(ul);
2011 sidebar.appendChild(div);
2015 block("primitive", "Primitive Types");
2016 block("mod", "Modules");
2017 block("macro", "Macros");
2018 block("struct", "Structs");
2019 block("enum", "Enums");
2020 block("union", "Unions");
2021 block("constant", "Constants");
2022 block("static", "Statics");
2023 block("trait", "Traits");
2024 block("fn", "Functions");
2025 block("type", "Type Definitions");
2026 block("foreigntype", "Foreign Types");
2027 block("keyword", "Keywords");
2028 block("traitalias", "Trait Aliases");
2031 window.register_implementors = function(imp) {
2032 var implementors = document.getElementById("implementors-list");
2033 var synthetic_implementors = document.getElementById("synthetic-implementors-list");
2035 if (synthetic_implementors) {
2036 // This `inlined_types` variable is used to avoid having the same implementation
2037 // showing up twice. For example "String" in the "Sync" doc page.
2039 // By the way, this is only used by and useful for traits implemented automatically
2040 // (like "Send" and "Sync").
2041 var inlined_types = new Set();
2042 onEachLazy(synthetic_implementors.getElementsByClassName("impl"), function(el) {
2043 var aliases = el.getAttribute("aliases");
2047 aliases.split(",").forEach(function(alias) {
2048 inlined_types.add(alias);
2053 var libs = Object.getOwnPropertyNames(imp);
2054 var llength = libs.length;
2055 for (var i = 0; i < llength; ++i) {
2056 if (libs[i] === currentCrate) { continue; }
2057 var structs = imp[libs[i]];
2059 var slength = structs.length;
2061 for (var j = 0; j < slength; ++j) {
2062 var struct = structs[j];
2064 var list = struct.synthetic ? synthetic_implementors : implementors;
2066 if (struct.synthetic) {
2067 var stlength = struct.types.length;
2068 for (var k = 0; k < stlength; k++) {
2069 if (inlined_types.has(struct.types[k])) {
2070 continue struct_loop;
2072 inlined_types.add(struct.types[k]);
2076 var code = document.createElement("code");
2077 code.innerHTML = struct.text;
2079 var x = code.getElementsByTagName("a");
2080 var xlength = x.length;
2081 for (var it = 0; it < xlength; it++) {
2082 var href = x[it].getAttribute("href");
2083 if (href && href.indexOf("http") !== 0) {
2084 x[it].setAttribute("href", rootPath + href);
2087 var display = document.createElement("h3");
2088 addClass(display, "impl");
2089 display.innerHTML = "<span class=\"in-band\"><table class=\"table-display\">" +
2090 "<tbody><tr><td><code>" + code.outerHTML + "</code></td><td></td></tr>" +
2091 "</tbody></table></span>";
2092 list.appendChild(display);
2096 if (window.pending_implementors) {
2097 window.register_implementors(window.pending_implementors);
2100 function labelForToggleButton(sectionIsCollapsed) {
2101 if (sectionIsCollapsed) {
2102 // button will expand the section
2105 // button will collapse the section
2106 // note that this text is also set in the HTML template in render.rs
2107 return "\u2212"; // "\u2212" is "−" minus sign
2110 function onEveryMatchingChild(elem, className, func) {
2111 if (elem && className && func) {
2112 var length = elem.childNodes.length;
2113 var nodes = elem.childNodes;
2114 for (var i = 0; i < length; ++i) {
2115 if (hasClass(nodes[i], className)) {
2118 onEveryMatchingChild(nodes[i], className, func);
2124 function toggleAllDocs(pageId, fromAutoCollapse) {
2125 var innerToggle = document.getElementById(toggleAllDocsId);
2129 if (hasClass(innerToggle, "will-expand")) {
2130 updateLocalStorage("rustdoc-collapse", "false");
2131 removeClass(innerToggle, "will-expand");
2132 onEveryMatchingChild(innerToggle, "inner", function(e) {
2133 e.innerHTML = labelForToggleButton(false);
2135 innerToggle.title = "collapse all docs";
2136 if (fromAutoCollapse !== true) {
2137 onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
2138 collapseDocs(e, "show");
2142 updateLocalStorage("rustdoc-collapse", "true");
2143 addClass(innerToggle, "will-expand");
2144 onEveryMatchingChild(innerToggle, "inner", function(e) {
2145 var parent = e.parentNode;
2146 var superParent = null;
2149 superParent = parent.parentNode;
2151 if (!parent || !superParent || superParent.id !== "main" ||
2152 hasClass(parent, "impl") === false) {
2153 e.innerHTML = labelForToggleButton(true);
2156 innerToggle.title = "expand all docs";
2157 if (fromAutoCollapse !== true) {
2158 onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
2159 var parent = e.parentNode;
2160 var superParent = null;
2163 superParent = parent.parentNode;
2165 if (!parent || !superParent || superParent.id !== "main" ||
2166 hasClass(parent, "impl") === false) {
2167 collapseDocs(e, "hide", pageId);
2174 function collapseDocs(toggle, mode, pageId) {
2175 if (!toggle || !toggle.parentNode) {
2179 function adjustToggle(arg) {
2180 return function(e) {
2181 if (hasClass(e, "toggle-label")) {
2183 e.style.display = "inline-block";
2185 e.style.display = "none";
2188 if (hasClass(e, "inner")) {
2189 e.innerHTML = labelForToggleButton(arg);
2194 function implHider(addOrRemove, fullHide) {
2195 return function(n) {
2196 var is_method = hasClass(n, "method") || fullHide;
2197 if (is_method || hasClass(n, "type")) {
2198 if (is_method === true) {
2200 addClass(n, "hidden-by-impl-hider");
2202 removeClass(n, "hidden-by-impl-hider");
2205 var ns = n.nextElementSibling;
2206 while (ns && (hasClass(ns, "docblock") || hasClass(ns, "stability"))) {
2208 addClass(ns, "hidden-by-impl-hider");
2210 removeClass(ns, "hidden-by-impl-hider");
2212 ns = ns.nextElementSibling;
2220 if (hasClass(toggle.parentNode, "impl") === false) {
2221 relatedDoc = toggle.parentNode.nextElementSibling;
2222 if (hasClass(relatedDoc, "stability")) {
2223 relatedDoc = relatedDoc.nextElementSibling;
2225 if (hasClass(relatedDoc, "docblock") || hasClass(relatedDoc, "sub-variant")) {
2226 if (mode === "toggle") {
2227 if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
2233 if (action === "hide") {
2234 addClass(relatedDoc, "hidden-by-usual-hider");
2235 onEachLazy(toggle.childNodes, adjustToggle(true));
2236 addClass(toggle.parentNode, "collapsed");
2237 } else if (action === "show") {
2238 removeClass(relatedDoc, "hidden-by-usual-hider");
2239 removeClass(toggle.parentNode, "collapsed");
2240 onEachLazy(toggle.childNodes, adjustToggle(false));
2244 // we are collapsing the impl block(s).
2246 var parentElem = toggle.parentNode;
2247 relatedDoc = parentElem;
2248 var docblock = relatedDoc.nextElementSibling;
2250 while (hasClass(relatedDoc, "impl-items") === false) {
2251 relatedDoc = relatedDoc.nextElementSibling;
2254 if (!relatedDoc && hasClass(docblock, "docblock") === false) {
2258 // Hide all functions, but not associated types/consts.
2260 if (mode === "toggle") {
2261 if (hasClass(relatedDoc, "fns-now-collapsed") ||
2262 hasClass(docblock, "hidden-by-impl-hider")) {
2269 var dontApplyBlockRule = toggle.parentNode.parentNode.id !== "main";
2270 if (action === "show") {
2271 removeClass(relatedDoc, "fns-now-collapsed");
2272 removeClass(docblock, "hidden-by-usual-hider");
2273 onEachLazy(toggle.childNodes, adjustToggle(false, dontApplyBlockRule));
2274 onEachLazy(relatedDoc.childNodes, implHider(false, dontApplyBlockRule));
2275 } else if (action === "hide") {
2276 addClass(relatedDoc, "fns-now-collapsed");
2277 addClass(docblock, "hidden-by-usual-hider");
2278 onEachLazy(toggle.childNodes, adjustToggle(true, dontApplyBlockRule));
2279 onEachLazy(relatedDoc.childNodes, implHider(true, dontApplyBlockRule));
2284 function collapser(pageId, e, collapse) {
2285 // inherent impl ids are like "impl" or impl-<number>'.
2286 // they will never be hidden by default.
2287 var n = e.parentElement;
2288 if (n.id.match(/^impl(?:-\d+)?$/) === null) {
2289 // Automatically minimize all non-inherent impls
2290 if (collapse || hasClass(n, "impl")) {
2291 collapseDocs(e, "hide", pageId);
2296 function autoCollapse(pageId, collapse) {
2298 toggleAllDocs(pageId, true);
2299 } else if (getCurrentValue("rustdoc-auto-hide-trait-implementations") !== "false") {
2300 var impl_list = document.getElementById("trait-implementations-list");
2302 if (impl_list !== null) {
2303 onEachLazy(impl_list.getElementsByClassName("collapse-toggle"), function(e) {
2304 collapser(pageId, e, collapse);
2308 var blanket_list = document.getElementById("blanket-implementations-list");
2310 if (blanket_list !== null) {
2311 onEachLazy(blanket_list.getElementsByClassName("collapse-toggle"), function(e) {
2312 collapser(pageId, e, collapse);
2318 function insertAfter(newNode, referenceNode) {
2319 referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
2322 function createSimpleToggle(sectionIsCollapsed) {
2323 var toggle = document.createElement("a");
2324 toggle.href = "javascript:void(0)";
2325 toggle.className = "collapse-toggle";
2326 toggle.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(sectionIsCollapsed) +
2331 function createToggle(toggle, otherMessage, fontSize, extraClass, show) {
2332 var span = document.createElement("span");
2333 span.className = "toggle-label";
2335 span.style.display = "none";
2337 if (!otherMessage) {
2338 span.innerHTML = " Expand description";
2340 span.innerHTML = otherMessage;
2344 span.style.fontSize = fontSize;
2347 var mainToggle = toggle.cloneNode(true);
2348 mainToggle.appendChild(span);
2350 var wrapper = document.createElement("div");
2351 wrapper.className = "toggle-wrapper";
2353 addClass(wrapper, "collapsed");
2354 var inner = mainToggle.getElementsByClassName("inner");
2355 if (inner && inner.length > 0) {
2356 inner[0].innerHTML = "+";
2360 addClass(wrapper, extraClass);
2362 wrapper.appendChild(mainToggle);
2367 var toggles = document.getElementById(toggleAllDocsId);
2369 toggles.onclick = toggleAllDocs;
2372 var toggle = createSimpleToggle(false);
2373 var hideMethodDocs = getCurrentValue("rustdoc-auto-hide-method-docs") === "true";
2374 var hideImplementors = getCurrentValue("rustdoc-auto-collapse-implementors") !== "false";
2375 var pageId = getPageId();
2377 var func = function(e) {
2378 var next = e.nextElementSibling;
2382 if (hasClass(next, "docblock") === true ||
2383 (hasClass(next, "stability") === true &&
2384 hasClass(next.nextElementSibling, "docblock") === true)) {
2385 var newToggle = toggle.cloneNode(true);
2386 insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
2387 if (hideMethodDocs === true && hasClass(e, "method") === true) {
2388 collapseDocs(newToggle, "hide", pageId);
2393 var funcImpl = function(e) {
2394 var next = e.nextElementSibling;
2395 if (next && hasClass(next, "docblock")) {
2396 next = next.nextElementSibling;
2401 if (hasClass(e, "impl") &&
2402 (next.getElementsByClassName("method").length > 0 ||
2403 next.getElementsByClassName("associatedconstant").length > 0)) {
2404 var newToggle = toggle.cloneNode(true);
2405 insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
2406 // In case the option "auto-collapse implementors" is not set to false, we collapse
2407 // all implementors.
2408 if (hideImplementors === true && e.parentNode.id === "implementors-list") {
2409 collapseDocs(newToggle, "hide", pageId);
2414 onEachLazy(document.getElementsByClassName("method"), func);
2415 onEachLazy(document.getElementsByClassName("associatedconstant"), func);
2416 onEachLazy(document.getElementsByClassName("impl"), funcImpl);
2417 var impl_call = function() {};
2418 if (hideMethodDocs === true) {
2419 impl_call = function(e, newToggle) {
2420 if (e.id.match(/^impl(?:-\d+)?$/) === null) {
2421 // Automatically minimize all non-inherent impls
2422 if (hasClass(e, "impl") === true) {
2423 collapseDocs(newToggle, "hide", pageId);
2428 var newToggle = document.createElement("a");
2429 newToggle.href = "javascript:void(0)";
2430 newToggle.className = "collapse-toggle hidden-default collapsed";
2431 newToggle.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(true) +
2432 "</span>] Show hidden undocumented items";
2433 function toggleClicked() {
2434 if (hasClass(this, "collapsed")) {
2435 removeClass(this, "collapsed");
2436 onEachLazy(this.parentNode.getElementsByClassName("hidden"), function(x) {
2437 if (hasClass(x, "content") === false) {
2438 removeClass(x, "hidden");
2442 this.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(false) +
2443 "</span>] Hide undocumented items";
2445 addClass(this, "collapsed");
2446 onEachLazy(this.parentNode.getElementsByClassName("x"), function(x) {
2447 if (hasClass(x, "content") === false) {
2448 addClass(x, "hidden");
2449 removeClass(x, "x");
2452 this.innerHTML = "[<span class=\"inner\">" + labelForToggleButton(true) +
2453 "</span>] Show hidden undocumented items";
2456 onEachLazy(document.getElementsByClassName("impl-items"), function(e) {
2457 onEachLazy(e.getElementsByClassName("associatedconstant"), func);
2458 var hiddenElems = e.getElementsByClassName("hidden");
2459 var needToggle = false;
2461 var hlength = hiddenElems.length;
2462 for (var i = 0; i < hlength; ++i) {
2463 if (hasClass(hiddenElems[i], "content") === false &&
2464 hasClass(hiddenElems[i], "docblock") === false) {
2469 if (needToggle === true) {
2470 var inner_toggle = newToggle.cloneNode(true);
2471 inner_toggle.onclick = toggleClicked;
2472 e.insertBefore(inner_toggle, e.firstChild);
2473 impl_call(e.previousSibling, inner_toggle);
2477 var currentType = document.getElementsByClassName("type-decl")[0];
2478 var className = null;
2480 currentType = currentType.getElementsByClassName("rust")[0];
2482 currentType.classList.forEach(function(item) {
2483 if (item !== "main") {
2490 var showItemDeclarations = getCurrentValue("rustdoc-auto-hide-" + className);
2491 if (showItemDeclarations === null) {
2492 if (className === "enum" || className === "macro") {
2493 showItemDeclarations = "false";
2494 } else if (className === "struct" || className === "union" || className === "trait") {
2495 showItemDeclarations = "true";
2497 // In case we found an unknown type, we just use the "parent" value.
2498 showItemDeclarations = getCurrentValue("rustdoc-auto-hide-declarations");
2501 showItemDeclarations = showItemDeclarations === "false";
2502 function buildToggleWrapper(e) {
2503 if (hasClass(e, "autohide")) {
2504 var wrap = e.previousElementSibling;
2505 if (wrap && hasClass(wrap, "toggle-wrapper")) {
2506 var inner_toggle = wrap.childNodes[0];
2507 var extra = e.childNodes[0].tagName === "H3";
2509 e.style.display = "none";
2510 addClass(wrap, "collapsed");
2511 onEachLazy(inner_toggle.getElementsByClassName("inner"), function(e) {
2512 e.innerHTML = labelForToggleButton(true);
2514 onEachLazy(inner_toggle.getElementsByClassName("toggle-label"), function(e) {
2515 e.style.display = "inline-block";
2516 if (extra === true) {
2517 e.innerHTML = " Show " + e.childNodes[0].innerHTML;
2522 if (e.parentNode.id === "main") {
2523 var otherMessage = "";
2527 if (hasClass(e, "type-decl")) {
2529 otherMessage = " Show declaration";
2530 if (showItemDeclarations === false) {
2531 extraClass = "collapsed";
2533 } else if (hasClass(e, "sub-variant")) {
2534 otherMessage = " Show fields";
2535 } else if (hasClass(e, "non-exhaustive")) {
2536 otherMessage = " This ";
2537 if (hasClass(e, "non-exhaustive-struct")) {
2538 otherMessage += "struct";
2539 } else if (hasClass(e, "non-exhaustive-enum")) {
2540 otherMessage += "enum";
2541 } else if (hasClass(e, "non-exhaustive-variant")) {
2542 otherMessage += "enum variant";
2543 } else if (hasClass(e, "non-exhaustive-type")) {
2544 otherMessage += "type";
2546 otherMessage += " is marked as non-exhaustive";
2547 } else if (hasClass(e.childNodes[0], "impl-items")) {
2548 extraClass = "marg-left";
2551 e.parentNode.insertBefore(
2557 hasClass(e, "type-decl") === false || showItemDeclarations === true),
2559 if (hasClass(e, "type-decl") === true && showItemDeclarations === true) {
2560 collapseDocs(e.previousSibling.childNodes[0], "toggle");
2562 if (hasClass(e, "non-exhaustive") === true) {
2563 collapseDocs(e.previousSibling.childNodes[0], "toggle");
2568 onEachLazy(document.getElementsByClassName("docblock"), buildToggleWrapper);
2569 onEachLazy(document.getElementsByClassName("sub-variant"), buildToggleWrapper);
2570 var pageId = getPageId();
2572 autoCollapse(pageId, getCurrentValue("rustdoc-collapse") === "true");
2574 if (pageId !== null) {
2575 expandSection(pageId);
2579 function createToggleWrapper(tog) {
2580 var span = document.createElement("span");
2581 span.className = "toggle-label";
2582 span.style.display = "none";
2583 span.innerHTML = " Expand attributes";
2584 tog.appendChild(span);
2586 var wrapper = document.createElement("div");
2587 wrapper.className = "toggle-wrapper toggle-attributes";
2588 wrapper.appendChild(tog);
2593 // To avoid checking on "rustdoc-item-attributes" value on every loop...
2594 var itemAttributesFunc = function() {};
2595 if (getCurrentValue("rustdoc-auto-hide-attributes") !== "false") {
2596 itemAttributesFunc = function(x) {
2597 collapseDocs(x.previousSibling.childNodes[0], "toggle");
2600 var attributesToggle = createToggleWrapper(createSimpleToggle(false));
2601 onEachLazy(main.getElementsByClassName("attributes"), function(i_e) {
2602 var attr_tog = attributesToggle.cloneNode(true);
2603 if (hasClass(i_e, "top-attr") === true) {
2604 addClass(attr_tog, "top-attr");
2606 i_e.parentNode.insertBefore(attr_tog, i_e);
2607 itemAttributesFunc(i_e);
2612 // To avoid checking on "rustdoc-line-numbers" value on every loop...
2613 var lineNumbersFunc = function() {};
2614 if (getCurrentValue("rustdoc-line-numbers") === "true") {
2615 lineNumbersFunc = function(x) {
2616 var count = x.textContent.split("\n").length;
2618 for (var i = 0; i < count; ++i) {
2621 var node = document.createElement("pre");
2622 addClass(node, "line-number");
2623 node.innerHTML = elems.join("\n");
2624 x.parentNode.insertBefore(node, x);
2627 onEachLazy(document.getElementsByClassName("rust-example-rendered"), function(e) {
2628 if (hasClass(e, "compile_fail")) {
2629 e.addEventListener("mouseover", function() {
2630 this.parentElement.previousElementSibling.childNodes[0].style.color = "#f00";
2632 e.addEventListener("mouseout", function() {
2633 this.parentElement.previousElementSibling.childNodes[0].style.color = "";
2635 } else if (hasClass(e, "ignore")) {
2636 e.addEventListener("mouseover", function() {
2637 this.parentElement.previousElementSibling.childNodes[0].style.color = "#ff9200";
2639 e.addEventListener("mouseout", function() {
2640 this.parentElement.previousElementSibling.childNodes[0].style.color = "";
2647 onEachLazy(document.getElementsByClassName("notable-traits"), function(e) {
2648 e.onclick = function() {
2649 this.getElementsByClassName('notable-traits-tooltiptext')[0]
2650 .classList.toggle("force-tooltip");
2654 // In the search display, allows to switch between tabs.
2655 function printTab(nb) {
2656 if (nb === 0 || nb === 1 || nb === 2) {
2660 onEachLazy(document.getElementById("titles").childNodes, function(elem) {
2661 if (nb_copy === 0) {
2662 addClass(elem, "selected");
2664 removeClass(elem, "selected");
2668 onEachLazy(document.getElementById("results").childNodes, function(elem) {
2670 elem.style.display = "";
2672 elem.style.display = "none";
2678 function putBackSearch(search_input) {
2679 var search = getSearchElement();
2680 if (search_input.value !== "" && hasClass(search, "hidden")) {
2681 showSearchResults(search);
2682 if (browserSupportsHistoryApi()) {
2683 history.replaceState(search_input.value,
2685 "?search=" + encodeURIComponent(search_input.value));
2690 function getSearchLoadingText() {
2691 return "Loading search results...";
2695 search_input.onfocus = function() {
2696 putBackSearch(this);
2700 var params = getQueryStringParams();
2701 if (params && params.search) {
2702 var search = getSearchElement();
2703 search.innerHTML = "<h3 style=\"text-align: center;\">" + getSearchLoadingText() + "</h3>";
2704 showSearchResults(search);
2707 var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];
2709 sidebar_menu.onclick = function() {
2710 var sidebar = document.getElementsByClassName("sidebar")[0];
2711 if (hasClass(sidebar, "mobile") === true) {
2720 onEachLazy(main.getElementsByClassName("loading-content"), function(e) {
2723 onEachLazy(main.childNodes, function(e) {
2724 // Unhide the actual content once loading is complete. Headers get
2725 // flex treatment for their horizontal layout, divs get block treatment
2726 // for vertical layout (column-oriented flex layout for divs caused
2727 // errors in mobile browsers).
2728 if (e.tagName === "H2" || e.tagName === "H3") {
2729 var nextTagName = e.nextElementSibling.tagName;
2730 if (nextTagName == "H2" || nextTagName == "H3") {
2731 e.nextElementSibling.style.display = "flex";
2733 e.nextElementSibling.style.display = "block";
2739 function enableSearchInput() {
2741 search_input.removeAttribute('disabled');
2745 window.addSearchOptions = function(crates) {
2746 var elem = document.getElementById("crate-search");
2749 enableSearchInput();
2752 var crates_text = [];
2753 if (Object.keys(crates).length > 1) {
2754 for (var crate in crates) {
2755 if (hasOwnProperty(crates, crate)) {
2756 crates_text.push(crate);
2760 crates_text.sort(function(a, b) {
2761 var lower_a = a.toLowerCase();
2762 var lower_b = b.toLowerCase();
2764 if (lower_a < lower_b) {
2766 } else if (lower_a > lower_b) {
2771 var savedCrate = getCurrentValue("rustdoc-saved-filter-crate");
2772 for (var i = 0; i < crates_text.length; ++i) {
2773 var option = document.createElement("option");
2774 option.value = crates_text[i];
2775 option.innerText = crates_text[i];
2776 elem.appendChild(option);
2777 // Set the crate filter from saved storage, if the current page has the saved crate
2780 // If not, ignore the crate filter -- we want to support filtering for crates on sites
2781 // like doc.rust-lang.org where the crates may differ from page to page while on the
2783 if (crates_text[i] === savedCrate) {
2784 elem.value = savedCrate;
2787 enableSearchInput();
2790 function buildHelperPopup() {
2791 var popup = document.createElement("aside");
2792 addClass(popup, "hidden");
2795 var book_info = document.createElement("span");
2796 book_info.innerHTML = "You can find more information in \
2797 <a href=\"https://doc.rust-lang.org/rustdoc/\">the rustdoc book</a>.";
2799 var container = document.createElement("div");
2801 ["?", "Show this help dialog"],
2802 ["S", "Focus the search field"],
2803 ["↑", "Move up in search results"],
2804 ["↓", "Move down in search results"],
2805 ["↹", "Switch tab"],
2806 ["⏎", "Go to active search result"],
2807 ["+", "Expand all sections"],
2808 ["-", "Collapse all sections"],
2809 ].map(x => "<dt><kbd>" + x[0] + "</kbd></dt><dd>" + x[1] + "</dd>").join("");
2810 var div_shortcuts = document.createElement("div");
2811 addClass(div_shortcuts, "shortcuts");
2812 div_shortcuts.innerHTML = "<h2>Keyboard Shortcuts</h2><dl>" + shortcuts + "</dl></div>";
2815 "Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to \
2816 restrict the search to a given item kind.",
2817 "Accepted kinds are: <code>fn</code>, <code>mod</code>, <code>struct</code>, \
2818 <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, \
2819 and <code>const</code>.",
2820 "Search functions by type signature (e.g., <code>vec -> usize</code> or \
2821 <code>* -> vec</code>)",
2822 "Search multiple things at once by splitting your query with comma (e.g., \
2823 <code>str,u8</code> or <code>String,struct:Vec,test</code>)",
2824 "You can look for items with an exact name by putting double quotes around \
2825 your request: <code>\"string\"</code>",
2826 "Look for items inside another one by searching for a path: <code>vec::Vec</code>",
2827 ].map(x => "<p>" + x + "</p>").join("");
2828 var div_infos = document.createElement("div");
2829 addClass(div_infos, "infos");
2830 div_infos.innerHTML = "<h2>Search Tricks</h2>" + infos;
2832 container.appendChild(book_info);
2833 container.appendChild(div_shortcuts);
2834 container.appendChild(div_infos);
2836 popup.appendChild(container);
2837 insertAfter(popup, getSearchElement());
2838 // So that it's only built once and then it'll do nothing when called!
2839 buildHelperPopup = function() {};
2843 window.onhashchange = onHashChange;
2846 // This is required in firefox. Explanations: when going back in the history, firefox doesn't re-run
2847 // the JS, therefore preventing rustdoc from setting a few things required to be able to reload the
2848 // previous search results (if you navigated to a search result with the keyboard, pressed enter on
2849 // it to navigate to that result, and then came back to this page).
2850 window.onunload = function(){};