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