werkzeug/src/mem.rs
Janis 9e3fa2cdb0 u32,u64,u16 conversion functions, sync queue, random utilities
- `NextIf` iterator trait that yields the next element iff some predicate holds
- `u64_from_u32s`, `u32s_from_u64`, `u32_from_u16s`, `u16s_from_u32` functions
- moved `can_transmute` to `mem` module, added `is_same_size` and `is_aligned`
functions
- `copy_from` function for `TaggedAtomicPtr`
- attempt at a sync queue?
- `is_whitespace` function
2025-08-06 22:51:27 +02:00

23 lines
830 B
Rust

pub const fn can_transmute<A, B>() -> bool {
use core::mem::{align_of, size_of};
// We can transmute `A` to `B` iff `A` and `B` have the same size and the
// alignment of `A` is greater than or equal to the alignment of `B`.
(size_of::<A>() == size_of::<B>()) & (align_of::<A>() >= align_of::<B>())
}
pub const fn is_same_size<A, B>() -> bool {
use core::mem::size_of;
size_of::<A>() == size_of::<B>()
}
/// Checks if `A` is aligned at least as well as `B`. e.g. `assert_aligned<u64,
/// u32>()` returns `true`, but `assert_aligned<u32, u64>()` returns
/// `false`. This is useful for ensuring that a type `A` can be safely cast to a
/// type `B` without violating alignment requirements.
pub const fn is_aligned<A, B>() -> bool {
use core::mem::align_of;
align_of::<A>() >= align_of::<B>()
}