You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: dispatch.rs
    path of file: ./repos/scryer-prolog/src/machine
    
    the code of the file until where you have to start completion: use crate::arena::*;
use crate::atom_table::*;
use crate::instructions::*;
use crate::machine::arithmetic_ops::*;
use crate::machine::machine_errors::*;
use crate::machine::
    
    fn keysort(&mut self, var_comparison: VarComparison) -> CallResult {
        self.check_keysort_errors()?;

        let stub_gen = || functor_stub(atom!("keysort"), 2);
        let list = self.try_from_list(self.registers[1], stub_gen)?;

        let mut key_pairs = Vec::with_capacity(list.len());

        for val in list {
            let key = self.project_onto_key(val)?;
            key_pairs.push((key, val));
        }

        key_pairs.sort_by(|a1, a2| {
            compare_term_test!(self, a1.0, a2.0, var_comparison).unwrap_or(Ordering::Less)
        });

        let key_pairs = key_pairs.into_iter().map(|kp| kp.1);
        let heap_addr = heap_loc_as_cell!(iter_to_heap_list(&mut self.heap, key_pairs));

        let target_addr = self.registers[2];

        unify_fn!(*self, target_addr, heap_addr);
        Ok(())
    }