//! Extensions pub trait Tuple2Ext { fn swap(self) -> (U, T); } impl Tuple2Ext for (T, U) { #[inline(always)] fn swap(self) -> (U, T) { (self.1, self.0) } } #[inline(always)] pub fn is_utf8_char_boundary(byte: u8) -> bool { //! //! Copied from stdlib (byte as i8) >= -0x40 } pub trait StrExt { fn floor_char_boundary_impl(&self, index: usize) -> usize; } impl> StrExt for T { #[inline] fn floor_char_boundary_impl(&self, index: usize) -> usize { //! //! copied from stdlib let this = self.as_ref(); if index >= this.len() { this.len() } else { let lower_bound = index.saturating_sub(3); let new_index = this.as_bytes()[lower_bound..=index] .iter() .rposition(|&b| is_utf8_char_boundary(b)); // SAFETY: we know that the character boundary will be within four bytes unsafe { lower_bound + new_index.unwrap_unchecked() } } } }