1use anyhow::{bail, Context, Error};
2use std::collections::{hash_map::Entry, BTreeMap, HashMap, HashSet};
3use std::env;
4use std::fs;
5use std::mem;
6use std::path::{Path, PathBuf};
7use std::str;
8use walrus::Module;
9
10pub(crate) const PLACEHOLDER_MODULE: &str = "__wbindgen_placeholder__";
11
12mod decode;
13mod descriptor;
14mod descriptors;
15mod externref;
16mod interpreter;
17mod intrinsic;
18mod js;
19mod multivalue;
20mod suggest;
21mod transforms;
22pub mod wasm2es6js;
23mod wasm_conventions;
24mod wit;
25
26pub struct Bindgen {
27 input: Input,
28 out_name: Option<String>,
29 mode: OutputMode,
30 debug: bool,
31 typescript: bool,
32 omit_imports: bool,
33 demangle: bool,
34 keep_lld_exports: bool,
35 keep_debug: bool,
36 remove_name_section: bool,
37 remove_producers_section: bool,
38 omit_default_module_path: bool,
39 emit_start: bool,
40 externref: bool,
41 multi_value: bool,
42 encode_into: EncodeInto,
43 split_linked_modules: bool,
44 generate_reset_state: bool,
45}
46
47pub struct Output {
48 module: walrus::Module,
49 stem: String,
50 generated: Generated,
51}
52
53struct Generated {
54 mode: OutputMode,
55 js: String,
56 ts: String,
57 start: Option<String>,
58 snippets: BTreeMap<String, Vec<String>>,
59 local_modules: HashMap<String, String>,
60 npm_dependencies: HashMap<String, (PathBuf, String)>,
61 typescript: bool,
62 emscripten_extern_pre_js: String,
66}
67
68#[derive(Clone)]
69enum OutputMode {
70 Bundler { browser_only: bool },
71 Web,
72 NoModules { global: String },
73 Node { module: bool },
74 Deno,
75 Module,
76 Emscripten,
77}
78
79enum Input {
80 Path(PathBuf),
81 Module(Module, String),
82 Bytes(Vec<u8>, String),
83 None,
84}
85
86#[derive(Debug, Clone, Copy)]
87pub enum EncodeInto {
88 Test,
89 Always,
90 Never,
91}
92
93impl Bindgen {
94 pub fn new() -> Bindgen {
95 let externref =
96 env::var("WASM_BINDGEN_ANYREF").is_ok() || env::var("WASM_BINDGEN_EXTERNREF").is_ok();
97 let multi_value = env::var("WASM_BINDGEN_MULTI_VALUE").is_ok();
98 Bindgen {
99 input: Input::None,
100 out_name: None,
101 mode: OutputMode::Bundler {
102 browser_only: false,
103 },
104 debug: false,
105 typescript: false,
106 omit_imports: false,
107 demangle: true,
108 keep_lld_exports: false,
109 keep_debug: false,
110 remove_name_section: false,
111 remove_producers_section: false,
112 emit_start: true,
113 externref,
114 multi_value,
115 encode_into: EncodeInto::Test,
116 omit_default_module_path: true,
117 split_linked_modules: false,
118 generate_reset_state: false,
119 }
120 }
121
122 pub fn input_path<P: AsRef<Path>>(&mut self, path: P) -> &mut Bindgen {
123 self.input = Input::Path(path.as_ref().to_path_buf());
124 self
125 }
126
127 pub fn out_name(&mut self, name: &str) -> &mut Bindgen {
128 self.out_name = Some(name.to_string());
129 self
130 }
131
132 #[deprecated = "automatically detected via `-Ctarget-feature=+reference-types`"]
133 pub fn reference_types(&mut self, enable: bool) -> &mut Bindgen {
134 self.externref = enable;
135 self
136 }
137
138 pub fn input_module(&mut self, name: &str, module: Module) -> &mut Bindgen {
140 let name = name.to_string();
141 self.input = Input::Module(module, name);
142 self
143 }
144
145 pub fn input_bytes(&mut self, name: &str, bytes: Vec<u8>) -> &mut Bindgen {
147 let name = name.to_string();
148 self.input = Input::Bytes(bytes, name);
149 self
150 }
151
152 fn switch_mode(&mut self, mode: OutputMode, flag: &str) -> Result<(), Error> {
153 match self.mode {
154 OutputMode::Bundler { .. } => self.mode = mode,
155 _ => bail!("cannot specify `{flag}` with another output mode already specified"),
156 }
157 Ok(())
158 }
159
160 pub fn nodejs(&mut self, node: bool) -> Result<&mut Bindgen, Error> {
161 if node {
162 self.switch_mode(OutputMode::Node { module: false }, "--target nodejs")?;
163 }
164 Ok(self)
165 }
166
167 pub fn nodejs_module(&mut self, node: bool) -> Result<&mut Bindgen, Error> {
168 if node {
169 self.switch_mode(
170 OutputMode::Node { module: true },
171 "--target experimental-nodejs-module",
172 )?;
173 }
174 Ok(self)
175 }
176
177 pub fn bundler(&mut self, bundler: bool) -> Result<&mut Bindgen, Error> {
178 if bundler {
179 self.switch_mode(
180 OutputMode::Bundler {
181 browser_only: false,
182 },
183 "--target bundler",
184 )?;
185 }
186 Ok(self)
187 }
188
189 pub fn web(&mut self, web: bool) -> Result<&mut Bindgen, Error> {
190 if web {
191 self.switch_mode(OutputMode::Web, "--target web")?;
192 }
193 Ok(self)
194 }
195
196 pub fn no_modules(&mut self, no_modules: bool) -> Result<&mut Bindgen, Error> {
197 if no_modules {
198 self.switch_mode(
199 OutputMode::NoModules {
200 global: "wasm_bindgen".to_string(),
201 },
202 "--target no-modules",
203 )?;
204 }
205 Ok(self)
206 }
207
208 pub fn browser(&mut self, browser: bool) -> Result<&mut Bindgen, Error> {
209 if browser {
210 match &mut self.mode {
211 OutputMode::Bundler { browser_only } => *browser_only = true,
212 _ => bail!("cannot specify `--browser` with other output types"),
213 }
214 }
215 Ok(self)
216 }
217
218 pub fn deno(&mut self, deno: bool) -> Result<&mut Bindgen, Error> {
219 if deno {
220 self.switch_mode(OutputMode::Deno, "--target deno")?;
221 self.encode_into(EncodeInto::Always);
222 }
223 Ok(self)
224 }
225
226 pub fn module(&mut self, source_phase: bool) -> Result<&mut Bindgen, Error> {
227 if source_phase {
228 self.switch_mode(OutputMode::Module, "--target module")?;
229 }
230 Ok(self)
231 }
232
233 pub fn no_modules_global(&mut self, name: &str) -> Result<&mut Bindgen, Error> {
234 match &mut self.mode {
235 OutputMode::NoModules { global } => *global = name.to_string(),
236 _ => bail!("can only specify `--no-modules-global` with `--target no-modules`"),
237 }
238 Ok(self)
239 }
240
241 pub fn debug(&mut self, debug: bool) -> &mut Bindgen {
242 self.debug = debug;
243 self
244 }
245
246 pub fn typescript(&mut self, typescript: bool) -> &mut Bindgen {
247 self.typescript = typescript;
248 self
249 }
250
251 pub fn omit_imports(&mut self, omit_imports: bool) -> &mut Bindgen {
252 self.omit_imports = omit_imports;
253 self
254 }
255
256 pub fn demangle(&mut self, demangle: bool) -> &mut Bindgen {
257 self.demangle = demangle;
258 self
259 }
260
261 pub fn keep_lld_exports(&mut self, keep_lld_exports: bool) -> &mut Bindgen {
262 self.keep_lld_exports = keep_lld_exports;
263 self
264 }
265
266 pub fn keep_debug(&mut self, keep_debug: bool) -> &mut Bindgen {
267 self.keep_debug = keep_debug;
268 self
269 }
270
271 pub fn remove_name_section(&mut self, remove: bool) -> &mut Bindgen {
272 self.remove_name_section = remove;
273 self
274 }
275
276 pub fn remove_producers_section(&mut self, remove: bool) -> &mut Bindgen {
277 self.remove_producers_section = remove;
278 self
279 }
280
281 pub fn emit_start(&mut self, emit: bool) -> &mut Bindgen {
282 self.emit_start = emit;
283 self
284 }
285
286 pub fn encode_into(&mut self, mode: EncodeInto) -> &mut Bindgen {
287 self.encode_into = mode;
288 self
289 }
290
291 pub fn omit_default_module_path(&mut self, omit_default_module_path: bool) -> &mut Bindgen {
292 self.omit_default_module_path = omit_default_module_path;
293 self
294 }
295
296 pub fn split_linked_modules(&mut self, split_linked_modules: bool) -> &mut Bindgen {
297 self.split_linked_modules = split_linked_modules;
298 self
299 }
300
301 pub fn reset_state_function(&mut self, generate_reset_state: bool) -> &mut Bindgen {
302 self.generate_reset_state = generate_reset_state;
303 self
304 }
305
306 pub fn generate<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
307 self.generate_output()?.emit(path.as_ref())
308 }
309
310 pub fn stem(&self) -> Result<&str, Error> {
311 Ok(match &self.input {
312 Input::None => bail!("must have an input by now"),
313 Input::Module(_, name) | Input::Bytes(_, name) => name,
314 Input::Path(path) => match &self.out_name {
315 Some(name) => name,
316 None => path.file_stem().unwrap().to_str().unwrap(),
317 },
318 })
319 }
320
321 pub fn generate_output(&mut self) -> Result<Output, Error> {
322 let mut module = match self.input {
323 Input::None => bail!("must have an input by now"),
324 Input::Module(ref mut m, _) => {
325 let blank_module = Module::default();
326 mem::replace(m, blank_module)
327 }
328 Input::Path(ref path) => {
329 let bytes = std::fs::read(path)
330 .with_context(|| format!("failed reading '{}'", path.display()))?;
331 self.module_from_bytes(&bytes).with_context(|| {
332 format!("failed getting Wasm module for '{}'", path.display())
333 })?
334 }
335 Input::Bytes(ref bytes, _) => self
336 .module_from_bytes(bytes)
337 .context("failed getting Wasm module")?,
338 };
339
340 if module
341 .customs
342 .remove_raw("__wasm_bindgen_emscripten_marker")
343 .is_some()
344 {
345 self.mode = OutputMode::Emscripten;
347 }
348
349 if let Ok(true) = wasm_conventions::target_feature(&module, "reference-types") {
351 self.externref = true;
352 }
353
354 if let Ok(true) = wasm_conventions::target_feature(&module, "multivalue") {
356 self.multi_value = true;
357 }
358
359 if matches!(self.mode, OutputMode::Web)
361 && module.exports.iter().any(|export| export.name == "default")
362 {
363 bail!("exported symbol \"default\" not allowed for --target web")
364 }
365
366 if self.generate_reset_state
368 && !matches!(
369 self.mode,
370 OutputMode::Module | OutputMode::Web | OutputMode::Node { module: false }
371 )
372 {
373 bail!("--experimental-reset-state-function is only supported for --target module, --target web, or --target nodejs")
374 }
375
376 let thread_count = transforms::threads::run(&mut module)
377 .with_context(|| "failed to prepare module for threading")?;
378
379 if self.demangle {
382 demangle(&mut module);
383 }
384 if !self.keep_lld_exports && !self.mode.emscripten() {
385 unexported_unused_lld_things(&mut module);
386 }
387 {
390 let exn_import = module.imports.iter().find_map(|impt| match impt.kind {
391 walrus::ImportKind::Tag(id)
392 if impt.module == "env" && impt.name == "__cpp_exception" =>
393 {
394 Some((impt, id))
395 }
396 _ => None,
397 });
398 if let Some((import, id)) = exn_import {
399 let original_import_id = import.id();
400 let tag = module.tags.get_mut(id);
401 tag.kind = walrus::TagKind::Local;
402 module.imports.delete(original_import_id);
403 module.exports.add("__cpp_exception", tag.id);
404 }
405
406 module
408 .producers
409 .add_processed_by("wasm-bindgen", &wasm_bindgen_shared::version());
410 }
411 let mut storage = Vec::new();
420 let programs = wit::extract_programs(&mut module, &mut storage)?;
421
422 descriptors::execute(&mut module)?;
427
428 wit::process(self, &mut module, programs, thread_count)?;
434
435 if self.externref {
445 externref::process(&mut module)?;
446 } else {
447 let ids = module
448 .exports
449 .iter()
450 .filter(|e| e.name.starts_with("__externref"))
451 .map(|e| e.id())
452 .collect::<Vec<_>>();
453 for id in ids {
454 module.exports.delete(id);
455 }
456 externref::force_contiguous_elements(&mut module)?;
461 }
462
463 if self.multi_value {
466 multivalue::run(&mut module)
467 .context("failed to transform return pointers into multi-value Wasm")?;
468 }
469
470 if !matches!(self.mode, OutputMode::Emscripten) {
481 generate_wasm_catch_wrappers(&mut module)?;
482 }
483
484 gc_module_and_adapters(&mut module);
488
489 let stem = self.stem()?;
490
491 let aux = module
493 .customs
494 .delete_typed::<wit::WasmBindgenAux>()
495 .expect("aux section should be present");
496 let adapters = module
497 .customs
498 .delete_typed::<wit::NonstandardWitSection>()
499 .unwrap();
500 let mut cx = js::Context::new(&mut module, self, &adapters, &aux)?;
501 cx.generate()?;
502 let js::FinalizedOutput {
503 js,
504 ts,
505 start,
506 emscripten_extern_pre_js,
507 } = cx.finalize(stem)?;
508 let generated = Generated {
509 snippets: aux.snippets.clone(),
510 local_modules: aux.local_modules.clone(),
511 mode: self.mode.clone(),
512 typescript: self.typescript,
513 npm_dependencies: cx.npm_dependencies.clone(),
514 js,
515 ts,
516 start,
517 emscripten_extern_pre_js,
518 };
519
520 Ok(Output {
521 module,
522 stem: stem.to_string(),
523 generated,
524 })
525 }
526
527 fn module_from_bytes(&self, bytes: &[u8]) -> Result<Module, Error> {
528 walrus::ModuleConfig::new()
529 .strict_validate(false)
536 .generate_dwarf(self.keep_debug)
537 .generate_name_section(!self.remove_name_section)
538 .generate_producers_section(!self.remove_producers_section)
539 .parse(bytes)
540 .context("failed to parse input as wasm")
541 }
542
543 fn local_module_name(&self, module: &str) -> String {
544 format!("./snippets/{module}")
545 }
546
547 fn inline_js_module_name(
548 &self,
549 unique_crate_identifier: &str,
550 snippet_idx_in_crate: usize,
551 ) -> String {
552 format!("./snippets/{unique_crate_identifier}/inline{snippet_idx_in_crate}.js",)
553 }
554}
555
556fn reset_indentation(s: &str) -> String {
557 let mut indent: u32 = 0;
558 let mut dst = String::new();
559
560 fn is_doc_comment(line: &str) -> bool {
561 line.starts_with("*")
562 }
563
564 static TAB: &str = " ";
565
566 for line in s.trim().lines() {
567 let line = line.trim();
568
569 if is_doc_comment(line) {
571 for _ in 0..indent {
572 dst.push_str(TAB);
573 }
574 dst.push(' ');
575 dst.push_str(line);
576 dst.push('\n');
577 continue;
578 }
579
580 if line.starts_with('}') {
581 indent = indent.saturating_sub(1);
582 }
583
584 let extra = if line.starts_with(':') || line.starts_with('?') {
585 1
586 } else {
587 0
588 };
589 if !line.is_empty() {
590 for _ in 0..indent + extra {
591 dst.push_str(TAB);
592 }
593 dst.push_str(line);
594 }
595 dst.push('\n');
596
597 if line.ends_with('{') {
598 indent += 1;
599 }
600 }
601 dst
602}
603
604fn demangle(module: &mut Module) {
610 let (lower, upper) = module.funcs.iter().size_hint();
611 let mut counter: HashMap<String, i32> = HashMap::with_capacity(upper.unwrap_or(lower));
612
613 for func in module.funcs.iter_mut() {
614 let Some(name) = &func.name else {
615 continue;
616 };
617
618 let Ok(sym) = rustc_demangle::try_demangle(name) else {
619 continue;
620 };
621
622 let demangled = sym.to_string();
623 match counter.entry(demangled) {
624 Entry::Occupied(mut entry) => {
625 func.name = Some(format!("{}[{}]", entry.key(), entry.get()));
626 *entry.get_mut() += 1;
627 }
628 Entry::Vacant(entry) => {
629 func.name = Some(entry.key().clone());
630 entry.insert(1);
631 }
632 }
633 }
634}
635
636impl OutputMode {
637 fn uses_es_modules(&self) -> bool {
638 matches!(
639 self,
640 OutputMode::Bundler { .. }
641 | OutputMode::Web
642 | OutputMode::Node { module: true }
643 | OutputMode::Deno
644 | OutputMode::Module
645 )
646 }
647
648 fn nodejs(&self) -> bool {
649 matches!(self, OutputMode::Node { .. })
650 }
651
652 fn no_modules(&self) -> bool {
653 matches!(self, OutputMode::NoModules { .. })
654 }
655
656 fn bundler(&self) -> bool {
657 matches!(self, OutputMode::Bundler { .. })
658 }
659
660 fn emscripten(&self) -> bool {
661 matches!(self, OutputMode::Emscripten)
662 }
663}
664
665fn unexported_unused_lld_things(module: &mut Module) {
669 let mut to_remove = Vec::new();
670 for export in module.exports.iter() {
671 match export.name.as_str() {
672 "__heap_base" | "__data_end" | "__indirect_function_table" => {
673 to_remove.push(export.id());
674 }
675 _ => {}
676 }
677 }
678 for id in to_remove {
679 module.exports.delete(id);
680 }
681}
682
683impl Output {
684 pub fn js(&self) -> &str {
685 &self.generated.js
686 }
687
688 pub fn ts(&self) -> Option<&str> {
689 if self.generated.typescript {
690 Some(&self.generated.ts)
691 } else {
692 None
693 }
694 }
695
696 pub fn start(&self) -> Option<&String> {
697 self.generated.start.as_ref()
698 }
699
700 pub fn snippets(&self) -> &BTreeMap<String, Vec<String>> {
701 &self.generated.snippets
702 }
703
704 pub fn local_modules(&self) -> &HashMap<String, String> {
705 &self.generated.local_modules
706 }
707
708 pub fn npm_dependencies(&self) -> &HashMap<String, (PathBuf, String)> {
709 &self.generated.npm_dependencies
710 }
711
712 pub fn wasm(&self) -> &walrus::Module {
713 &self.module
714 }
715
716 pub fn wasm_mut(&mut self) -> &mut walrus::Module {
717 &mut self.module
718 }
719
720 pub fn emit(&mut self, out_dir: impl AsRef<Path>) -> Result<(), Error> {
721 self._emit(out_dir.as_ref())
722 }
723
724 fn _emit(&mut self, out_dir: &Path) -> Result<(), Error> {
725 let wasm_name = format!("{}_bg", self.stem);
726 let wasm_path = out_dir.join(&wasm_name).with_extension("wasm");
727 fs::create_dir_all(out_dir)?;
728
729 let wasm_bytes = self.module.emit_wasm();
730 fs::write(&wasm_path, wasm_bytes)
731 .with_context(|| format!("failed to write `{}`", wasm_path.display()))?;
732
733 let gen = &self.generated;
734
735 for (identifier, list) in gen.snippets.iter() {
738 for (i, js) in list.iter().enumerate() {
739 let name = format!("inline{i}.js");
740 let path = out_dir.join("snippets").join(identifier).join(name);
741 fs::create_dir_all(path.parent().unwrap())?;
742 fs::write(&path, js)
743 .with_context(|| format!("failed to write `{}`", path.display()))?;
744 }
745 }
746
747 for (path, contents) in gen.local_modules.iter() {
748 let path = out_dir.join("snippets").join(path);
749 fs::create_dir_all(path.parent().unwrap())?;
750 fs::write(&path, contents)
751 .with_context(|| format!("failed to write `{}`", path.display()))?;
752 }
753
754 let is_genmode_nodemodule = matches!(gen.mode, OutputMode::Node { module: true });
755 if !gen.npm_dependencies.is_empty() || is_genmode_nodemodule {
756 #[derive(serde::Serialize)]
757 struct PackageJson<'a> {
758 #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
759 ty: Option<&'static str>,
760 dependencies: BTreeMap<&'a str, &'a str>,
761 }
762 let pj = PackageJson {
763 ty: is_genmode_nodemodule.then_some("module"),
764 dependencies: gen
765 .npm_dependencies
766 .iter()
767 .map(|(k, v)| (k.as_str(), v.1.as_str()))
768 .collect(),
769 };
770 let json = serde_json::to_string_pretty(&pj)?;
771 fs::write(out_dir.join("package.json"), json)?;
772 }
773
774 let extension = "js";
777
778 fn write<P, C>(path: P, contents: C) -> Result<(), anyhow::Error>
779 where
780 P: AsRef<Path>,
781 C: AsRef<[u8]>,
782 {
783 fs::write(&path, contents)
784 .with_context(|| format!("failed to write `{}`", path.as_ref().display()))
785 }
786
787 let js_path = out_dir.join(&self.stem).with_extension(extension);
788 if matches!(self.generated.mode, OutputMode::Emscripten) {
789 let emscripten_js_path = out_dir.join("library_bindgen.js");
790 write(&emscripten_js_path, reset_indentation(&gen.js))?;
791 let extern_pre_js_path = out_dir.join("library_bindgen.extern-pre.js");
799 if gen.emscripten_extern_pre_js.is_empty() {
800 let _ = fs::remove_file(&extern_pre_js_path);
801 } else {
802 write(
803 &extern_pre_js_path,
804 reset_indentation(&gen.emscripten_extern_pre_js),
805 )?;
806 }
807 } else {
808 write(&js_path, reset_indentation(&gen.js))?;
809 }
810
811 if let Some(start) = &gen.start {
812 let js_path = out_dir.join(wasm_name).with_extension(extension);
813 write(&js_path, reset_indentation(start))?;
814 }
815
816 if gen.typescript {
817 let ts_path = js_path.with_extension("d.ts");
818 fs::write(&ts_path, reset_indentation(&gen.ts))
819 .with_context(|| format!("failed to write `{}`", ts_path.display()))?;
820 }
821
822 if gen.typescript {
823 let ts_path = wasm_path.with_extension("wasm.d.ts");
824 let ts = wasm2es6js::typescript(&self.module)?;
825 fs::write(&ts_path, reset_indentation(&ts))
826 .with_context(|| format!("failed to write `{}`", ts_path.display()))?;
827 }
828
829 Ok(())
830 }
831}
832
833fn generate_wasm_catch_wrappers(module: &mut Module) -> Result<(), Error> {
839 let eh_version = transforms::detect_exception_handling_version(module);
840 log::debug!("Exception handling version: {eh_version:?}");
841
842 if eh_version == transforms::ExceptionHandlingVersion::None {
843 return Ok(());
844 }
845
846 let mut aux = module
848 .customs
849 .delete_typed::<wit::WasmBindgenAux>()
850 .expect("aux section should exist");
851 let wit = module
852 .customs
853 .delete_typed::<wit::NonstandardWitSection>()
854 .expect("wit section should exist");
855
856 log::debug!(
857 "Running catch handler: imports_with_catch={}, externref_table={:?}, externref_alloc={:?}, exn_store={:?}",
858 aux.imports_with_catch.len(),
859 aux.externref_table,
860 aux.externref_alloc,
861 aux.exn_store
862 );
863
864 let result = transforms::catch_handler::run(module, &mut aux, &wit, eh_version)
865 .context("failed to generate catch wrappers");
866
867 module.customs.add(*wit);
869 module.customs.add(*aux);
870
871 result?;
872
873 Ok(())
874}
875
876fn gc_module_and_adapters(module: &mut Module) {
877 loop {
878 walrus::passes::gc::run(module);
882
883 let imports_remaining = module
886 .imports
887 .iter()
888 .map(|i| i.id())
889 .collect::<HashSet<_>>();
890 let mut section = module
891 .customs
892 .delete_typed::<wit::NonstandardWitSection>()
893 .unwrap();
894 section
895 .implements
896 .retain(|pair| imports_remaining.contains(&pair.0));
897
898 let any_removed = section.gc();
904 module.customs.add(*section);
905 if !any_removed {
906 break;
907 }
908 }
909}
910
911fn sorted_iter<K, V>(map: &HashMap<K, V>) -> impl Iterator<Item = (&K, &V)>
918where
919 K: Ord,
920{
921 let mut pairs = map.iter().collect::<Vec<_>>();
922 pairs.sort_by_key(|(k, _)| *k);
923 pairs.into_iter()
924}