summaryrefslogtreecommitdiff
path: root/kernel/src/util.rs
diff options
context:
space:
mode:
authorNathan Ringo <nathan@remexre.com>2024-09-01 19:59:44 -0500
committerNathan Ringo <nathan@remexre.com>2024-09-01 19:59:44 -0500
commit386df39c9866a4d945de46ef0dcab2363c674e0e (patch)
treec0572ce6a2c81c93546210f599dff553783c5760 /kernel/src/util.rs
parent6b98b6afea6e790abe738a67aa28bab54c91afe0 (diff)
Move almost all the kernel into crates/.
Diffstat (limited to 'kernel/src/util.rs')
-rw-r--r--kernel/src/util.rs99
1 files changed, 0 insertions, 99 deletions
diff --git a/kernel/src/util.rs b/kernel/src/util.rs
deleted file mode 100644
index dbcb228..0000000
--- a/kernel/src/util.rs
+++ /dev/null
@@ -1,99 +0,0 @@
-//! Miscellaneous utilities.
-
-use core::{mem::size_of, ops::Range};
-
-#[cold]
-#[inline(always)]
-fn cold() {}
-
-/// A hint that `b` is likely to be true. See `core::intrinsics::likely`.
-#[inline(always)]
-pub fn likely(b: bool) -> bool {
- if !b {
- cold()
- }
- b
-}
-
-/// A hint that `b` is likely to be false. See `core::intrinsics::unlikely`.
-#[inline(always)]
-pub fn unlikely(b: bool) -> bool {
- if b {
- cold()
- }
- b
-}
-
-/// A version of `std::dbg` built on top of `log::debug` instead of
-/// `std::eprintln`.
-///
-/// This code is copied from libstd, and inherits its copyright.
-#[macro_export]
-macro_rules! dbg {
- // NOTE: We cannot use `concat!` to make a static string as a format
- // argument of `log::debug!` because the `$expr` expression could be a
- // block (`{ .. }`), in which case the format string will be malformed.
- () => {
- log::debug!("")
- };
- ($expr:expr $(,)?) => {
- // Use of `match` here is intentional because it affects the lifetimes
- // of temporaries - https://stackoverflow.com/a/48732525/1063961
- match $expr {
- tmp => {
- log::debug!("{} = {:#?}", core::stringify!($expr), &tmp);
- tmp
- }
- }
- };
- ($($expr:expr),+ $(,)?) => {
- ($($crate::dbg!($expr)),+,)
- };
-}
-
-/// Returns whether the two ranges overlap.
-pub fn ranges_overlap<T: Copy + Ord>(r1: &Range<T>, r2: &Range<T>) -> bool {
- r1.start.max(r2.start) < r1.end.min(r2.end)
-}
-
-/// A trait for types that can be converted to from big-endian or little-endian byte slices.
-pub trait FromEndianBytes {
- /// Converts from a big-endian byte slice.
- fn from_big_endian_bytes(bytes: &[u8]) -> Self;
-
- /// Converts from a little-endian byte slice.
- fn from_little_endian_bytes(bytes: &[u8]) -> Self;
-}
-
-macro_rules! impl_FromEndianBytes {
- ($($ty:ty),* $(,)?) => {
- $(impl FromEndianBytes for $ty {
- fn from_big_endian_bytes(bytes: &[u8]) -> $ty {
- let chunk = match bytes.last_chunk() {
- Some(chunk) => *chunk,
- None => {
- let mut chunk = [0; size_of::<$ty>()];
- chunk[size_of::<$ty>() - bytes.len()..]
- .copy_from_slice(bytes);
- chunk
- },
- };
- <$ty>::from_be_bytes(chunk)
- }
-
- fn from_little_endian_bytes(bytes: &[u8]) -> $ty {
- let chunk = match bytes.first_chunk() {
- Some(chunk) => *chunk,
- None => {
- let mut chunk = [0; size_of::<$ty>()];
- chunk[.. bytes.len()].copy_from_slice(bytes);
- chunk
- },
- };
- <$ty>::from_le_bytes(chunk)
- }
- })*
- };
-}
-
-impl_FromEndianBytes!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);