x_map/util.rs
1use std::ptr;
2
3/// Returns a `usize` corresponding to the size of a struct time the amount of space for those
4/// structs to be allocated.
5///
6/// This is useful for when we want to get an accurate memory size to allocate for a map.
7pub(in crate) fn new_capacity_of<T>(size: usize) -> usize {
8 size_of::<T>() * size
9}
10
11/// Compares the memory in two pointers.
12///
13/// The index at which the two pointers' underlying data no longer match is returned. This will
14/// return `None` if the two structures are identical.
15pub(in crate) unsafe fn mem_cmp(left: *const u8, right: *const u8, size: usize) -> Option<usize> {
16 // Compare each pointer byte-by-byte.
17 for i in 0..size {
18 if ptr::read(left.add(i)) != ptr::read(right.add(i)) {
19 return Some(i);
20 }
21 }
22
23 None
24}