Skip to content

Instantly share code, notes, and snippets.

@ChrisDenton
Created June 2, 2020 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisDenton/b7105ce946fe4388298cd144b4d06d00 to your computer and use it in GitHub Desktop.
Save ChrisDenton/b7105ce946fe4388298cd144b4d06d00 to your computer and use it in GitHub Desktop.
/// Never create or use this struct directly.
#[repr(C)]
pub struct RenameInfoW {
_flags: u32,
_root_dir: *const std::ffi::c_void,
_name_length: u32,
_name: [u16; 1],
}
impl RenameInfoW {
const FLAGS_OFFSET: usize = 0;
const DIR_OFFSET: usize = align::<usize>(Self::FLAGS_OFFSET + size_of::<u32>());
const LENGTH_OFFSET: usize = align::<u32>(Self::DIR_OFFSET + size_of::<usize>());
const NAME_OFFSET: usize = align::<u16>(Self::LENGTH_OFFSET + size_of::<u32>());
pub unsafe fn flags(ptr: *mut u8) -> *mut RenameOptions {
ptr.add(Self::FLAGS_OFFSET) as *mut _
}
pub unsafe fn root_dir(ptr: *mut u8) -> *mut *const std::ffi::c_void {
ptr.add(Self::DIR_OFFSET) as *mut _
}
pub unsafe fn name_length(ptr: *mut u8) -> *mut u32 {
ptr.add(Self::LENGTH_OFFSET) as *mut _
}
pub unsafe fn name(ptr: *mut u8) -> *mut u16 {
ptr.add(Self::NAME_OFFSET) as *mut _
}
}
/// Align `n` up to the alignment of `T`.
const fn align<T>(n: usize) -> usize {
round_up_pow2_unchecked(n, align_of::<T>())
}
/// Rounds `n` up to nearest multiple of `pow2`.
///
/// * `pow2` must be a power of two and not zero.
/// * `n` must be less than `usize::MAX - pow2 - 1`.
const fn round_up_pow2_unchecked(n: usize, pow2: usize) -> usize {
let round = pow2.wrapping_sub(1);
n.wrapping_add(round) & !round
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment