xxhash_rust/
utils.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
46
47
48
49
50
51
//! Utilities of the crate
use core::{ptr, mem};

#[inline(always)]
pub const fn get_aligned_chunk_ref<T: Copy>(input: &[u8], offset: usize) -> &T {
    debug_assert!(mem::size_of::<T>() > 0); //Size MUST be positive
    debug_assert!(mem::size_of::<T>() <= input.len().saturating_sub(offset)); //Must fit

    unsafe {
        &*(input.as_ptr().add(offset) as *const T)
    }
}

#[allow(unused)]
#[inline(always)]
pub const fn get_aligned_chunk<T: Copy>(input: &[u8], offset: usize) -> T {
    *get_aligned_chunk_ref(input, offset)
}

#[inline(always)]
pub fn get_unaligned_chunk<T: Copy>(input: &[u8], offset: usize) -> T {
    debug_assert!(mem::size_of::<T>() > 0); //Size MUST be positive
    debug_assert!(mem::size_of::<T>() <= input.len().saturating_sub(offset)); //Must fit

    unsafe {
        ptr::read_unaligned(input.as_ptr().add(offset) as *const T)
    }
}

#[derive(Debug)]
pub struct Buffer<T> {
    pub ptr: T,
    pub len: usize,
    pub offset: usize,
}

impl Buffer<*mut u8> {
    #[inline(always)]
    pub fn copy_from_slice(&self, src: &[u8]) {
        self.copy_from_slice_by_size(src, src.len())
    }

    #[inline(always)]
    pub fn copy_from_slice_by_size(&self, src: &[u8], len: usize) {
        debug_assert!(self.len.saturating_sub(self.offset) >= len);

        unsafe {
            ptr::copy_nonoverlapping(src.as_ptr(), self.ptr.add(self.offset), len);
        }
    }
}