Function try_binary_search_range_by

Source
pub(crate) fn try_binary_search_range_by<K, F>(
    range: Range<usize>,
    target: &K,
    key_extractor: F,
) -> Option<Result<usize, usize>>
where K: Ord, F: Fn(usize) -> Option<K>,
Expand description

Performs a binary search over a range using a fallible key extraction function; a failed key extraction immediately terminats the search.

This is similar to the standard library’s binary_search_by, but generalized to ranges instead of slices.

§Arguments

  • range - The range to search in
  • target - The target value to search for
  • key_extractor - A function that extracts a comparable key from slice elements. This function can fail and return None.

§Returns

  • Some(Ok(index)) - Element found at the given index
  • Some(Err(index)) - Element not found, but would be inserted at the given index
  • None - Key extraction failed