common.rs: port get_by_sorted_name()

This commit is contained in:
Johannes Altmanninger 2023-04-09 19:46:51 +02:00
parent c6b8b7548f
commit 9d436ee5e9

View file

@ -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<T: Named>(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)*) => {