Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Batch Query

Querying multiple types and aggregating the results.

query_methods_batch runs each query in parallel: one thread per type, each with its own independent rust-analyzer subprocess. Total wall time is roughly the cost of the slowest single query rather than the sum of all. Errors are per-type and do not abort the batch.

batch_query.rs

For Vec<u8>, VecDeque<u8>, and LinkedList<u8>, this example returns:

  • Per-type method counts: printed after all queries complete
  • Common methods: the intersection across all three types
  • Unique methods: what each type has that neither of the others does
use rust_meth_lib::analyzer::{find_rust_analyzer, query_methods_batch};
use rust_meth_lib::error::RustMethError;
use std::collections::{BTreeMap, BTreeSet};

fn main() -> rust_meth_lib::error::Result<()> {
    let ra_path = find_rust_analyzer()?;

    let queries: &[(&str, Option<&str>)] = &[
        ("Vec<u8>", None),
        ("VecDeque<u8>", None),
        ("LinkedList<u8>", None),
    ];

    // All three queries run in parallel. Results come back in input order.
    let batch_results = query_methods_batch(queries, &ra_path);

    // -- Partition into successes and failures --

    let mut results: BTreeMap<&str, BTreeSet<String>> = BTreeMap::new();
    let mut failures: Vec<(&str, RustMethError)> = Vec::new();

    for (type_name, result) in batch_results {
        match result {
            Ok(methods) => {
                let names: BTreeSet<String> = methods.into_iter().map(|m| m.name).collect();
                println!("{type_name}: {} methods", names.len());
                results.insert(type_name, names);
            }
            Err(e) => {
                eprintln!("{type_name}: FAILED: {e}");
                failures.push((type_name, e));
            }
        }
    }

    println!();

    // -- Methods common to all successfully queried types --

    if results.len() >= 2 {
        let mut common = results.values().next().cloned().unwrap_or_default();
        for set in results.values().skip(1) {
            common = common.intersection(set).cloned().collect();
        }

        println!(
            "Methods common to all {} types ({} total):",
            results.len(),
            common.len()
        );
        for name in &common {
            println!("  {name}");
        }
        println!();
    }

    // -- Per-type unique methods --
    //
    // What does each type have that none of the others do?

    for (type_name, methods) in &results {
        let others_union: BTreeSet<&String> = results
            .iter()
            .filter(|(t, _)| *t != type_name)
            .flat_map(|(_, s)| s.iter())
            .collect();

        let unique: Vec<&String> = methods
            .iter()
            .filter(|m| !others_union.contains(m))
            .collect();

        if unique.is_empty() {
            println!("{type_name}: no unique methods");
        } else {
            println!("{type_name} unique methods ({}):", unique.len());
            for name in unique {
                println!("  {name}");
            }
        }
        println!();
    }

    // -- Summary of failures --

    if !failures.is_empty() {
        println!("Failed queries:");
        for (type_name, err) in failures {
            println!("  {type_name}: {err}");
        }
    }

    Ok(())
}

Output

Vec<u8>: 229 methods
VecDeque<u8>: 114 methods
LinkedList<u8>: 46 methods

Methods common to all 3 types (32 total):
  append
  clamp
  clear
  clone
  clone_from
  clone_into
  cmp
  contains
  eq
  extend
  extend_one
  extend_reserve
  extract_if
  ge
  gt
  into
  into_iter
  is_empty
  iter
  iter_mut
  le
  len
  lt
  max
  min
  ne
  partial_cmp
  remove
  retain
  split_off
  to_owned
  try_into

LinkedList<u8> unique methods (4):
  cursor_back
  cursor_back_mut
  cursor_front
  cursor_front_mut

Vec<u8> unique methods (156):
  align_to
  align_to_mut
  array_windows
  as_array
  as_ascii
  as_ascii_unchecked
  as_chunks
  as_chunks_mut
  as_chunks_unchecked
  as_chunks_unchecked_mut
  as_mut
  as_mut_array
  as_mut_ptr
  as_mut_ptr_range
  as_mut_slice
  as_non_null
  as_ptr
  as_ptr_range
  as_rchunks
  as_rchunks_mut
  as_ref
  as_simd
  as_simd_mut
  as_slice
  chunk_by
  chunk_by_mut
  chunks
  chunks_exact
  chunks_exact_mut
  chunks_mut
  clone_from_slice
  concat
  connect
  const_make_global
  copy_from_slice
  copy_within
  dedup
  dedup_by
  dedup_by_key
  deref
  deref_mut
  element_offset
  ends_with
  eq_ignore_ascii_case
  escape_ascii
  extend_from_slice
  fill
  fill_with
  first
  first_chunk
  first_chunk_mut
  first_mut
  get_disjoint_mut
  get_disjoint_unchecked_mut
  get_unchecked
  get_unchecked_mut
  into_array
  into_boxed_slice
  into_chunks
  into_parts
  into_parts_with_alloc
  into_raw_parts
  into_raw_parts_with_alloc
  is_ascii
  is_sorted
  is_sorted_by
  is_sorted_by_key
  join
  last
  last_chunk
  last_chunk_mut
  last_mut
  leak
  make_ascii_lowercase
  make_ascii_uppercase
  partial_sort_unstable
  partial_sort_unstable_by
  partial_sort_unstable_by_key
  partition_dedup
  partition_dedup_by
  partition_dedup_by_key
  peek_mut
  pop
  pop_if
  push
  push_mut
  push_within_capacity
  rchunks
  rchunks_exact
  rchunks_exact_mut
  rchunks_mut
  recycle
  repeat
  reverse
  rsplit
  rsplit_mut
  rsplit_once
  rsplitn
  rsplitn_mut
  select_nth_unstable
  select_nth_unstable_by
  select_nth_unstable_by_key
  set_len
  shift_left
  shift_right
  sort
  sort_by
  sort_by_cached_key
  sort_by_key
  sort_unstable
  sort_unstable_by
  sort_unstable_by_key
  spare_capacity_mut
  split_at
  split_at_checked
  split_at_mut
  split_at_mut_checked
  split_at_mut_unchecked
  split_at_spare_mut
  split_at_unchecked
  split_first
  split_first_chunk
  split_first_chunk_mut
  split_first_mut
  split_inclusive
  split_inclusive_mut
  split_last
  split_last_chunk
  split_last_chunk_mut
  split_last_mut
  split_mut
  split_once
  splitn
  splitn_mut
  starts_with
  strip_circumfix
  strip_prefix
  strip_suffix
  subslice_range
  swap_remove
  swap_unchecked
  swap_with_slice
  to_ascii_lowercase
  to_ascii_uppercase
  to_vec
  to_vec_in
  trim_ascii
  trim_ascii_end
  trim_ascii_start
  trim_prefix
  trim_suffix
  try_remove
  try_shrink_to
  try_shrink_to_fit
  utf8_chunks
  windows

VecDeque<u8> unique methods (31):
  as_mut_slices
  as_slices
  consume
  extend_front
  fill_buf
  has_data_left
  is_read_vectored
  make_contiguous
  pop_back_if
  pop_front_if
  prepend
  prepend_from_within
  range
  range_mut
  read
  read_array
  read_be
  read_buf
  read_buf_exact
  read_exact
  read_le
  read_line
  read_to_end
  read_to_string
  read_until
  read_vectored
  retain_back
  skip_until
  swap_remove_back
  swap_remove_front
  truncate_to_range