1 // SPDX-License-Identifier: GPL-2.0 2 3 use core::time::Duration; 4 5 use kernel::prelude::*; 6 use kernel::time::Instant; 7 8 pub(crate) const fn to_lowercase_bytes<const N: usize>(s: &str) -> [u8; N] { 9 let src = s.as_bytes(); 10 let mut dst = [0; N]; 11 let mut i = 0; 12 13 while i < src.len() && i < N { 14 dst[i] = (src[i] as char).to_ascii_lowercase() as u8; 15 i += 1; 16 } 17 18 dst 19 } 20 21 pub(crate) const fn const_bytes_to_str(bytes: &[u8]) -> &str { 22 match core::str::from_utf8(bytes) { 23 Ok(string) => string, 24 Err(_) => kernel::build_error!("Bytes are not valid UTF-8."), 25 } 26 } 27 28 /// Wait until `cond` is true or `timeout` elapsed. 29 /// 30 /// When `cond` evaluates to `Some`, its return value is returned. 31 /// 32 /// `Err(ETIMEDOUT)` is returned if `timeout` has been reached without `cond` evaluating to 33 /// `Some`. 34 /// 35 /// TODO[DLAY]: replace with `read_poll_timeout` once it is available. 36 /// (https://lore.kernel.org/lkml/20250220070611.214262-8-fujita.tomonori@gmail.com/) 37 pub(crate) fn wait_on<R, F: Fn() -> Option<R>>(timeout: Duration, cond: F) -> Result<R> { 38 let start_time = Instant::now(); 39 40 loop { 41 if let Some(ret) = cond() { 42 return Ok(ret); 43 } 44 45 if start_time.elapsed().as_nanos() > timeout.as_nanos() as i64 { 46 return Err(ETIMEDOUT); 47 } 48 } 49 } 50