use std::{collections::HashMap, hash::Hash}; pub trait Utf8Len { fn utf8_len(&self) -> usize; fn len_at_char(&self, idx: usize) -> usize; } impl Utf8Len for &str { fn utf8_len(&self) -> usize { self.chars().count() } fn len_at_char(&self, idx: usize) -> usize { self.char_indices().nth(idx).map_or_else(|| self.len(), |(i, _)| i) } } pub trait CountItems: Sized + Iterator { fn map_count(self) -> HashMap { let mut map = HashMap::new(); for i in self { *map.entry(i).or_default() += 1; } map } } impl> CountItems for V {} pub trait CollectVec: Sized + Iterator { fn collect_vec(self) -> Vec { self.collect() } fn collect_box(self) -> Box<[T]> { self.collect() } fn collect_arr(self) -> [T; N] { let vec = self.collect_vec(); ASSERT!(vec.len() == N, "Collecting into array of wrong length"); unsafe { vec.try_into().unwrap_unchecked() } } } impl, T> CollectVec for V {}