From 9d436ee5e9bb85f2f9ca690cb50c7977a2f6d7f9 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 9 Apr 2023 19:46:51 +0200 Subject: [PATCH] common.rs: port get_by_sorted_name() --- fish-rust/src/common.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fish-rust/src/common.rs b/fish-rust/src/common.rs index d22238265..7de35c916 100644 --- a/fish-rust/src/common.rs +++ b/fish-rust/src/common.rs @@ -1971,6 +1971,19 @@ macro_rules! assert_sorted_by_name { }; } +pub trait Named { + fn name(&self) -> &'static wstr; +} + +/// \return a pointer to the first entry with the given name, assuming the entries are sorted by +/// name. \return nullptr if not found. +pub fn get_by_sorted_name(name: &wstr, vals: &'static [T]) -> Option<&'static T> { + match vals.binary_search_by_key(&name, |val| val.name()) { + Ok(index) => Some(&vals[index]), + Err(_) => None, + } +} + #[allow(unused_macros)] macro_rules! fwprintf { ($fd:expr, $format:literal $(, $arg:expr)*) => {