-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathutil.rs
45 lines (39 loc) · 1.43 KB
/
util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// SPDX-License-Identifier: MIT OR Apache-2.0
use core::ptr::{self, NonNull};
/// Copy the bytes of `val` to `ptr`, then advance pointer to just after the
/// newly-copied bytes.
pub unsafe fn ptr_write_unaligned_and_add<T>(ptr: &mut *mut u8, val: T) {
unsafe {
ptr.cast::<T>().write_unaligned(val);
*ptr = ptr.add(size_of::<T>());
}
}
/// Convert from a `u32` to a `usize`. Panic if the input does fit. On typical
/// targets `usize` is at least as big as `u32`, so this should never panic
/// except on unusual targets.
///
/// Comparison to alternatives:
/// * `val as usize` doesn't check that `val` actually fits in a `usize`.
/// * `usize::try_from(val).unwrap()` doesn't work in a const context.
pub const fn usize_from_u32(val: u32) -> usize {
// This is essentially the same as `usize::try_from(val).unwrap()`, but
// works in a `const` context on stable.
if size_of::<usize>() < size_of::<u32>() && val < (usize::MAX as u32) {
panic!("value does not fit in a usize");
} else {
val as usize
}
}
/// Get the raw pointer from `opt`, defaulting to `null_mut`.
pub fn opt_nonnull_to_ptr<T>(opt: Option<NonNull<T>>) -> *mut T {
opt.map(NonNull::as_ptr).unwrap_or(ptr::null_mut())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_usize_from_u32() {
assert_eq!(usize_from_u32(0), 0usize);
assert_eq!(usize_from_u32(u32::MAX), 4294967295usize);
}
}